-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
49 lines (37 loc) · 1.48 KB
/
api.js
File metadata and controls
49 lines (37 loc) · 1.48 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
const puppeteer = require('puppeteer');
async function GetAnnotion(id) {
console.log("API: annotation requested: "+id)
var req = await fetch(`https://genius.com/api/annotations/${id}`)
var json = await req.json()
return json.response
}
async function GetGeniusBody(url) { //html crawler
console.log("API: song page requested: "+ url)
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setJavaScriptEnabled(true);
//prevents the loading of components other than script tags.
await page.setRequestInterception(true);
page.on('request', request => {
if (['image', 'stylesheet', 'font', 'script', 'xhr', 'fetch'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
page.on("console", (msg) => console.log("Browser Logs (ignore it):", msg.text()));
await page.setExtraHTTPHeaders({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
"Accept": "*/*"
});
await page.setBypassCSP(true);
await page.goto(url, { method: 'GET', waitUntil: 'domcontentloaded' });
var state = await page.evaluate(() => window.__PRELOADED_STATE__);
await browser.close();
return state;
//return require("./preload.json") // for test
}
exports = module.exports = {
GetAnnotion,
GetGeniusBody
}