Addded favorite artists to taste

This commit is contained in:
Adrian 2022-08-20 23:43:39 -07:00
commit d66f20aa13
No known key found for this signature in database
GPG key ID: 8DA9BC885CE952A4
4 changed files with 74 additions and 15 deletions

View file

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

View file

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

View file

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

View file

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