rootshellz / Beatporter

Scrape Beatport charts and add songs to Spotify playlists
MIT License
35 stars 6 forks source link

Getting a 404 Error from Spotify on certain tracks. #15

Open mclopes opened 2 years ago

mclopes commented 2 years ago

Hi,

I've been running this script for about a year but recently I started getting an error. I am running on terminal on OSX.

It seems some tracks are returning a 404 error. Its unfortunately happening with the first track on the all genres playlist but if i remove that playlist and go straight to Afro House it finds some tracks and works as normal until one returns a 404.

Is there any way to skip a track if it returns a 404?

Any help that you can provide will be greatly appreciated.

[+] Searching for track: World Hold On feat. Steve Edwards (Fisher Rework, Extended Mix) by Bob Sinclar on World Hold On (feat. Steve Edwards) [Fisher Rework, Extended Mix]
    [+] Search Query: World Hold On feat. Steve Edwards Fisher Rework, Extended Mix Bob Sinclar World Hold On (feat. Steve Edwards) [Fisher Rework, Extended Mix]
**HTTP Error for GET to https://api.spotify.com/v1/search returned 404 due to Not found.**
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 943, 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/search?q=World+Hold+On+feat.+Steve+Edwards+Fisher+Rework%2C+Extended+Mix+Bob+Sinclar+World+Hold+On+%28feat.+Steve+Edwards%29+%5BFisher+Rework%2C+Extended+Mix%5D&limit=10&offset=0&type=track

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/marcelo/beatporter.py", line 36, in <module>
    spotify.add_new_tracks_to_playlist(genre, top_100_charts[genre])
  File "/Users/marcelo/spotify.py", line 247, in add_new_tracks_to_playlist
    track_id = search_for_track(track)
  File "/Users/marcelo/spotify.py", line 135, in search_for_track
    search_results = spotify.search(query)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/spotipy/client.py", line 547, in search
    return self._get(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/search?q=World+Hold+On+feat.+Steve+Edwards+Fisher+Rework%2C+Extended+Mix+Bob+Sinclar+World+Hold+On+%28feat.+Steve+Edwards%29+%5BFisher+Rework%2C+Extended+Mix%5D&limit=10&offset=0&type=track:
 Not found., reason: None
`
mclopes commented 2 years ago

I've gotten some success by moving the search with release but then in other songs it gets triggered by a 404.

I think the best solution would be an if statement to handle 404s. i tried coding one myself but didnt work : (

mrlopezco commented 2 years ago

Same here, let's hope someone smart can fix this :(

benjaminlewandowski commented 2 years ago

found the problem in the spotipy repo - my quickfix would be to limit the query to 99 chars via spotify.search(query[:99]) instead of spotify.search(query)

additionally i trimmed the title, name, mix, artists, remixers, and release info to 25 and removed all brackets from the string like this: "title": track["title"].replace("[", " ").replace("]", " ").replace("(", " ").replace(")", " ").strip()[:25]

it works now on my machine but its really not an ideal solution

mclopes commented 2 years ago

Thank you so much @benjaminlewandowski ! Limiting it to 99 worked.

The trimming part is still a little over my head but I'm going to keep trying.