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.
This commit is contained in:
SAROND 2024-01-16 04:05:42 +04:00
parent b428f87321
commit f9664fe267
2 changed files with 17 additions and 6 deletions

View File

@ -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<HomePageContent[]> {
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
}
}

View File

@ -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)