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.

24 lines (18 loc) 719 B
'use strict' const debug = require('debug-logfmt')('metascraper:get-data') const { findRule, has } = require('@metascraper/helpers') const { map, fromPairs } = require('lodash') const normalizeValue = value => (has(value) ? value : null) const getData = async ({ rules, name, ...props }) => { const data = await Promise.all( map(rules, async ([propName, innerRules]) => { const duration = debug.duration() const value = await findRule(innerRules, props, propName) const normalizedValue = normalizeValue(value) duration(`${propName}=${normalizedValue} rules=${innerRules.length}`) return [propName, normalizedValue] }) ) return fromPairs(data) } module.exports = getData