This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
forked from HermanFassett/youtube-scrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
49 lines (49 loc) · 1.56 KB
/
scraper.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
37
38
39
40
41
42
43
44
45
46
47
48
49
/* eslint-disable no-prototype-builtins */
const centra = require('@nia3208/centra');
const scrapper = async params => {
const results = [];
let data;
const html = await centra('https://www.youtube.com/results')
.header(
'User-Agent',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'
)
.query({q: params, page: 1})
.text();
const parser = data => {
return {
id: data.videoId,
thumbnail:
data.thumbnail.thumbnails[data.thumbnail.thumbnails.length - 1].url,
title: data.title.runs[0].text,
duration: data.lengthText ? data.lengthText.simpleText : 'LIVE',
url: `https://youtube.com${data.navigationEndpoint.commandMetadata.webCommandMetadata.url}`,
};
};
try {
data = JSON.parse(
html
.substring(
html.indexOf('window["ytInitialData"] = '),
html.indexOf('window["ytInitialPlayerResponse"]')
)
.replace('window["ytInitialData"] = ', '')
.replace(';', '')
);
} catch (e) {
data = JSON.parse(
html
.split('ytInitialData = ')[1]
.split('</script>')[0]
.replace('// scraper_data_end', '')
.replace(';', '')
);
}
data.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents
.filter(x => x.hasOwnProperty('itemSectionRenderer'))
.map(x => x.itemSectionRenderer.contents)[0]
.filter(x => x.hasOwnProperty('videoRenderer'))
.forEach(x => results.push(parser(x.videoRenderer)));
return results;
};
module.exports = scrapper;