refactor: move getLyrics to Innertube.js

This commit is contained in:
LuanRT
2022-02-05 19:16:36 -03:00
parent 339a01f3a9
commit 89c018c431
2 changed files with 21 additions and 21 deletions

View File

@@ -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.
*

View File

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