spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

program just hangs indefinitely with no error message, am i being rate limited? #1094

Closed JackDyre closed 6 months ago

JackDyre commented 6 months ago

I have been making a lot of requests this evening working on a project. As far as I know I did not change anything in my main file that I was running by accident, and I know for a fact that I did not change anything in my SpotifyTracksFromPlaylist .py file because it was not open.

The code was working, and then randomly it started hanging whenever it got to the call of get_tracks() without any error message. Am I being rate limited? What is happening?

main.py

from internal_modules.SpotifyTracksFromPlaylist import get_tracks
from internal_modules.SpotipyAPIClient import initialize_api

sp = initialize_api(scope='playlist-read-private playlist-modify-public playlist-modify-private')
user_id = input('Input User ID: ')

ref_playlist = 'https://open.spotify.com/playlist/5qxu9APTbnKyWzhJjuMzBS?si=15e677b542c846f5'
ref_tracks, q , w = get_tracks(ref_playlist)
ref_ids = []
for track in ref_tracks:
    ref_ids.append(track['id'])

SpotifyTracksFromPlaylist .py

def get_tracks(url: str | None=None, market: str = 'US') -> tuple[list, int, bool]:

    playlist_url: str = url or input_url()
    is_error: bool = False
    tracks: list = []; offset = 0

    try:

        width: int = 275
        height: int = 100

        root = tk.Tk()
        root.title('Fetching Tracks')

        left = (root.winfo_screenwidth() - width)//2
        top = (root.winfo_screenheight() - height)//2
        root.geometry(f'{width}x{height}+{left}+{top}')

        frame: tk.Frame = tk.Frame(root)
        frame.pack(expand=True)
        root.after(ms=1, func=root.focus_force)

        progress_var = tk.StringVar(frame, name='Progress')
        label = tk.Label(frame, textvariable=progress_var)
        label.pack(pady=5)

        total_tracks: int = sp.playlist(playlist_url)["tracks"]["total"]

        while True:
            track_batch = sp.playlist_tracks(playlist_url, offset=offset, market=market)
            offset += 100
            for track in track_batch["items"]:
                tracks.append(track["track"])

            progress = 100 * len(tracks) / (total_tracks or 1)
            progress_var.set(f"Fetching Tracks: {progress:.2f}%")
            root.update()
            if not track_batch["next"]:
                root.destroy()
                break

    except Exception as e:
        total_tracks = 0
        print(f'An API error was encountered. Please try again\n{e}')
        is_error = True
        root.destroy()

    return (tracks, total_tracks, is_error)
dieser-niko commented 6 months ago

Yes. The library is basically waiting until the ratelimit is gone. Here's a possible solution if you want the script to raise an exception instead: https://github.com/spotipy-dev/spotipy/issues/913#issuecomment-1899143238

JackDyre commented 6 months ago

How long does the ratelimit usually last?

What is the ratelimit? If I knew the limit of how many requests I can make in 30 secs I could write some logic to make sure I stay under it.

dieser-niko commented 6 months ago

Not exactly, but you can read more about it here: https://developer.spotify.com/documentation/web-api/concepts/rate-limits