Fixed issue #4: Added increasing delay on consecutive rate limit hits

This commit is contained in:
DraftKinner 2025-02-10 16:42:02 -05:00
parent 645be0def3
commit 3b2dfe8c2f
2 changed files with 24 additions and 12 deletions

View file

@ -37,8 +37,6 @@ API_URL = "https://api.sp" + "otify.com/v1/"
AUTH_URL = "https://accounts.sp" + "otify.com/"
REDIRECT_URI = "http://127.0.0.1:4381/login"
CLIENT_ID = "65b70807" + "3fc0480e" + "a92a0772" + "33ca87bd"
RATE_LIMIT_INTERVAL_SECS = 30
RATE_LIMIT_CALLS_PER_INTERVAL = 9
SCOPES = [
"app-remote-control",
"playlist-modify",
@ -68,6 +66,9 @@ SCOPES = [
"user-top-read",
]
RATE_LIMIT_INTERVAL_SECS = 30
RATE_LIMIT_CALLS_PER_INTERVAL = 9
class Session(LibrespotSession):
def __init__(

View file

@ -1,8 +1,9 @@
from argparse import Namespace
from pathlib import Path
from typing import Any
from time import sleep
from zotify import OAuth, Session
from zotify import OAuth, Session, RATE_LIMIT_INTERVAL_SECS
from zotify.collections import Album, Artist, Collection, Episode, Playlist, Show, Track
from zotify.config import Config
from zotify.file import TranscodingError
@ -262,6 +263,7 @@ class App:
def download_all(self, collections: list[Collection]) -> None:
count = 0
total = sum(len(c.playables) for c in collections)
rate_limit_hits = 0
for collection in collections:
for playable in collection.playables:
count += 1
@ -282,17 +284,21 @@ class App:
# Get track data
if playable.type == PlayableType.TRACK:
with Loader("Fetching track..."):
try:
try:
with Loader("Fetching track..."):
track = self.__session.get_track(
playable.id, self.__config.download_quality
)
except RuntimeError as err:
Logger.log(
LogChannel.SKIPS,
f"Skipping track id = {playable.id}: {err}",
)
continue
except RuntimeError as err:
Logger.log(LogChannel.SKIPS, f"Skipping track #{count}: {err}")
if "audio key" in str(err).lower():
rate_limit_hits += 1
sleep_time = RATE_LIMIT_INTERVAL_SECS * rate_limit_hits
with Loader(
f"Rate limit hit! Sleeping for {sleep_time}s..."
):
sleep(sleep_time)
continue
elif playable.type == PlayableType.EPISODE:
with Loader("Fetching episode..."):
track = self.__session.get_episode(playable.id)
@ -341,7 +347,9 @@ class App:
track.lyrics().save(output)
except FileNotFoundError as e:
Logger.log(LogChannel.SKIPS, str(e))
Logger.log(LogChannel.DOWNLOADS, f"\nDownloaded {track.name}")
Logger.log(
LogChannel.DOWNLOADS, f"\nDownloaded {track.name} ({count}/{total})"
)
# Transcode audio
if (
@ -367,3 +375,6 @@ class App:
file.write_cover_art(
track.get_cover_art(self.__config.artwork_size)
)
# Reset rate limit counter for every successful download
rate_limit_hits = 0