Skip to content

Commit

Permalink
search: add podcasts filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Nov 11, 2023
1 parent 6f80e94 commit f73f77b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 69 deletions.
6 changes: 6 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def test_search_filters(self):
results = self.yt_auth.search("some user", filter="profiles")
self.assertGreater(len(results), 10)
self.assertTrue(all(item['resultType'] == 'profile' for item in results))
results = self.yt_auth.search(query, filter="podcasts")
self.assertGreater(len(results), 10)
self.assertTrue(all(item['resultType'] == 'podcast' for item in results))
results = self.yt_auth.search(query, filter="episodes")
self.assertGreater(len(results), 10)
self.assertTrue(all(item['resultType'] == 'episode' for item in results))

def test_search_uploads(self):
self.assertRaises(
Expand Down
138 changes: 69 additions & 69 deletions ytmusicapi/mixins/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,77 +109,77 @@ def get_playlist(self,
response = self._send_request(endpoint, body)
results = nav(response,
SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + ['musicPlaylistShelfRenderer'])
playlist = {'id': results['playlistId']}
own_playlist = 'musicEditablePlaylistDetailHeaderRenderer' in response['header']
if not own_playlist:
header = response['header']['musicDetailHeaderRenderer']
playlist['privacy'] = 'PUBLIC'
else:
header = response['header']['musicEditablePlaylistDetailHeaderRenderer']
playlist['privacy'] = header['editHeader']['musicPlaylistEditHeaderRenderer'][
'privacy']
header = header['header']['musicDetailHeaderRenderer']

playlist['title'] = nav(header, TITLE_TEXT)
playlist['thumbnails'] = nav(header, THUMBNAIL_CROPPED)
playlist["description"] = nav(header, DESCRIPTION, True)
run_count = len(nav(header, SUBTITLE_RUNS))
if run_count > 1:
playlist['author'] = {
'name': nav(header, SUBTITLE2),
'id': nav(header, SUBTITLE_RUNS + [2] + NAVIGATION_BROWSE_ID, True)
}
if run_count == 5:
playlist['year'] = nav(header, SUBTITLE3)

playlist['views'] = None
playlist['duration'] = None
if 'runs' in header['secondSubtitle']:
second_subtitle_runs = header['secondSubtitle']['runs']
has_views = (len(second_subtitle_runs) > 3) * 2
playlist['views'] = None if not has_views else to_int(second_subtitle_runs[0]['text'])
has_duration = (len(second_subtitle_runs) > 1) * 2
playlist['duration'] = None if not has_duration else second_subtitle_runs[
has_views + has_duration]['text']
song_count = second_subtitle_runs[has_views + 0]['text'].split(" ")
song_count = to_int(song_count[0]) if len(song_count) > 1 else 0
else:
song_count = len(results['contents'])

playlist['trackCount'] = song_count

playlist = {}
# own_playlist = 'musicEditablePlaylistDetailHeaderRenderer' in response['header']
# if not own_playlist:
# header = response['header']['musicDetailHeaderRenderer']
# playlist['privacy'] = 'PUBLIC'
# else:
# header = response['header']['musicEditablePlaylistDetailHeaderRenderer']
# playlist['privacy'] = header['editHeader']['musicPlaylistEditHeaderRenderer'][
# 'privacy']
# header = header['header']['musicDetailHeaderRenderer']
#
# playlist['title'] = nav(header, TITLE_TEXT)
# playlist['thumbnails'] = nav(header, THUMBNAIL_CROPPED)
# playlist["description"] = nav(header, DESCRIPTION, True)
# run_count = len(nav(header, SUBTITLE_RUNS))
# if run_count > 1:
# playlist['author'] = {
# 'name': nav(header, SUBTITLE2),
# 'id': nav(header, SUBTITLE_RUNS + [2] + NAVIGATION_BROWSE_ID, True)
# }
# if run_count == 5:
# playlist['year'] = nav(header, SUBTITLE3)
#
# playlist['views'] = None
# playlist['duration'] = None
# if 'runs' in header['secondSubtitle']:
# second_subtitle_runs = header['secondSubtitle']['runs']
# has_views = (len(second_subtitle_runs) > 3) * 2
# playlist['views'] = None if not has_views else to_int(second_subtitle_runs[0]['text'])
# has_duration = (len(second_subtitle_runs) > 1) * 2
# playlist['duration'] = None if not has_duration else second_subtitle_runs[
# has_views + has_duration]['text']
# song_count = second_subtitle_runs[has_views + 0]['text'].split(" ")
# song_count = to_int(song_count[0]) if len(song_count) > 1 else 0
# else:
# song_count = len(results['contents'])
#
# playlist['trackCount'] = song_count
#
request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams
)

# suggestions and related are missing e.g. on liked songs
section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer'])
playlist['related'] = []
if 'continuations' in section_list:
additionalParams = get_continuation_params(section_list)
if own_playlist and (suggestions_limit > 0 or related):
parse_func = lambda results: parse_playlist_items(results)
suggested = request_func(additionalParams)
continuation = nav(suggested, SECTION_LIST_CONTINUATION)
additionalParams = get_continuation_params(continuation)
suggestions_shelf = nav(continuation, CONTENT + MUSIC_SHELF)
playlist['suggestions'] = get_continuation_contents(suggestions_shelf, parse_func)

parse_func = lambda results: parse_playlist_items(results)
playlist['suggestions'].extend(
get_continuations(suggestions_shelf,
'musicShelfContinuation',
suggestions_limit - len(playlist['suggestions']),
request_func,
parse_func,
reloadable=True))

if related:
response = request_func(additionalParams)
continuation = nav(response, SECTION_LIST_CONTINUATION, True)
if continuation:
parse_func = lambda results: parse_content_list(results, parse_playlist)
playlist['related'] = get_continuation_contents(
nav(continuation, CONTENT + CAROUSEL), parse_func)
#
# # suggestions and related are missing e.g. on liked songs
# section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer'])
# playlist['related'] = []
# if 'continuations' in section_list:
# additionalParams = get_continuation_params(section_list)
# if own_playlist and (suggestions_limit > 0 or related):
# parse_func = lambda results: parse_playlist_items(results)
# suggested = request_func(additionalParams)
# continuation = nav(suggested, SECTION_LIST_CONTINUATION)
# additionalParams = get_continuation_params(continuation)
# suggestions_shelf = nav(continuation, CONTENT + MUSIC_SHELF)
# playlist['suggestions'] = get_continuation_contents(suggestions_shelf, parse_func)
#
# parse_func = lambda results: parse_playlist_items(results)
# playlist['suggestions'].extend(
# get_continuations(suggestions_shelf,
# 'musicShelfContinuation',
# suggestions_limit - len(playlist['suggestions']),
# request_func,
# parse_func,
# reloadable=True))
#
# if related:
# response = request_func(additionalParams)
# continuation = nav(response, SECTION_LIST_CONTINUATION, True)
# if continuation:
# parse_func = lambda results: parse_content_list(results, parse_playlist)
# playlist['related'] = get_continuation_contents(
# nav(continuation, CONTENT + CAROUSEL), parse_func)

playlist['tracks'] = []
if 'contents' in results:
Expand Down

0 comments on commit f73f77b

Please sign in to comment.