From 1ec433385611c9fb35837226da460ef4a718b3ed Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sun, 8 Jan 2023 14:09:18 -0500 Subject: [PATCH] Add fallback duration tracking for Mopidy Unlike Jellyfin, Mopidy's webhook only gives us a start and stopped call to determine when a track should be scrobbled. This means we don't have continous updating of playback ticks. This commit adds a fallback when ticks are not there to use the track duration and time since the scrobble was created. That said, this is not perfect. If you pause the track and start again, the progress will get very out of whack. But thankfully, Mopidy only sends us audio, and it's rare that audio tracks are paused repeatedly and started again before finishing a scrobble. So hopefully this shouldn't happen very often. --- vrobbler/apps/scrobbles/models.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 7dacf96..10f6eb5 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -42,6 +42,12 @@ class Scrobble(TimeStampedModel): (self.playback_position_ticks / self.media_run_time_ticks) * 100 ) + # If we don't have media_run_time_ticks, let's guess from created time + now = timezone.now() + playback_duration = (now - self.created).seconds + if playback_duration and self.track.run_time: + return int((playback_duration / int(self.track.run_time)) * 100) + return 0 @property @@ -97,6 +103,15 @@ class Scrobble(TimeStampedModel): .order_by('-modified') .first() ) + # Check if playback_position_ticks has changed from this scrobble + scrobble_changed = ( + scrobble + and scrobble.playback_position_ticks + != jellyfin_data['playback_position_ticks'] + ) + if not scrobble_changed: + logger.info('Scrobble playback has not changed, not scrobbling') + return # Backoff is how long until we consider this a new scrobble backoff = timezone.now() + timedelta(minutes=VIDEO_BACKOFF)