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