From f7ef0d2c15f9f3f9ee87e92cf6c22b1282c14751 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 23 Aug 2022 20:33:48 -0700 Subject: [PATCH] Covered more cases in unittest and documentation --- docs/source/reference.rst | 3 ++- tests/test.py | 13 +++++++++-- ytmusicapi/mixins/browsing.py | 42 ++++++++++++++++++++++++---------- ytmusicapi/parsers/browsing.py | 14 ------------ 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/docs/source/reference.rst b/docs/source/reference.rst index e373f5b..db3d769 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -31,7 +31,8 @@ Browsing .. automethod:: YTMusic.get_song .. automethod:: YTMusic.get_song_related .. automethod:: YTMusic.get_lyrics - +.. automethod:: YTMusic.get_tasteprofile +.. automethod:: YTMusic.set_tasteprofile Explore -------- diff --git a/tests/test.py b/tests/test.py index 0547838..76dabd3 100644 --- a/tests/test.py +++ b/tests/test.py @@ -165,10 +165,19 @@ class TestYTMusic(unittest.TestCase): self.assertIsNotNone(signatureTimestamp) def test_set_tasteprofile(self): - self.assertRaises(self.yt.set_tasteprofile("Galactic")) + artists = [artist for artist in self.yt.get_tasteprofile()] + + self.assertRaises(self.yt.set_tasteprofile(artists[:1])) + self.assertRaises(self.yt.set_tasteprofile(artists[:5])) + + self.assertRaises(self.yt_auth.set_tasteprofile(artists[:1])) + self.assertRaises(self.yt_auth.set_tasteprofile(artists[:5])) def test_get_tasteprofile(self): - result = self.yt.get_tasteprofiles() + result = self.yt.get_tasteprofile() + self.assertGreaterEqual(len(result), 0) + + result = self.yt_auth.get_tasteprofile() self.assertGreaterEqual(len(result), 0) ################ diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 67d8edf..ae41270 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -711,29 +711,48 @@ class BrowsingMixin: return int(match.group(1)) - def get_tasteprofiles(self) -> Dict: + def get_tasteprofile(self) -> Dict: """ - Fetches all artists from taste profile (music.youtube.com/tasteprofile) + Fetches suggested artists from taste profile (music.youtube.com/tasteprofile). + Tasteprofile allows users to pick artists to update their reccomendations. + Only returns a list of suggested artists, not the actual list of selected entries :return: Dictionary with artist and their selection & impression value Example:: - { - "Drake": { - "selectionValue": "tastebuilder_selection=/m/05mt_q", - "impressionValue": "tastebuilder_impression=/m/05mt_q" + + { + "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) + profiles = nav(response, TASTE_PROFILE_ITEMS) - def set_tasteprofile(self, *artists: str) -> None: + 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 + + def set_tasteprofile(self, artists: List[str]) -> None: """ - Favorites artists to see more recommendations from the artist + Favorites artists to see more recommendations from the artist. + Use get_tasteprofile() to see which artists are available to be recommended + + :param artists: A List with names of artists + """ - taste_profiles = self.get_tasteprofiles() + taste_profiles = self.get_tasteprofile() formData = { "impressionValues": [taste_profiles[profile]["impressionValue"] for profile in taste_profiles], @@ -746,5 +765,4 @@ class BrowsingMixin: formData["selectedValues"].append(taste_profiles[artist]["selectionValue"]) body = {'browseId': "FEmusic_home", "formData": formData} - self._send_request('browse', body) diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 177e4f0..cf8d63f 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -287,17 +287,3 @@ 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