From 4594a748e33947e8bc248841bcac3158dd773539 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 12 Dec 2022 15:02:10 +0100 Subject: [PATCH] improve test coverage --- tests/test.cfg.example | 2 + tests/test.py | 30 ++++--- ytmusicapi/parsers/playlists.py | 134 ++++++++++++++++---------------- 3 files changed, 86 insertions(+), 80 deletions(-) diff --git a/tests/test.cfg.example b/tests/test.cfg.example index afff702..d2dd8e9 100644 --- a/tests/test.cfg.example +++ b/tests/test.cfg.example @@ -1,6 +1,8 @@ [auth] brand_account = 101234229123420379537 +brand_account_empty = 1123456629123420379537 headers = headers_auth_json_as_string +headers_empty = headers_account_with_empty_library_as_json_as_string headers_file = ./headers_auth.json headers_raw = raw_headers_pasted_from_browser diff --git a/tests/test.py b/tests/test.py index 3975580..25b6398 100644 --- a/tests/test.py +++ b/tests/test.py @@ -20,6 +20,8 @@ class TestYTMusic(unittest.TestCase): cls.yt = YTMusic(requests_session=False) cls.yt_auth = YTMusic(config['auth']['headers_file']) cls.yt_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account']) + cls.yt_empty = YTMusic(config['auth']['headers_empty'], + config['auth']['brand_account_empty']) def test_init(self): self.assertRaises(Exception, YTMusic, "{}") @@ -232,6 +234,9 @@ class TestYTMusic(unittest.TestCase): playlists = self.yt_auth.get_library_playlists(None) self.assertGreaterEqual(len(playlists), config.getint('limits', 'library_playlists')) + playlists = self.yt_empty.get_library_playlists() + self.assertEqual(len(playlists), 0) + def test_get_library_songs(self): self.assertRaises(Exception, self.yt_auth.get_library_songs, None, True) songs = self.yt_auth.get_library_songs(100) @@ -240,6 +245,8 @@ class TestYTMusic(unittest.TestCase): self.assertGreaterEqual(len(songs), config.getint('limits', 'library_songs')) songs = self.yt_auth.get_library_songs(order='a_to_z') self.assertGreaterEqual(len(songs), 25) + songs = self.yt_empty.get_library_songs() + self.assertEqual(len(songs), 0) def test_get_library_albums(self): albums = self.yt_auth.get_library_albums(100) @@ -252,6 +259,8 @@ class TestYTMusic(unittest.TestCase): self.assertGreater(len(albums), 50) albums = self.yt_brand.get_library_albums(100, order='recently_added') self.assertGreater(len(albums), 50) + albums = self.yt_empty.get_library_albums() + self.assertEqual(len(albums), 0) def test_get_library_artists(self): artists = self.yt_auth.get_library_artists(50) @@ -260,6 +269,8 @@ class TestYTMusic(unittest.TestCase): self.assertGreater(len(artists), 40) artists = self.yt_brand.get_library_artists(limit=None) self.assertGreater(len(artists), config.getint('limits', 'library_artists')) + artists = self.yt_empty.get_library_artists() + self.assertEqual(len(artists), 0) def test_get_library_subscriptions(self): artists = self.yt_brand.get_library_subscriptions(50) @@ -268,10 +279,14 @@ class TestYTMusic(unittest.TestCase): self.assertGreater(len(artists), 20) artists = self.yt_brand.get_library_subscriptions(limit=None) self.assertGreater(len(artists), config.getint('limits', 'library_subscriptions')) + artists = self.yt_empty.get_library_subscriptions() + self.assertEqual(len(artists), 0) def test_get_liked_songs(self): songs = self.yt_brand.get_liked_songs(200) self.assertGreater(len(songs['tracks']), 100) + songs = self.yt_empty.get_liked_songs() + self.assertEqual(songs['trackCount'], 0) def test_get_history(self): songs = self.yt_auth.get_history() @@ -383,12 +398,7 @@ class TestYTMusic(unittest.TestCase): results = self.yt_auth.get_library_upload_songs(50, order='z_to_a') self.assertGreater(len(results), 25) - # songs = self.yt_auth.get_library_upload_songs(None) - # self.assertEqual(len(songs), 1000) - - @unittest.skip("Must not have any uploaded songs to pass") - def test_get_library_upload_songs_empty(self): - results = self.yt_auth.get_library_upload_songs(100) + results = self.yt_empty.get_library_upload_songs(100) self.assertEqual(len(results), 0) def test_get_library_upload_albums(self): @@ -398,9 +408,7 @@ class TestYTMusic(unittest.TestCase): albums = self.yt_auth.get_library_upload_albums(None) self.assertGreaterEqual(len(albums), config.getint('limits', 'library_upload_albums')) - @unittest.skip("Must not have any uploaded albums to pass") - def test_get_library_upload_albums_empty(self): - results = self.yt_auth.get_library_upload_albums(100) + results = self.yt_empty.get_library_upload_albums(100) self.assertEqual(len(results), 0) def test_get_library_upload_artists(self): @@ -410,9 +418,7 @@ class TestYTMusic(unittest.TestCase): results = self.yt_auth.get_library_upload_artists(50, order='recently_added') self.assertGreaterEqual(len(results), 25) - @unittest.skip("Must not have any uploaded artsts to pass") - def test_get_library_upload_artists_empty(self): - results = self.yt_auth.get_library_upload_artists(100) + results = self.yt_empty.get_library_upload_artists(100) self.assertEqual(len(results), 0) def test_upload_song(self): diff --git a/ytmusicapi/parsers/playlists.py b/ytmusicapi/parsers/playlists.py index fd7b648..3552bb8 100644 --- a/ytmusicapi/parsers/playlists.py +++ b/ytmusicapi/parsers/playlists.py @@ -12,89 +12,87 @@ def parse_playlist_items(results, menu_entries: List[List] = None): continue data = result[MRLIR] - try: - videoId = setVideoId = None - like = None - feedback_tokens = None + videoId = setVideoId = None + like = None + feedback_tokens = None - # if the item has a menu, find its setVideoId - if 'menu' in data: - for item in nav(data, MENU_ITEMS): - if 'menuServiceItemRenderer' in item: - menu_service = nav(item, MENU_SERVICE) - if 'playlistEditEndpoint' in menu_service: - setVideoId = menu_service['playlistEditEndpoint']['actions'][0][ - 'setVideoId'] - videoId = menu_service['playlistEditEndpoint']['actions'][0][ - 'removedVideoId'] + # if the item has a menu, find its setVideoId + if 'menu' in data: + for item in nav(data, MENU_ITEMS): + if 'menuServiceItemRenderer' in item: + menu_service = nav(item, MENU_SERVICE) + if 'playlistEditEndpoint' in menu_service: + setVideoId = menu_service['playlistEditEndpoint']['actions'][0][ + 'setVideoId'] + videoId = menu_service['playlistEditEndpoint']['actions'][0][ + 'removedVideoId'] - if TOGGLE_MENU in item: - feedback_tokens = parse_song_menu_tokens(item) + if TOGGLE_MENU in item: + feedback_tokens = parse_song_menu_tokens(item) - # if item is not playable, the videoId was retrieved above - if nav(data, PLAY_BUTTON, none_if_absent=True) != None: - if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON): - videoId = nav(data, - PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId'] + # if item is not playable, the videoId was retrieved above + if nav(data, PLAY_BUTTON, none_if_absent=True) is not None: + if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON): + videoId = nav(data, + PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId'] - if 'menu' in data: - like = nav(data, MENU_LIKE_STATUS, True) + if 'menu' in data: + like = nav(data, MENU_LIKE_STATUS, True) - title = get_item_text(data, 0) - if title == 'Song deleted': - continue + title = get_item_text(data, 0) + if title == 'Song deleted': + continue - artists = parse_song_artists(data, 1) + artists = parse_song_artists(data, 1) - album = parse_song_album(data, 2) + album = parse_song_album(data, 2) - duration = None - if 'fixedColumns' in data: - if 'simpleText' in get_fixed_column_item(data, 0)['text']: - duration = get_fixed_column_item(data, 0)['text']['simpleText'] - else: - duration = get_fixed_column_item(data, 0)['text']['runs'][0]['text'] + duration = None + if 'fixedColumns' in data: + if 'simpleText' in get_fixed_column_item(data, 0)['text']: + duration = get_fixed_column_item(data, 0)['text']['simpleText'] + else: + duration = get_fixed_column_item(data, 0)['text']['runs'][0]['text'] - thumbnails = None - if 'thumbnail' in data: - thumbnails = nav(data, THUMBNAILS) + thumbnails = None + if 'thumbnail' in data: + thumbnails = nav(data, THUMBNAILS) - isAvailable = True - if 'musicItemRendererDisplayPolicy' in data: - isAvailable = data[ - 'musicItemRendererDisplayPolicy'] != 'MUSIC_ITEM_RENDERER_DISPLAY_POLICY_GREY_OUT' + isAvailable = True + if 'musicItemRendererDisplayPolicy' in data: + isAvailable = data[ + 'musicItemRendererDisplayPolicy'] != 'MUSIC_ITEM_RENDERER_DISPLAY_POLICY_GREY_OUT' - isExplicit = nav(data, BADGE_LABEL, True) is not None + isExplicit = nav(data, BADGE_LABEL, True) is not None - videoType = nav(data, MENU_ITEMS + [0, 'menuNavigationItemRenderer', 'navigationEndpoint'] + NAVIGATION_VIDEO_TYPE, True) + videoType = nav( + data, MENU_ITEMS + [0, 'menuNavigationItemRenderer', 'navigationEndpoint'] + + NAVIGATION_VIDEO_TYPE, True) - song = { - 'videoId': videoId, - 'title': title, - 'artists': artists, - 'album': album, - 'likeStatus': like, - 'thumbnails': thumbnails, - 'isAvailable': isAvailable, - 'isExplicit': isExplicit, - 'videoType': videoType - } - if duration: - song['duration'] = duration - song['duration_seconds'] = parse_duration(duration) - if setVideoId: - song['setVideoId'] = setVideoId - if feedback_tokens: - song['feedbackTokens'] = feedback_tokens + song = { + 'videoId': videoId, + 'title': title, + 'artists': artists, + 'album': album, + 'likeStatus': like, + 'thumbnails': thumbnails, + 'isAvailable': isAvailable, + 'isExplicit': isExplicit, + 'videoType': videoType + } + if duration: + song['duration'] = duration + song['duration_seconds'] = parse_duration(duration) + if setVideoId: + song['setVideoId'] = setVideoId + if feedback_tokens: + song['feedbackTokens'] = feedback_tokens - if menu_entries: - for menu_entry in menu_entries: - song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry) + if menu_entries: + for menu_entry in menu_entries: + song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry) - songs.append(song) - - except Exception as e: - print("Item " + str(count) + ": " + str(e)) + songs.append(song) return songs