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