Covered more cases in unittest and documentation

This commit is contained in:
Adrian 2022-08-23 20:33:48 -07:00
commit f7ef0d2c15
4 changed files with 43 additions and 29 deletions

View file

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

View file

@ -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)
################

View file

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

View file

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