From f9664fe2677f7c652aa73098803e79c03b98fc94 Mon Sep 17 00:00:00 2001 From: SAROND Date: Tue, 16 Jan 2024 04:05:42 +0400 Subject: [PATCH] feat(YTMusic): implement continuous pagination in getHome method This commit introduces the ability to fetch and parse additional pages of content in the `getHome` method of the `YTMusic` class by utilizing a continuation token. The previous implementation only fetched the initial page of content. Now, after parsing the initial page, the method checks for a continuation token and continues to fetch and parse subsequent pages until no continuation token is found, allowing for complete retrieval of home page content. Additionally, there has been a minor change in the `SongParser.parseSearchResult` method where the logic to identify the title and artist from search results has been simplified due to the impossibility of distinguishing them with the current data structure. The new approach uses fixed positions in the columns array to assign title and artist. --- src/YTMusic.ts | 16 +++++++++++++--- src/parsers/SongParser.ts | 7 ++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/YTMusic.ts b/src/YTMusic.ts index b33d851..55a7555 100644 --- a/src/YTMusic.ts +++ b/src/YTMusic.ts @@ -15,15 +15,15 @@ import { VideoDetailed, VideoFull, } from "./@types/types" +import { FE_MUSIC_HOME } from "./constants" import AlbumParser from "./parsers/AlbumParser" import ArtistParser from "./parsers/ArtistParser" +import Parser from "./parsers/Parser" import PlaylistParser from "./parsers/PlaylistParser" import SearchParser from "./parsers/SearchParser" import SongParser from "./parsers/SongParser" import VideoParser from "./parsers/VideoParser" import { traverse, traverseList, traverseString } from "./utils/traverse" -import { FE_MUSIC_HOME } from "./constants" -import Parser from "./parsers/Parser" export default class YTMusic { private cookiejar: CookieJar @@ -510,11 +510,21 @@ export default class YTMusic { public async getHome(): Promise { const results: HomePageContent[] = [] const page = await this.constructRequest("browse", { browseId: FE_MUSIC_HOME }) - traverseList(page, "sectionListRenderer", "contents").forEach(content => { + traverseList(page, "contents").forEach(content => { const parsed = Parser.parseMixedContent(content) parsed && results.push(parsed) }) + let continuation = traverseString(page, "continuation") + while (continuation) { + const nextPage = await this.constructRequest("browse", {}, { continuation }) + traverseList(nextPage, "contents").forEach(content => { + const parsed = Parser.parseMixedContent(content) + parsed && results.push(parsed) + }) + continuation = traverseString(nextPage, "continuation") + } + return results } } diff --git a/src/parsers/SongParser.ts b/src/parsers/SongParser.ts index 39c354c..cb7f21c 100644 --- a/src/parsers/SongParser.ts +++ b/src/parsers/SongParser.ts @@ -25,10 +25,11 @@ export default class SongParser { } public static parseSearchResult(item: any): SongDetailed { - const columns = traverseList(item, "flexColumns", "runs").flat() + const columns = traverseList(item, "flexColumns", "runs") - const title = columns.find(isTitle) - const artist = columns.find(isArtist) || columns[1] + // It is not possible to identify the title and author + const title = columns[0] + const artist = columns[1] const album = columns.find(isAlbum) ?? null const duration = columns.find(isDuration)