-
-
Save Frulko/fda8121a65f5cc276285e84885f8eda5 to your computer and use it in GitHub Desktop.
Fetch, extract, parse, expand, frame and compact JSON-LD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { JSDOM } = require('jsdom') | |
const { compact, expand, frame } = require('jsonld') | |
const url = 'https://www.bbc.co.uk/schedules/p00fzl6p/2020/06/14' | |
// fetch and parse HTML | |
const { window: { document } } = await JSDOM.fromURL(url) | |
// select the script elements containing JSON-LD | |
const elements = document.querySelectorAll('script[type="application/ld+json"]') | |
for (const element of elements) { | |
// parse the JSON | |
const doc = JSON.parse(element.textContent) | |
console.log({ doc }) | |
// expand the doc to use full URIs for property names | |
// const expanded = await expand(doc) | |
// console.log({ expanded }) | |
// frame the doc to filter and pick certain properties | |
const framed = await frame(doc, { | |
'@context': 'https://schema.org/', | |
'@type': 'TVEpisode', | |
'@explicit': true, | |
identifier: {}, | |
description: {}, | |
name: {}, | |
published: {}, | |
image: {}, | |
partOfSeries: { | |
'@type': 'TVSeries', | |
'@explicit': true, | |
'@default': [], | |
identifier: {}, | |
name: {}, | |
}, | |
partOfSeason: { | |
'@type': 'TVSeason', | |
'@explicit': true, | |
'@default': [], | |
identifier: {}, | |
name: {}, | |
position: {}, | |
}, | |
}) | |
console.log({ framed }) | |
// compact the framed doc to use own property names | |
const compacted = await compact(framed, { | |
'@context': { | |
identifier: 'http://schema.org/identifier', | |
description: 'http://schema.org/description', | |
name: 'http://schema.org/name', | |
series: 'http://schema.org/partOfSeries', | |
season: 'http://schema.org/partOfSeason', | |
position: 'http://schema.org/position', | |
image: { | |
'@type': '@id', | |
'@id': 'http://schema.org/image', | |
}, | |
published: { | |
'@type': 'http://schema.org/Date', | |
'@id': 'http://schema.org/datePublished', | |
}, | |
}, | |
}) | |
console.log({ compacted }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment