From 89c018c43157e9249f0d2b665888b2feb6c0c2c5 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Sat, 5 Feb 2022 19:16:36 -0300 Subject: [PATCH] refactor: move getLyrics to Innertube.js --- lib/Innertube.js | 19 +++++++++++++++++++ lib/Parser.js | 23 ++--------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/Innertube.js b/lib/Innertube.js index 2e54a940..facd20fb 100644 --- a/lib/Innertube.js +++ b/lib/Innertube.js @@ -150,6 +150,25 @@ class Innertube { return refined_data; } + /** + * Retrieves the lyrics for a given song + * + * @param {string} id + * @returns {string} Song lyrics + */ + async getLyrics(id) { + const data_continuation = await Actions.getContinuation(this, { video_id: id, ytmusic: true }); + + const lyrics_tab = data_continuation.data.contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer + .watchNextTabbedResultsRenderer.tabs.find((obj) => obj.tabRenderer.title == 'Lyrics'); + + const response = await Actions.browse(this, 'lyrics', { ytmusic: true, browse_id: lyrics_tab.tabRenderer.endpoint.browseEndpoint.browseId }); + if (!response.data.contents.sectionListRenderer) throw new Error(response.data.contents.messageRenderer.text.runs[0].text); + + const lyrics = response.data.contents.sectionListRenderer.contents[0].musicDescriptionShelfRenderer.description.runs[0].text; + return lyrics + } + /** * Gets the comments section of a video. * diff --git a/lib/Parser.js b/lib/Parser.js index 5c3ad044..76908d83 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -77,25 +77,6 @@ class Parser { const tabs = this.data.contents.tabbedSearchResultsRenderer.tabs; const contents = tabs[0].tabRenderer.content.sectionListRenderer.contents; - /** - * Retrieves the lyrics for a given song - * - * @param {string} id - * @returns {string} Song lyrics - */ - const getLyrics = async (id) => { - const data_continuation = await Actions.getContinuation(this.session, { video_id: id, ytmusic: true }); - - const lyrics_tab = data_continuation.data.contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer - .watchNextTabbedResultsRenderer.tabs.find((obj) => obj.tabRenderer.title == 'Lyrics'); - - const response = await Actions.browse(this.session, 'lyrics', { ytmusic: true, browse_id: lyrics_tab.tabRenderer.endpoint.browseEndpoint.browseId }); - if (!response.data.contents.sectionListRenderer) throw new Error(response.data.contents.messageRenderer.text.runs[0].text); - - const lyrics = response.data.contents.sectionListRenderer.contents[0].musicDescriptionShelfRenderer.description.runs[0].text; - return lyrics - } - const songs_ms = contents.find((content) => content.musicShelfRenderer.title.runs[0].text == 'Songs'); const songs = songs_ms.musicShelfRenderer.contents.map((item) => { const list_item = item.musicResponsiveListItemRenderer; @@ -106,7 +87,7 @@ class Parser { album: list_item.flexColumns[1].musicResponsiveListItemFlexColumnRenderer.text.runs[4].text, duration: list_item.flexColumns[1].musicResponsiveListItemFlexColumnRenderer.text.runs[6].text, thumbnail: list_item.thumbnail.musicThumbnailRenderer.thumbnail, - getLyrics: () => getLyrics(list_item.playlistItemData.videoId) + getLyrics: () => this.session.getLyrics(list_item.playlistItemData.videoId) }; }); @@ -120,7 +101,7 @@ class Parser { views: list_item.flexColumns[1].musicResponsiveListItemFlexColumnRenderer.text.runs[4].text, duration: list_item.flexColumns[1].musicResponsiveListItemFlexColumnRenderer.text.runs[6].text, thumbnail: list_item.thumbnail.musicThumbnailRenderer.thumbnail, - getLyrics: () => getLyrics(list_item.playlistItemData.videoId) + getLyrics: () => this.session.getLyrics(list_item.playlistItemData.videoId) }; });