Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JackDyre opened this issue Apr 30, 2024 · 3 comments
Labels

Comments

@JackDyre
Copy link

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
Copy link
Member

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: #913 (comment)

@JackDyre JackDyre changed the title program just hands indefinitely with no error message, am i being rate limited? program just hangs indefinitely with no error message, am i being rate limited? Apr 30, 2024
@JackDyre
Copy link
Author

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
Copy link
Member

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

@JackDyre JackDyre closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants