[GH-ISSUE #1094] program just hangs indefinitely with no error message, am i being rate limited? #651

Closed
opened 2026-02-28 00:00:32 +03:00 by kerem · 3 comments
Owner

Originally created by @JackDyre on GitHub (Apr 30, 2024).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1094

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)
Originally created by @JackDyre on GitHub (Apr 30, 2024). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1094 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 ```python 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 ```python 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) ```
kerem 2026-02-28 00:00:32 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@dieser-niko commented on GitHub (Apr 30, 2024):

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

<!-- gh-comment-id:2085476478 --> @dieser-niko commented on GitHub (Apr 30, 2024): 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
Author
Owner

@JackDyre commented on GitHub (Apr 30, 2024):

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.

<!-- gh-comment-id:2085675703 --> @JackDyre commented on GitHub (Apr 30, 2024): 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.
Author
Owner

@dieser-niko commented on GitHub (Apr 30, 2024):

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

<!-- gh-comment-id:2085946036 --> @dieser-niko commented on GitHub (Apr 30, 2024): Not exactly, but you can read more about it here: https://developer.spotify.com/documentation/web-api/concepts/rate-limits
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#651
No description provided.