UNPKG

metascraper

Version:

A library to easily scrape metadata from an article on the web using Open Graph, JSON+LD, regular HTML metadata, and series of fallbacks.

39 lines (31 loc) 932 B
'use strict' const debug = require('debug-logfmt')('metascraper:get-data') const { findRule, has } = require('@metascraper/helpers') const getData = async ({ rules, ...props }) => { const data = {} await Promise.all( rules.map(async ([propName, innerRules]) => { const duration = debug.duration() let normalizedValue = null let status = 'ok' try { const value = await findRule(innerRules, props, propName) normalizedValue = has(value) ? value : null } catch (error) { status = 'error' debug('rule:error', { propName, rules: innerRules.length, errorName: error?.name, errorMessage: error?.message }) } duration( `${propName}=${normalizedValue} rules=${innerRules.length} status=${status}` ) data[propName] = normalizedValue }) ) return data } module.exports = getData