From d66f20aa13ce2117fce7abcadbb9ae288bd352e7 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sat, 20 Aug 2022 23:43:39 -0700 Subject: [PATCH] Addded favorite artists to taste --- tests/test.py | 15 +++++++--- ytmusicapi/mixins/browsing.py | 51 +++++++++++++++++++++++++++++----- ytmusicapi/navigation.py | 2 ++ ytmusicapi/parsers/browsing.py | 21 +++++++++++--- 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/tests/test.py b/tests/test.py index 50eae4e..0547838 100644 --- a/tests/test.py +++ b/tests/test.py @@ -164,9 +164,16 @@ class TestYTMusic(unittest.TestCase): signatureTimestamp = self.yt.get_signatureTimestamp() self.assertIsNotNone(signatureTimestamp) - ############### + def test_set_tasteprofile(self): + self.assertRaises(self.yt.set_tasteprofile("Galactic")) + + def test_get_tasteprofile(self): + result = self.yt.get_tasteprofiles() + self.assertGreaterEqual(len(result), 0) + + ################ # EXPLORE - ############### + ################ def test_get_mood_playlists(self): categories = self.yt.get_mood_categories() @@ -206,9 +213,9 @@ class TestYTMusic(unittest.TestCase): playlist = self.yt_brand.get_watch_playlist_shuffle(playlistId=config['playlists']['own']) self.assertEqual(len(playlist['tracks']), 4) - ############### + ################ # LIBRARY - ############### + ################ def test_get_library_playlists(self): playlists = self.yt_auth.get_library_playlists(50) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 26b07b1..67d8edf 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -211,9 +211,7 @@ class BrowsingMixin: artist = {'description': None, 'views': None} header = response['header']['musicImmersiveHeaderRenderer'] artist['name'] = nav(header, TITLE_TEXT) - descriptionShelf = find_object_by_key(results, - DESCRIPTION_SHELF[0], - is_key=True) + descriptionShelf = find_object_by_key(results, DESCRIPTION_SHELF[0], is_key=True) if descriptionShelf: artist['description'] = nav(descriptionShelf, DESCRIPTION) artist['views'] = None if 'subheader' not in descriptionShelf else descriptionShelf[ @@ -674,10 +672,11 @@ class BrowsingMixin: raise Exception("Invalid browseId provided. This song might not have lyrics.") response = self._send_request('browse', {'browseId': browseId}) - lyrics['lyrics'] = nav(response, ['contents'] + SECTION_LIST_ITEM - + DESCRIPTION_SHELF + DESCRIPTION, True) - lyrics['source'] = nav(response, ['contents'] + SECTION_LIST_ITEM - + DESCRIPTION_SHELF + ['footer'] + RUN_TEXT, True) + lyrics['lyrics'] = nav(response, + ['contents'] + SECTION_LIST_ITEM + DESCRIPTION_SHELF + DESCRIPTION, + True) + lyrics['source'] = nav(response, ['contents'] + SECTION_LIST_ITEM + DESCRIPTION_SHELF + + ['footer'] + RUN_TEXT, True) return lyrics @@ -711,3 +710,41 @@ class BrowsingMixin: raise Exception("Unable to identify the signatureTimestamp.") return int(match.group(1)) + + def get_tasteprofiles(self) -> Dict: + """ + Fetches all artists from taste profile (music.youtube.com/tasteprofile) + + :return: Dictionary with artist and their selection & impression value + + Example:: + { + "Drake": { + "selectionValue": "tastebuilder_selection=/m/05mt_q", + "impressionValue": "tastebuilder_impression=/m/05mt_q" + } + } + """ + response = self._send_request('browse', {'browseId': "FEmusic_tastebuilder"}) + return parse_tasteprofiles(response) + + def set_tasteprofile(self, *artists: str) -> None: + """ + Favorites artists to see more recommendations from the artist + """ + + taste_profiles = self.get_tasteprofiles() + formData = { + "impressionValues": + [taste_profiles[profile]["impressionValue"] for profile in taste_profiles], + "selectedValues": [] + } + + for artist in artists: + if artist not in taste_profiles: + raise Exception("The artist, {}, was not present in taste!".format(artist)) + formData["selectedValues"].append(taste_profiles[artist]["selectionValue"]) + + body = {'browseId': "FEmusic_home", "formData": formData} + + self._send_request('browse', body) diff --git a/ytmusicapi/navigation.py b/ytmusicapi/navigation.py index 1937075..2a74fa2 100644 --- a/ytmusicapi/navigation.py +++ b/ytmusicapi/navigation.py @@ -57,6 +57,8 @@ CATEGORY_TITLE = ['musicNavigationButtonRenderer', 'buttonText'] + RUN_TEXT CATEGORY_PARAMS = ['musicNavigationButtonRenderer', 'clickCommand', 'browseEndpoint', 'params'] MRLIR = 'musicResponsiveListItemRenderer' MTRIR = 'musicTwoRowItemRenderer' +TASTE_PROFILE_ITEMS = ["contents", "tastebuilderRenderer", "contents"] +TASTE_PROFILE_ARTIST = ["title", "runs"] def nav(root, items, none_if_absent=False): diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index ed325dc..177e4f0 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -227,9 +227,7 @@ def parse_song(result): def parse_song_flat(data): - columns = [ - get_flex_column_item(data, i) for i in range(0, len(data['flexColumns'])) - ] + columns = [get_flex_column_item(data, i) for i in range(0, len(data['flexColumns']))] song = { 'title': nav(columns[0], TEXT_RUN_TEXT), 'videoId': nav(columns[0], TEXT_RUN + NAVIGATION_VIDEO_ID, True), @@ -237,7 +235,8 @@ def parse_song_flat(data): 'thumbnails': nav(data, THUMBNAILS), 'isExplicit': nav(data, BADGE_LABEL, True) is not None } - if len(columns) > 2 and columns[2] is not None and 'navigationEndpoint' in nav(columns[2], TEXT_RUN): + if len(columns) > 2 and columns[2] is not None and 'navigationEndpoint' in nav( + columns[2], TEXT_RUN): song['album'] = { 'name': nav(columns[2], TEXT_RUN_TEXT), 'id': nav(columns[2], TEXT_RUN + NAVIGATION_BROWSE_ID) @@ -288,3 +287,17 @@ def parse_related_artist(data): 'subscribers': subscribers, 'thumbnails': nav(data, THUMBNAIL_RENDERER), } + + +def parse_tasteprofiles(data): + profiles = nav(data, TASTE_PROFILE_ITEMS) + + taste_profiles = {} + for itemList in profiles: + for item in itemList["tastebuilderItemListRenderer"]["contents"]: + artist = nav(item["tastebuilderItemRenderer"], TASTE_PROFILE_ARTIST)[0]["text"] + taste_profiles[artist] = { + "selectionValue": item["tastebuilderItemRenderer"]["selectionFormValue"], + "impressionValue": item["tastebuilderItemRenderer"]["impressionFormValue"] + } + return taste_profiles