From 4512dac4091a2c639077c5f13f18415a9a3c9a18 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Wed, 24 Aug 2022 09:45:20 +0200 Subject: [PATCH] set_tasteprofile: make internal get_tasteprofile request optional (#289) --- tests/test.py | 10 +++++----- ytmusicapi/mixins/browsing.py | 23 +++++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/tests/test.py b/tests/test.py index 0448621..0896a24 100644 --- a/tests/test.py +++ b/tests/test.py @@ -165,13 +165,13 @@ class TestYTMusic(unittest.TestCase): self.assertIsNotNone(signatureTimestamp) def test_set_tasteprofile(self): - artists = [artist for artist in self.yt.get_tasteprofile()] - self.assertRaises(Exception, self.yt.set_tasteprofile, "not an artist") - self.assertIsNone(self.yt.set_tasteprofile(artists[:5])) + taste_profile = self.yt.get_tasteprofile() + self.assertIsNone(self.yt.set_tasteprofile(list(taste_profile)[:5], taste_profile)) - self.assertIsNone(self.yt_brand.set_tasteprofile(artists[:1])) - self.assertIsNone(self.yt_brand.set_tasteprofile(artists[:5])) + self.assertRaises(Exception, self.yt_brand.set_tasteprofile, ["test", "test2"]) + taste_profile = self.yt_brand.get_tasteprofile() + self.assertIsNone(self.yt_brand.set_tasteprofile(list(taste_profile)[:1], taste_profile)) def test_get_tasteprofile(self): result = self.yt.get_tasteprofile() diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index ae41270..113b8c7 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -714,7 +714,7 @@ class BrowsingMixin: def get_tasteprofile(self) -> Dict: """ Fetches suggested artists from taste profile (music.youtube.com/tasteprofile). - Tasteprofile allows users to pick artists to update their reccomendations. + Tasteprofile allows users to pick artists to update their recommendations. Only returns a list of suggested artists, not the actual list of selected entries :return: Dictionary with artist and their selection & impression value @@ -725,11 +725,11 @@ class BrowsingMixin: "Drake": { "selectionValue": "tastebuilder_selection=/m/05mt_q" "impressionValue": "tastebuilder_impression=/m/05mt_q" - } + } } """ - + response = self._send_request('browse', {'browseId': "FEmusic_tastebuilder"}) profiles = nav(response, TASTE_PROFILE_ITEMS) @@ -743,26 +743,29 @@ class BrowsingMixin: } return taste_profiles - def set_tasteprofile(self, artists: List[str]) -> None: + def set_tasteprofile(self, artists: List[str], taste_profile: Dict = None) -> None: """ 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 - + :param artists: A List with names of artists, must be contained in the tasteprofile + :param taste_profile: tasteprofile result from :py:func:`get_tasteprofile`. + Pass this if you call :py:func:`get_tasteprofile` anyway to save an extra request. + :return None if successful """ - taste_profiles = self.get_tasteprofile() + if taste_profile is None: + taste_profile = self.get_tasteprofile() formData = { "impressionValues": - [taste_profiles[profile]["impressionValue"] for profile in taste_profiles], + [taste_profile[profile]["impressionValue"] for profile in taste_profile], "selectedValues": [] } for artist in artists: - if artist not in taste_profiles: + if artist not in taste_profile: raise Exception("The artist, {}, was not present in taste!".format(artist)) - formData["selectedValues"].append(taste_profiles[artist]["selectionValue"]) + formData["selectedValues"].append(taste_profile[artist]["selectionValue"]) body = {'browseId': "FEmusic_home", "formData": formData} self._send_request('browse', body)