sigma67 / ytmusicapi

Unofficial API for YouTube Music
https://ytmusicapi.readthedocs.io
MIT License
1.77k stars 209 forks source link

KeyError: 'watchEndpoint' When Retrieving Artist Discography #423

Closed skandallouz closed 1 year ago

skandallouz commented 1 year ago

Describe the bug
While attempting to retrieve an artist's discography using the get_ytmusic_discography function, a KeyError related to the 'watchEndpoint' is encountered. This issue seems to occur specifically for artists with very large discographies, while it works normally for artists with small to medium-sized libraries. This prevents the successful retrieval of the artist information for those with extensive discographies.

To Reproduce
Steps to reproduce the behavior:

  1. Initialize ytmusicapi with appropriate headers and setup.
  2. Call the get_ytmusic_discography function with valid ytmusic_artist_ids.
  3. Observe the KeyError related to 'watchEndpoint'.

Additional context
The error seems to be within the ytmusicapi library when trying to navigate through the response data. Here's a sample code and the error traceback:

(this is the code and error I'm getting)

def get_ytmusic_discography(ytmusic_artist_ids):
    discography = {}

    for ytmusic_artist_id in ytmusic_artist_ids:
        artist = ytmusic.get_artist(ytmusic_artist_id)
        print("Debugging Artist Info:", artist)
        artist_name = artist["name"]

        if 'albums' in artist:
            browseId = artist['albums']['browseId']
            params = artist['albums'].get('params', None)
            if params:
                album_data = ytmusic.get_artist_albums(browseId, params)
            else:
                album_data = artist['albums']['results']

            for album in album_data:
                discography[album['title']] = {
                    'url': 'https://music.youtube.com/' + album['browseId'],
                    'cover_art': album['thumbnails'][-1]['url'] if album['thumbnails'] else None
                }

        if 'singles' in artist:
            browseId = artist['singles']['browseId']
            params = artist['singles'].get('params', None)
            if params:
                single_data = ytmusic.get_artist_albums(browseId, params)
            else:
                single_data = artist['singles']['results']

            for single in single_data:
                discography[single['title']] = {
                    'url': 'https://music.youtube.com/' + single['browseId'],
                    'cover_art': single['thumbnails'][-1]['url'] if single['thumbnails'] else None
                }

    return discography, artist_name

# Error traceback:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/ytmusicapi/navigation.py", line 75, in nav
    root = root[k]
    ^^^^^^^
KeyError: 'watchEndpoint'

The code is attempting to retrieve discography information for specific artist IDs, but it encounters an issue within the ytmusicapi library. The error seems related to the internal parsing of the response data.

sigma67 commented 1 year ago

What is the value of ytmusic_artist_ids?

skandallouz commented 1 year ago

Thank you for the quick response! ytmusic_artist_ids = ['UC13ToEQgfmTe8_GW19LYtCg'] This ID corresponds to Udit Narayan, and I've encountered the KeyError related to the 'watchEndpoint' when working with his extensive discography as well as similar artists with large catalogs.

sigma67 commented 1 year ago

I ran your code with the exact same setup and it worked fine. It must be an issue with your setup or a regional issue, which I can't help with.