Feature: add uploads filter to search()

This commit is contained in:
sigma67 2020-09-17 11:29:09 +02:00
commit 654551b0ee
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
4 changed files with 74 additions and 18 deletions

View file

@ -26,7 +26,7 @@ class TestYTMusic(unittest.TestCase):
def test_search(self): def test_search(self):
query = "Oasis Wonderwall" query = "Oasis Wonderwall"
self.assertRaises(Exception, youtube_auth.search, query, "song") self.assertRaises(Exception, youtube_auth.search, query, "song")
results = youtube_auth.search(query) results = youtube.search(query)
self.assertGreater(len(results), 0) self.assertGreater(len(results), 0)
results = youtube_auth.search(query, 'songs') results = youtube_auth.search(query, 'songs')
self.assertGreater(len(results), 0) self.assertGreater(len(results), 0)
@ -39,6 +39,10 @@ class TestYTMusic(unittest.TestCase):
results = youtube_auth.search(query, 'playlists') results = youtube_auth.search(query, 'playlists')
self.assertGreater(len(results), 0) self.assertGreater(len(results), 0)
def test_search_uploads(self):
results = youtube_auth.search('audiomachine', 'uploads')
self.assertGreater(len(results), 0)
def test_get_artist(self): def test_get_artist(self):
results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA") results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA")
self.assertEqual(len(results), 11) self.assertEqual(len(results), 11)

View file

