Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/social-media/youtube-search.js
1126 views
1
import cheerio from 'cheerio';
2
import got from 'got';
3
export default async function youtubeSearch(query) {
4
const body = await got('https://www.youtube.com/results', {
5
headers: {
6
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
7
},
8
searchParams: {
9
search_query: query
10
}
11
}).text();
12
const $ = cheerio.load(body);
13
let sc;
14
$('script').map(function () {
15
const el = $(this).html();
16
let regex;
17
if ((regex = /var ytInitialData = /gi.exec(el || ''))) {
18
sc = JSON.parse(regex.input.replace(/^var ytInitialData = /i, '').replace(/;$/, ''));
19
}
20
return regex && sc;
21
});
22
const results = { video: [], channel: [], playlist: [] };
23
sc.contents.twoColumnSearchResultsRenderer.primaryContents
24
.sectionListRenderer.contents[0].itemSectionRenderer.contents.forEach((v) => {
25
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13;
26
const typeName = Object.keys(v)[0];
27
const result = v[typeName];
28
if (['horizontalCardListRenderer', 'shelfRenderer'].includes(typeName)) {
29
return;
30
} // Todo: add this result as results
31
const isChannel = typeName === 'channelRenderer';
32
const isVideo = typeName === 'videoRenderer';
33
const isMix = typeName === 'radioRenderer';
34
if (isVideo) {
35
const view = ((_a = result.viewCountText) === null || _a === void 0 ? void 0 : _a.simpleText) ||
36
((_b = result.shortViewCountText) === null || _b === void 0 ? void 0 : _b.simpleText) ||
37
((_d = (_c = result.shortViewCountText) === null || _c === void 0 ? void 0 : _c.accessibility) === null || _d === void 0 ? void 0 : _d.accessibilityData.label);
38
const _duration = (_f = (_e = result.thumbnailOverlays) === null || _e === void 0 ? void 0 : _e.find((v) => Object.keys(v)[0] === 'thumbnailOverlayTimeStatusRenderer')) === null || _f === void 0 ? void 0 : _f.thumbnailOverlayTimeStatusRenderer.text;
39
const videoId = result.videoId;
40
const duration = ((_g = result.lengthText) === null || _g === void 0 ? void 0 : _g.simpleText) || (_duration === null || _duration === void 0 ? void 0 : _duration.simpleText);
41
let durationS = 0;
42
(_h = ((duration === null || duration === void 0 ? void 0 : duration.split('.').length) && duration.indexOf(':') === -1
43
? duration.split('.')
44
: duration === null || duration === void 0 ? void 0 : duration.split(':'))) === null || _h === void 0 ? void 0 : _h.forEach((v, i, arr) => (durationS +=
45
durationMultipliers[arr.length]['' + i] * parseInt(v)));
46
results.video.push({
47
authorName: (_l = (((_j = result.ownerText) === null || _j === void 0 ? void 0 : _j.runs) ||
48
((_k = result.longBylineText) === null || _k === void 0 ? void 0 : _k.runs) ||
49
[])[0]) === null || _l === void 0 ? void 0 : _l.text,
50
authorAvatar: (_p = (_o = (_m = result.channelThumbnailSupportedRenderers) === null || _m === void 0 ? void 0 : _m.channelThumbnailWithLinkRenderer.thumbnail.thumbnails) === null || _o === void 0 ? void 0 : _o.filter(({ url }) => url)) === null || _p === void 0 ? void 0 : _p.pop().url,
51
videoId,
52
url: encodeURI('https://www.youtube.com/watch?v=' + videoId),
53
thumbnail: result.thumbnail.thumbnails.pop().url,
54
title: (_t = (((_r = (_q = result.title) === null || _q === void 0 ? void 0 : _q.runs.find((v) => v.text)) === null || _r === void 0 ? void 0 : _r.text) ||
55
((_s = result.title) === null || _s === void 0 ? void 0 : _s.accessibility.accessibilityData.label))) === null || _t === void 0 ? void 0 : _t.trim(),
56
description: (_y = (_x = (_w = (_v = (_u = result.detailedMetadataSnippets) === null || _u === void 0 ? void 0 : _u[0]) === null || _v === void 0 ? void 0 : _v.snippetText.runs) === null || _w === void 0 ? void 0 : _w.filter(({ text }) => text)) === null || _x === void 0 ? void 0 : _x.map(({ text }) => text)) === null || _y === void 0 ? void 0 : _y.join(''),
57
publishedTime: (_z = result.publishedTimeText) === null || _z === void 0 ? void 0 : _z.simpleText,
58
durationH: ((_0 = result.lengthText) === null || _0 === void 0 ? void 0 : _0.accessibility.accessibilityData.label) ||
59
(_duration === null || _duration === void 0 ? void 0 : _duration.accessibility.accessibilityData.label),
60
durationS,
61
duration,
62
viewH: view,
63
view: (_1 = (((view === null || view === void 0 ? void 0 : view.indexOf('x')) === -1
64
? view === null || view === void 0 ? void 0 : view.split(' ')[0]
65
: view === null || view === void 0 ? void 0 : view.split('x')[0]) || view)) === null || _1 === void 0 ? void 0 : _1.trim(),
66
type: typeName.replace(/Renderer/i, '')
67
});
68
}
69
if (isChannel) {
70
const channelId = result.channelId;
71
const _subscriber = ((_2 = result.subscriberCountText) === null || _2 === void 0 ? void 0 : _2.accessibility.accessibilityData.label) ||
72
((_3 = result.subscriberCountText) === null || _3 === void 0 ? void 0 : _3.simpleText);
73
results.channel.push({
74
channelId,
75
url: encodeURI('https://www.youtube.com/channel/' + channelId),
76
channelName: result.title.simpleText ||
77
((_5 = (_4 = result.shortBylineText) === null || _4 === void 0 ? void 0 : _4.runs.find((v) => v.text)) === null || _5 === void 0 ? void 0 : _5.text),
78
avatar: 'https:' +
79
((_6 = result.thumbnail.thumbnails
80
.filter(({ url }) => url)) === null || _6 === void 0 ? void 0 : _6.pop().url),
81
isVerified: ((_7 = result.ownerBadges) === null || _7 === void 0 ? void 0 : _7.pop().metadataBadgeRenderer.style) ===
82
'BADGE_STYLE_TYPE_VERIFIED',
83
subscriberH: _subscriber === null || _subscriber === void 0 ? void 0 : _subscriber.trim(),
84
subscriber: _subscriber === null || _subscriber === void 0 ? void 0 : _subscriber.split(' ')[0],
85
videoCount: parseInt((_9 = (_8 = result.videoCountText) === null || _8 === void 0 ? void 0 : _8.runs[0]) === null || _9 === void 0 ? void 0 : _9.text),
86
description: (_13 = (_12 = (_11 = (_10 = result.descriptionSnippet) === null || _10 === void 0 ? void 0 : _10.runs) === null || _11 === void 0 ? void 0 : _11.filter(({ text }) => text)) === null || _12 === void 0 ? void 0 : _12.map(({ text }) => text)) === null || _13 === void 0 ? void 0 : _13.join(''),
87
type: typeName.replace(/Renderer/i, '')
88
});
89
}
90
if (isMix) {
91
results.playlist.push({
92
playlistId: result.playlistId,
93
title: result.title.simpleText,
94
thumbnail: result.thumbnail.thumbnails.pop().url,
95
video: result.videos.map(({ childVideoRenderer }) => {
96
return {
97
videoId: childVideoRenderer.videoId,
98
title: childVideoRenderer.title.simpleText,
99
durationH: childVideoRenderer.lengthText.accessibility
100
.accessibilityData.label,
101
duration: childVideoRenderer.lengthText.simpleText
102
};
103
}),
104
type: 'mix'
105
});
106
}
107
});
108
return results;
109
}
110
const durationMultipliers = {
111
1: {
112
0: 1
113
},
114
2: {
115
0: 60,
116
1: 1
117
},
118
3: {
119
0: 3600,
120
1: 60,
121
2: 1
122
}
123
};
124
//# sourceMappingURL=youtube-search.js.map
125