-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathontology_lookups.js
36 lines (32 loc) · 1.14 KB
/
ontology_lookups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const microdataParser = require('microdata-node');
const Apify = require('apify');
const jsonLdLookup = async (page) => {
// const url = page.url();
// console.time(`${url} jsonLdLookup`);
let isJsonLd = false;
let jsonLdData = {};
if (await page.$('script[type="application/ld+json"]')) {
try {
isJsonLd = true;
jsonLdData = await page.$eval('script[type="application/ld+json"]', (el) => JSON.parse(el.innerText));
} catch (e) {
Apify.utils.log.warning(`Parsing LD+JSON failed: ${e.message}`);
}
}
// console.timeEnd(`${url} jsonLdLookup`);
return { isJsonLd, jsonLdData };
};
const microdataLookup = async (page) => {
// const url = page.url();
// console.time(`${url} microdataLookup`);
let isMicrodata = false;
const pageHtml = await page.evaluate(() => document.documentElement.outerHTML);
const microdata = microdataParser.toJsonld(pageHtml, {});
if (microdata.length) isMicrodata = true;
// console.timeEnd(`${url} microdataLookup`);
return { isMicrodata, microdata };
};
module.exports = {
microdataLookup,
jsonLdLookup,
};