From 68daeed093a39b861434b79ba94456980d0c2cb7 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Thu, 9 Sep 2021 12:44:49 +0530 Subject: [PATCH] Search command cleaned up --- play-dl/YouTube/search.ts | 1 + play-dl/YouTube/utils/parser.ts | 66 +++++++-------------------------- 2 files changed, 15 insertions(+), 52 deletions(-) diff --git a/play-dl/YouTube/search.ts b/play-dl/YouTube/search.ts index 913ff48..010788f 100644 --- a/play-dl/YouTube/search.ts +++ b/play-dl/YouTube/search.ts @@ -13,6 +13,7 @@ enum SearchType { export async function search(search :string, options? : ParseSearchInterface): Promise<(Video | Channel | PlayList)[]> { let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+') + if(!options || options.type) options = { type : "video" } if(!url.match('&sp=')){ url += '&sp=' switch(options?.type){ diff --git a/play-dl/YouTube/utils/parser.ts b/play-dl/YouTube/utils/parser.ts index 24afcc6..a79f865 100644 --- a/play-dl/YouTube/utils/parser.ts +++ b/play-dl/YouTube/utils/parser.ts @@ -3,7 +3,7 @@ import { PlayList } from "../classes/Playlist"; import { Channel } from "../classes/Channel"; export interface ParseSearchInterface { - type?: "video" | "playlist" | "channel" | "all"; + type?: "video" | "playlist" | "channel" ; limit?: number; } @@ -13,73 +13,35 @@ export interface thumbnail{ url : string } -export function ParseSearchResult(html :string, options? : ParseSearchInterface): (Video | PlayList | Channel)[] { +export function ParseSearchResult(html : string, options? : ParseSearchInterface): (Video | PlayList | Channel)[] { if(!html) throw new Error('Can\'t parse Search result without data') if (!options) options = { type: "video", limit: 0 }; if (!options.type) options.type = "video"; + let data = html.split("var ytInitialData = ")[1].split("}};")[0] + '}}'; + let json_data = JSON.parse(data) let results = [] - let details = [] - let fetched = false; - - try { - let data = html.split("ytInitialData = JSON.parse('")[1].split("');")[0]; - html = data.replace(/\\x([0-9A-F]{2})/gi, (...items) => { - return String.fromCharCode(parseInt(items[1], 16)); - }); - } catch { - /* do nothing */ - } - - try { - details = JSON.parse(html.split('{"itemSectionRenderer":{"contents":')[html.split('{"itemSectionRenderer":{"contents":').length - 1].split(',"continuations":[{')[0]); - fetched = true; - } catch { - /* Do nothing*/ - } - - if (!fetched) { - try { - details = JSON.parse(html.split('{"itemSectionRenderer":')[html.split('{"itemSectionRenderer":').length - 1].split('},{"continuationItemRenderer":{')[0]).contents; - fetched = true; - } catch { - /* do nothing */ - } - } - - if (!fetched) throw new Error('Failed to Fetch the data') - - for (let i = 0; i < details.length; i++) { + let details = json_data.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents + for(let i = 0; i < details.length; i++){ if (typeof options.limit === "number" && options.limit > 0 && results.length >= options.limit) break; - let data = details[i]; - let res; - if (options.type === "all") { - if (!!data.videoRenderer) options.type = "video"; - else if (!!data.channelRenderer) options.type = "channel"; - else if (!!data.playlistRenderer) options.type = "playlist"; - else continue; - } - if (options.type === "video") { - const parsed = parseVideo(data); + const parsed = parseVideo(details[i]); if (!parsed) continue; - res = parsed; + results.push(parsed) } else if (options.type === "channel") { - const parsed = parseChannel(data); + const parsed = parseChannel(details[i]); if (!parsed) continue; - res = parsed; + results.push(parsed) } else if (options.type === "playlist") { - const parsed = parsePlaylist(data); + const parsed = parsePlaylist(details[i]); if (!parsed) continue; - res = parsed; + results.push(parsed) } - - results.push(res); } - -return results as (Video | Channel | PlayList)[]; + return results } + function parseDuration(duration: string): number { duration ??= "0:00"; const args = duration.split(":");