@ -15,7 +15,7 @@ class BrowsingMixin:
:param query: Query string, i.e. 'Oasis Wonderwall' :param query: Query string, i.e. 'Oasis Wonderwall'
:param filter: Filter for item types. Allowed values: :param filter: Filter for item types. Allowed values:
'songs', 'videos', 'albums', 'artists', 'playlists'. 'songs', 'videos', 'albums', 'artists', 'playlists', 'uploads'.
Default: Default search, including all types of items. Default: Default search, including all types of items.
:return: List of results depending on filter. :return: List of results depending on filter.
resultType specifies the type of item (important for default search). resultType specifies the type of item (important for default search).
@ -47,7 +47,7 @@ class BrowsingMixin:
body = {'query': query} body = {'query': query}
endpoint = 'search' endpoint = 'search'
search_results = [] search_results = []
filters = ['albums', 'artists', 'playlists', 'songs', 'videos'] filters = ['albums', 'artists', 'playlists', 'songs', 'videos', 'uploads']
if filter and filter not in filters: if filter and filter not in filters:
raise Exception( raise Exception(
"Invalid filter provided. Please use one of the following filters or leave out the parameter: " "Invalid filter provided. Please use one of the following filters or leave out the parameter: "
@ -57,18 +57,25 @@ class BrowsingMixin:
param1 = 'Eg-KAQwIA' param1 = 'Eg-KAQwIA'
param3 = 'MABqChAEEAMQCRAFEAo%3D' param3 = 'MABqChAEEAMQCRAFEAo%3D'
if filter == 'videos': if filter == 'uploads':
param2 = 'BABGAAgACgA' params = 'agIYAw%3D%3D'
elif filter == 'albums':
param2 = 'BAAGAEgACgA'
elif filter == 'artists':
param2 = 'BAAGAAgASgA'
elif filter == 'playlists':
param2 = 'BAAGAAgACgB'
else: else:
param2 = 'RAAGAAgACgA' if filter == 'videos':
param2 = 'BABGAAgACgA'
elif filter == 'albums':
param2 = 'BAAGAEgACgA'
elif filter == 'artists':
param2 = 'BAAGAAgASgA'
elif filter == 'playlists':
param2 = 'BAAGAAgACgB'
elif filter == 'uploads':
self.__check_auth()
param2 = 'RABGAEgASgB'
else:
param2 = 'RAAGAAgACgA'
params = param1 + param2 + param3
body['params'] = param1 + param2 + param3 body['params'] = params
response = self._send_request(endpoint, body) response = self._send_request(endpoint, body)
@ -78,8 +85,8 @@ class BrowsingMixin:
return search_results return search_results
if 'tabbedSearchResultsRenderer' in response['contents']: if 'tabbedSearchResultsRenderer' in response['contents']:
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][0][ results = response['contents']['tabbedSearchResultsRenderer']['tabs'][int(
'tabRenderer']['content'] filter == "uploads")]['tabRenderer']['content']
else: else:
results = response['contents'] results = response['contents']
@ -528,8 +535,11 @@ class BrowsingMixin:
song_meta['provider'] = description[0].replace('Provided to YouTube by ', '') song_meta['provider'] = description[0].replace('Provided to YouTube by ', '')
song_meta['artists'] = [artist for artist in description[1].split(' · ')[1:]] song_meta['artists'] = [artist for artist in description[1].split(' · ')[1:]]
song_meta['copyright'] = description[3] song_meta['copyright'] = description[3]
song_meta['release'] = None if len(description) < 5 else description[4].replace('Released on: ', '') song_meta['release'] = None if len(description) < 5 else description[4].replace(
song_meta['production'] = None if len(description) < 6 else [pub for pub in description[5].split('\n')] 'Released on: ', '')
song_meta['production'] = None if len(description) < 6 else [
pub for pub in description[5].split('\n')
]
except (KeyError, IndexError): except (KeyError, IndexError):
pass pass
song_meta['streamingData'] = player_response['streamingData'] song_meta['streamingData'] = player_response['streamingData']

View file

@ -19,6 +19,7 @@ CAROUSEL_TITLE = ['header', 'musicCarouselShelfBasicHeaderRenderer', 'title', 'r
FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations'] FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations']
TITLE = ['title', 'runs', 0] TITLE = ['title', 'runs', 0]
TITLE_TEXT = ['title', 'runs', 0, 'text'] TITLE_TEXT = ['title', 'runs', 0, 'text']
TEXT_RUN = ['text', 'runs', 0]
SUBTITLE = ['subtitle', 'runs', 0, 'text'] SUBTITLE = ['subtitle', 'runs', 0, 'text']
SUBTITLE2 = ['subtitle', 'runs', 2, 'text'] SUBTITLE2 = ['subtitle', 'runs', 2, 'text']
SUBTITLE3 = ['subtitle', 'runs', 4, 'text'] SUBTITLE3 = ['subtitle', 'runs', 4, 'text']

View file

@ -21,6 +21,8 @@ class Parser:
else: else:
resultType = result_types[result_types_local.index(resultType)] resultType = result_types[result_types_local.index(resultType)]
search_result['resultType'] = resultType
if resultType in ['song', 'video']: if resultType in ['song', 'video']:
search_result['videoId'] = nav( search_result['videoId'] = nav(
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId'] data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
@ -55,8 +57,47 @@ class Parser:
search_result['views'] = get_item_text(data, 2 + default).split(' ')[0] search_result['views'] = get_item_text(data, 2 + default).split(' ')[0]
search_result['duration'] = get_item_text(data, 3 + default) search_result['duration'] = get_item_text(data, 3 + default)
elif resultType in ['upload']:
search_result['title'] = get_item_text(data, 0)
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
if not browse_id: # song result
flex_items = [
nav(get_flex_column_item(data, i), ['text', 'runs', 0], True) for i in range(3)
]
if flex_items[0]:
search_result['videoId'] = nav(flex_items[0], NAVIGATION_VIDEO_ID)
search_result['playlistId'] = nav(flex_items[0], NAVIGATION_PLAYLIST_ID)
if flex_items[1]:
search_result['artist'] = {
'name': flex_items[1]['text'],
'id': nav(flex_items[1], NAVIGATION_BROWSE_ID)
}
if flex_items[2]:
search_result['album'] = {
'name': flex_items[2]['text'],
'id': nav(flex_items[2], NAVIGATION_BROWSE_ID)
}
search_result['duration'] = data['fixedColumns'][0][
'musicResponsiveListItemFixedColumnRenderer']['text']['runs'][0]['text']
search_result['resultType'] = 'song'
else: # artist or album result
search_result['browseId'] = browse_id
if 'artist' in search_result['browseId']:
search_result['resultType'] = 'artist'
else:
flex_item2 = get_flex_column_item(data, 1)
runs = [
run['text'] for i, run in enumerate(flex_item2['text']['runs'])
if i % 2 == 0
]
search_result['artist'] = runs[1]
if len(runs) > 2: # date may be missing
search_result['releaseDate'] = runs[2]
search_result['resultType'] = 'album'
search_result['thumbnails'] = nav(data, THUMBNAILS) search_result['thumbnails'] = nav(data, THUMBNAILS)
search_result['resultType'] = resultType
return search_result return search_result