Fix historical LastFM import resume and dedup (f91fdd53)

This commit is contained in:
2026-08-07 23:56:37 -04:00
parent 080814d839
commit 4e46bcd5c1
3 changed files with 276 additions and 18 deletions

View File

@ -66,6 +66,8 @@ class LastFM:
)
timestamp = lfm_scrobble.get("timestamp")
stop_timestamp = timestamp + timedelta(seconds=track.run_time_seconds)
tzinfo = tz_timestamp.tzinfo
timezone = getattr(tzinfo, "key", None) or getattr(tzinfo, "name", None)
new_scrobble = Scrobble(
user=self.vrobbler_user,
timestamp=timestamp,
@ -75,15 +77,15 @@ class LastFM:
played_to_completion=True,
in_progress=False,
media_type=Scrobble.MediaType.TRACK,
timezone=tz_timestamp.tzinfo.name,
timezone=timezone,
visibility="private",
)
# Vrobbler scrobbles on finish, LastFM scrobbles on start
seconds_eariler = timestamp - timedelta(seconds=20)
seconds_later = timestamp + timedelta(seconds=20)
existing = Scrobble.objects.filter(
created__gte=seconds_eariler,
created__lte=seconds_later,
timestamp__gte=seconds_eariler,
timestamp__lte=seconds_later,
track=track,
).first()
if existing:
@ -238,21 +240,23 @@ def dispatch_historical_imports(user_id):
day=last_day, hour=23, minute=59, second=59, microsecond=999999
)
earliest = (
LastFmImport.objects.filter(user_id=user_id, processed_finished__isnull=False)
.order_by("processed_finished")
.first()
earliest_scrobble_dt = None
completed_imports = LastFmImport.objects.filter(
user_id=user_id, processed_finished__isnull=False
)
if earliest:
earliest_log_scrobble = earliest.scrobbles().order_by("timestamp").first()
cursor = (
_first_of_month(earliest_log_scrobble.timestamp)
if earliest_log_scrobble
else earliest.processed_finished
)
for lfm_import in completed_imports:
log_scrobble = lfm_import.scrobbles().order_by("timestamp").first()
if log_scrobble and (
earliest_scrobble_dt is None
or log_scrobble.timestamp < earliest_scrobble_dt
):
earliest_scrobble_dt = log_scrobble.timestamp
if earliest_scrobble_dt:
cursor = _first_of_month(earliest_scrobble_dt)
logger.info(
"Found existing import; earliest scrobble %s, cursor set to %s",
earliest_log_scrobble.timestamp if earliest_log_scrobble else None,
"Found existing imports; earliest scrobble %s, cursor set to %s",
earliest_scrobble_dt,
cursor,
)
else: