Fix LastFM rate limiting dropping scrobbles (f91fdd53 follow-up)

This commit is contained in:
2026-08-08 00:40:29 -04:00
parent f8094fb1b0
commit d5a753146f
3 changed files with 66 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import calendar
import logging
import time
from datetime import datetime, timedelta
import pylast
@ -138,30 +139,38 @@ class LastFM:
for scrobble in found_scrobbles:
logger.info(f"Processing {scrobble}")
log_dict = {"scrobble": scrobble}
# Artist/title/album come from the recent-tracks response itself,
# no extra API call required.
artist = scrobble.track.artist.name if scrobble.track.artist else None
log_dict["artist"] = artist
# Duration and MBID require a per-track `getInfo` API call, which
# quickly exhausts Last.fm's rate limit for large historical
# imports. Treat them as best-effort so one failure doesn't drop
# the whole scrobble.
run_time = None
mbid = None
artist = None
log_dict = {"scrobble": scrobble}
try:
run_time = int(scrobble.track.get_duration() / 1000)
mbid = scrobble.track.get_mbid()
artist = scrobble.track.get_artist().name
log_dict["artist"] = artist
log_dict["mbid"] = mbid
log_dict["run_time"] = run_time
except pylast.MalformedResponseError as e:
logger.warning(e)
except pylast.WSError as e:
logger.info(
"LastFM barfed trying to get the track for {scrobble.track}",
extra=log_dict,
f"LastFM barfed trying to enrich {scrobble.track}", extra=log_dict
)
except pylast.NetworkError as e:
logger.info(
"LastFM barfed trying to get the track for {scrobble.track}",
extra=log_dict,
f"LastFM barfed trying to enrich {scrobble.track}", extra=log_dict
)
finally:
# Last.fm rate limit is ~5 req/sec; keep per-track enrichment
# calls spaced out so large imports don't trip it.
time.sleep(0.2)
if not artist:
logger.info(