Cornelius-Figgle
Cornelius-Figgle

Reputation: 172

Spotipy throws 404 when retrieving song data

EDIT 2: As @ximzend said, spotipy has moved to playlist_items instead of playlist_tracks

EDIT 1: In the process of moving to a curses interface, I changed the progress bar to a tqdm one (rather than rich), and that seemed to solve my problem. I did also upgrade spotipy (after it started working) but no change was needed in the API code ¯( ツ )/¯ so idk if rich was breaking the API req somehow or whatever

Thought I would put this here to help anyone in the future ツ



Spotipy throws this error when trying to retrieve data about tracks in a playlist

requests.exceptions.HTTPError: 404 Client Error: Not Found for url:

The error is thrown on the line below, specifically the self.sp.playlist_tracks(playlist_uri)['items'] part (The progress.track is a progress bar for the loop from rich)

for song in progress.track(self.sp.playlist_tracks(playlist_uri)['items'], description='Listing songs...'):

Full traceback is:

HTTP Error for GET to https://api.spotify.com/v1/playlists/4wi8hpuh4oyjtivbzk3k9n/tracks with Params: {'limit': 100, 'offset': 0, 'fields': None, 'market': None, 'additional_types': 'track'} returned 404 due to Not found.
Traceback (most recent call last):
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\spotipy\client.py", line 245, in _internal_call
    response.raise_for_status()
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/playlists/4wi8hpuh4oyjtivbzk3k9n/tracks?limit=100&offset=0&additional_types=track

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t:\projects\youtusic\main.py", line 132, in <module>
    main()
  File "t:\projects\youtusic\main.py", line 119, in main
    track_list = obj.sp_get_tracks(playlist_uri)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "t:\projects\youtusic\youtusic.py", line 104, in sp_get_tracks
    for song in progress.track(self.sp.playlist_tracks(playlist_uri)['items'], description='Listing songs...'):
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\spotipy\client.py", line 657, in playlist_tracks
    return self.playlist_items(playlist_id, fields, limit, offset,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\spotipy\client.py", line 681, in playlist_items
    return self._get(
           ^^^^^^^^^^
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "T:\projects\youtusic\youtusic-env\Lib\site-packages\spotipy\client.py", line 267, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/playlists/4wi8hpuh4oyjtivbzk3k9n/tracks?limit=100&offset=0&additional_types=track:
 Not found., reason: None

The code I was using was as follows:

def sp_get_tracks(self, playlist_link: str) -> list:
        '''
        Uses [spotipy](https://pypi.org/project/spotipy/) to retrieve
        a list of songs in the playlist provided. Returns a list to be
        used with `grab_yt_links`
        '''

        playlist_uri = playlist_link.split('/')[-1].split('?')[0]
        song_titles = []

        for song in progress.track(self.sp.playlist_tracks(playlist_uri)['items'], description='Listing songs...'):

            track_name: str = song['track']['name']
            artist_name: str = song['track']['artists'][0]['name']

            track_name = track_name.replace(' ', '+')
            artist_name = artist_name.replace(' ', '+')
            
            song_titles.append(f'{artist_name}+{track_name}')

        return song_titles

It should returns a list of all the songs (and artists) in the playlist. (The progress.track is a progress bar for the loop from rich). The 404 confuses me because my network connection is fine and I am able to perform Google searches etc

Additionally, the playlist only has 19 songs in so should be resticted by the limits I have read elsewhere

Upvotes: 0

Views: 714

Answers (1)

Ximzend
Ximzend

Reputation: 448

You get this error, because Spotify made a code breaking change to its API. SpotiPy has fixed this issue by releasing an upgrade.
To install this upgrade, open a command prompt and run pip install spotipy --upgrade.
After the upgrade, you need to use playlist_items(playlist_id, fields=None, limit=100, offset=0, market=None, additional_types=('track', 'episode')). playlist_tracks is deprecated.

Upvotes: 1

Related Questions