diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index eb00403..490226b 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -749,6 +749,91 @@ class BrowsingMixin: return lyrics + def get_mood_categories(self) -> Dict: + """ + Fetch "Moods & Genres" categories from YouTube Music. + + :return: Dictionary of sections and categories. + + Example:: + + { + 'For you': [ + { + 'params': 'ggMPOg1uX1ZwN0pHT2NBT1Fk', + 'title': '1980s' + }, + { + 'params': 'ggMPOg1uXzZQbDB5eThLRTQ3', + 'title': 'Feel Good' + }, + ... + ], + 'Genres': [ + { + 'params': 'ggMPOg1uXzVLbmZnaWI4STNs', + 'title': 'Dance & Electronic' + }, + { + 'params': 'ggMPOg1uX3NjZllsNGVEMkZo', + 'title': 'Decades' + }, + ... + ], + 'Moods & moments': [ + { + 'params': 'ggMPOg1uXzVuc0dnZlhpV3Ba', + 'title': 'Chill' + }, + { + 'params': 'ggMPOg1uX2ozUHlwbWM3ajNq', + 'title': 'Commute' + }, + ... + ], + } + + """ + sections = {} + response = self._send_request('browse', {'browseId': 'FEmusic_moods_and_genres'}) + for section in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): + title = nav(section, GRID + ['header', 'gridHeaderRenderer'] + TITLE_TEXT) + sections[title] = [] + for category in nav(section, GRID_ITEMS): + sections[title].append({ + "title": nav(category, CATEGORY_TITLE), + "params": nav(category, CATEGORY_PARAMS) + }) + + return sections + + def get_mood_playlists(self, params: str) -> List[Dict]: + """ + Retrieve a list of playlists for a given "Moods & Genres" category. + + :param params: params obtained by :py:func:`get_mood_categories` + :return: List of playlists in the format of :py:func:`get_library_playlists` + + """ + playlists = [] + response = self._send_request('browse', { + 'browseId': 'FEmusic_moods_and_genres_category', + 'params': params + }) + for section in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST): + path = [] + if 'gridRenderer' in section: + path = GRID_ITEMS + elif 'musicCarouselShelfRenderer' in section: + path = ['musicCarouselShelfRenderer', 'contents'] + elif 'musicImmersiveCarouselShelfRenderer' in section: + path = ['musicImmersiveCarouselShelfRenderer', 'contents'] + if len(path): + results = nav(section, path) + playlists += parse_content_list(results, parse_playlist) + + return playlists + def get_basejs_url(self): """ Extract the URL for the `base.js` script from YouTube Music. @@ -758,7 +843,7 @@ class BrowsingMixin: response = self._send_get_request(url=YTM_DOMAIN) match = re.search(r'jsUrl"\s*:\s*"([^"]+)"', response) if match is None: - raise Exception("Could not identify the URL for base.js player.") + raise Exception("Could not identify the URL for base.js player.") return YTM_DOMAIN + match.group(1) def get_signatureTimestamp(self, url: str = None) -> int: @@ -767,13 +852,13 @@ class BrowsingMixin: `signatureTimestamp` for use with :py:func:`get_song`. :param url: Optional. Provide the URL of the `base.js` script. If this - isn't specified a call will be made to :py:func:`get_basejs_url`. + isn't specified a call will be made to :py:func:`get_basejs_url`. :return: `signatureTimestamp` string """ if url is None: - url = self.get_basejs_url() + url = self.get_basejs_url() response = self._send_get_request(url=url) match = re.search(r"signatureTimestamp[:=](\d+)", response) if match is None: - raise Exception("Unable to identify the signatureTimestamp.") + raise Exception("Unable to identify the signatureTimestamp.") return int(match.group(1)) diff --git a/ytmusicapi/parsers/__init__.py b/ytmusicapi/parsers/__init__.py index fd91e6a..d421665 100644 --- a/ytmusicapi/parsers/__init__.py +++ b/ytmusicapi/parsers/__init__.py @@ -39,3 +39,5 @@ BADGE_LABEL = [ 'badges', 0, 'musicInlineBadgeRenderer', 'accessibilityData', 'accessibilityData', 'label' ] RELOAD_CONTINUATION = ['continuations', 0, 'reloadContinuationData', 'continuation'] +CATEGORY_TITLE = ['musicNavigationButtonRenderer', 'buttonText'] + RUN_TEXT +CATEGORY_PARAMS = ['musicNavigationButtonRenderer', 'clickCommand', 'browseEndpoint', 'params']