get_charts: fix index errors on authorized song chart missing

This commit is contained in:
jcbirdwell 2023-12-16 15:58:00 -06:00
commit b2c078df63
2 changed files with 8 additions and 3 deletions

View file

@ -284,7 +284,8 @@ class TestYTMusic(unittest.TestCase):
def test_get_charts(self): def test_get_charts(self):
charts = self.yt_oauth.get_charts() charts = self.yt_oauth.get_charts()
self.assertEqual(len(charts), 4) # songs section appears to be removed currently (US)
self.assertGreaterEqual(len(charts), 3)
charts = self.yt.get_charts(country="US") charts = self.yt.get_charts(country="US")
self.assertEqual(len(charts), 5) self.assertEqual(len(charts), 5)
charts = self.yt.get_charts(country="BE") charts = self.yt.get_charts(country="BE")

View file

@ -95,7 +95,7 @@ class ExploreMixin:
Global charts have no Trending section, US charts have an extra Genres section with some Genre charts. Global charts have no Trending section, US charts have an extra Genres section with some Genre charts.
:param country: ISO 3166-1 Alpha-2 country code. Default: ZZ = Global :param country: ISO 3166-1 Alpha-2 country code. Default: ZZ = Global
:return: Dictionary containing chart songs (only if authenticated), chart videos, chart artists and :return: Dictionary containing chart songs (only if authenticated and available), chart videos, chart artists and
trending videos. trending videos.
Example:: Example::
@ -209,9 +209,13 @@ class ExploreMixin:
])) ]))
charts_categories = ['videos', 'artists'] charts_categories = ['videos', 'artists']
has_songs = bool(self.auth)
has_genres = country == 'US' has_genres = country == 'US'
has_trending = country != 'ZZ' has_trending = country != 'ZZ'
# songs section appears to no longer exist, extra length check avoids
# index errors and will still include songs if the feature is added back
has_songs = bool(
self.auth) and len(results) - 1 > (len(charts_categories) + has_genres + has_trending)
if has_songs: if has_songs:
charts_categories.insert(0, 'songs') charts_categories.insert(0, 'songs')
if has_genres: if has_genres: