Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/social-media/youtube-search.js
1126 views
import cheerio from 'cheerio';1import got from 'got';2export default async function youtubeSearch(query) {3const body = await got('https://www.youtube.com/results', {4headers: {5'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'6},7searchParams: {8search_query: query9}10}).text();11const $ = cheerio.load(body);12let sc;13$('script').map(function () {14const el = $(this).html();15let regex;16if ((regex = /var ytInitialData = /gi.exec(el || ''))) {17sc = JSON.parse(regex.input.replace(/^var ytInitialData = /i, '').replace(/;$/, ''));18}19return regex && sc;20});21const results = { video: [], channel: [], playlist: [] };22sc.contents.twoColumnSearchResultsRenderer.primaryContents23.sectionListRenderer.contents[0].itemSectionRenderer.contents.forEach((v) => {24var _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;25const typeName = Object.keys(v)[0];26const result = v[typeName];27if (['horizontalCardListRenderer', 'shelfRenderer'].includes(typeName)) {28return;29} // Todo: add this result as results30const isChannel = typeName === 'channelRenderer';31const isVideo = typeName === 'videoRenderer';32const isMix = typeName === 'radioRenderer';33if (isVideo) {34const view = ((_a = result.viewCountText) === null || _a === void 0 ? void 0 : _a.simpleText) ||35((_b = result.shortViewCountText) === null || _b === void 0 ? void 0 : _b.simpleText) ||36((_d = (_c = result.shortViewCountText) === null || _c === void 0 ? void 0 : _c.accessibility) === null || _d === void 0 ? void 0 : _d.accessibilityData.label);37const _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;38const videoId = result.videoId;39const duration = ((_g = result.lengthText) === null || _g === void 0 ? void 0 : _g.simpleText) || (_duration === null || _duration === void 0 ? void 0 : _duration.simpleText);40let durationS = 0;41(_h = ((duration === null || duration === void 0 ? void 0 : duration.split('.').length) && duration.indexOf(':') === -142? duration.split('.')43: duration === null || duration === void 0 ? void 0 : duration.split(':'))) === null || _h === void 0 ? void 0 : _h.forEach((v, i, arr) => (durationS +=44durationMultipliers[arr.length]['' + i] * parseInt(v)));45results.video.push({46authorName: (_l = (((_j = result.ownerText) === null || _j === void 0 ? void 0 : _j.runs) ||47((_k = result.longBylineText) === null || _k === void 0 ? void 0 : _k.runs) ||48[])[0]) === null || _l === void 0 ? void 0 : _l.text,49authorAvatar: (_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,50videoId,51url: encodeURI('https://www.youtube.com/watch?v=' + videoId),52thumbnail: result.thumbnail.thumbnails.pop().url,53title: (_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) ||54((_s = result.title) === null || _s === void 0 ? void 0 : _s.accessibility.accessibilityData.label))) === null || _t === void 0 ? void 0 : _t.trim(),55description: (_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(''),56publishedTime: (_z = result.publishedTimeText) === null || _z === void 0 ? void 0 : _z.simpleText,57durationH: ((_0 = result.lengthText) === null || _0 === void 0 ? void 0 : _0.accessibility.accessibilityData.label) ||58(_duration === null || _duration === void 0 ? void 0 : _duration.accessibility.accessibilityData.label),59durationS,60duration,61viewH: view,62view: (_1 = (((view === null || view === void 0 ? void 0 : view.indexOf('x')) === -163? view === null || view === void 0 ? void 0 : view.split(' ')[0]64: view === null || view === void 0 ? void 0 : view.split('x')[0]) || view)) === null || _1 === void 0 ? void 0 : _1.trim(),65type: typeName.replace(/Renderer/i, '')66});67}68if (isChannel) {69const channelId = result.channelId;70const _subscriber = ((_2 = result.subscriberCountText) === null || _2 === void 0 ? void 0 : _2.accessibility.accessibilityData.label) ||71((_3 = result.subscriberCountText) === null || _3 === void 0 ? void 0 : _3.simpleText);72results.channel.push({73channelId,74url: encodeURI('https://www.youtube.com/channel/' + channelId),75channelName: result.title.simpleText ||76((_5 = (_4 = result.shortBylineText) === null || _4 === void 0 ? void 0 : _4.runs.find((v) => v.text)) === null || _5 === void 0 ? void 0 : _5.text),77avatar: 'https:' +78((_6 = result.thumbnail.thumbnails79.filter(({ url }) => url)) === null || _6 === void 0 ? void 0 : _6.pop().url),80isVerified: ((_7 = result.ownerBadges) === null || _7 === void 0 ? void 0 : _7.pop().metadataBadgeRenderer.style) ===81'BADGE_STYLE_TYPE_VERIFIED',82subscriberH: _subscriber === null || _subscriber === void 0 ? void 0 : _subscriber.trim(),83subscriber: _subscriber === null || _subscriber === void 0 ? void 0 : _subscriber.split(' ')[0],84videoCount: parseInt((_9 = (_8 = result.videoCountText) === null || _8 === void 0 ? void 0 : _8.runs[0]) === null || _9 === void 0 ? void 0 : _9.text),85description: (_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(''),86type: typeName.replace(/Renderer/i, '')87});88}89if (isMix) {90results.playlist.push({91playlistId: result.playlistId,92title: result.title.simpleText,93thumbnail: result.thumbnail.thumbnails.pop().url,94video: result.videos.map(({ childVideoRenderer }) => {95return {96videoId: childVideoRenderer.videoId,97title: childVideoRenderer.title.simpleText,98durationH: childVideoRenderer.lengthText.accessibility99.accessibilityData.label,100duration: childVideoRenderer.lengthText.simpleText101};102}),103type: 'mix'104});105}106});107return results;108}109const durationMultipliers = {1101: {1110: 1112},1132: {1140: 60,1151: 1116},1173: {1180: 3600,1191: 60,1202: 1121}122};123//# sourceMappingURL=youtube-search.js.map124125