Fixing flow for mopidy scrobble

This commit is contained in:
2023-01-15 01:20:18 -05:00
parent 499546503c
commit 290e6dc8d9
3 changed files with 24 additions and 28 deletions

View File

@ -89,9 +89,6 @@ class Scrobble(TimeStampedModel):
cls, video: "Video", user_id: int, jellyfin_data: dict cls, video: "Video", user_id: int, jellyfin_data: dict
) -> "Scrobble": ) -> "Scrobble":
jellyfin_data['video_id'] = video.id jellyfin_data['video_id'] = video.id
logger.debug(
f"Creating or updating scrobble for video {video} with data {jellyfin_data}"
)
scrobble = ( scrobble = (
cls.objects.filter(video=video, user_id=user_id) cls.objects.filter(video=video, user_id=user_id)
.order_by('-modified') .order_by('-modified')
@ -116,10 +113,11 @@ class Scrobble(TimeStampedModel):
.order_by('-modified') .order_by('-modified')
.first() .first()
) )
logger.debug( if scrobble:
f"Found existing scrobble for track {track}, updating", logger.debug(
{"scrobble_data": scrobble_data}, f"Found existing scrobble for track {track}, updating",
) {"scrobble_data": scrobble_data},
)
backoff = timezone.now() + timedelta(seconds=TRACK_BACKOFF) backoff = timezone.now() + timedelta(seconds=TRACK_BACKOFF)
wait_period = timezone.now() + timedelta(minutes=TRACK_WAIT_PERIOD) wait_period = timezone.now() + timedelta(minutes=TRACK_WAIT_PERIOD)
@ -169,8 +167,10 @@ class Scrobble(TimeStampedModel):
) )
return return
logger.debug(f"Scrobbling to {scrobble} with status {scrobble_status}")
if scrobble: if scrobble:
logger.debug(
f"Scrobbling to {scrobble} with status {scrobble_status}"
)
scrobble.update_ticks(scrobble_data) scrobble.update_ticks(scrobble_data)
# On stop, stop progress and send it to the check for completion # On stop, stop progress and send it to the check for completion
@ -184,6 +184,10 @@ class Scrobble(TimeStampedModel):
if scrobble_status == "resumed": if scrobble_status == "resumed":
return scrobble.resume() return scrobble.resume()
for key, value in scrobble_data.items():
setattr(scrobble, key, value)
scrobble.save()
# We're not changing the scrobble, but we don't want to walk over an existing one # We're not changing the scrobble, but we don't want to walk over an existing one
# scrobble_is_finished = ( # scrobble_is_finished = (
# not scrobble.in_progress and scrobble.modified < backoff # not scrobble.in_progress and scrobble.modified < backoff
@ -193,48 +197,42 @@ class Scrobble(TimeStampedModel):
# 'Found a very recent scrobble for this item, holding off scrobbling again' # 'Found a very recent scrobble for this item, holding off scrobbling again'
# ) # )
# return # return
check_scrobble_for_finish(scrobble)
if not scrobble: else:
logger.debug(
f"Creating new scrobble with status {scrobble_status}"
)
# If we default this to "" we can probably remove this # If we default this to "" we can probably remove this
scrobble_data['scrobble_log'] = "" scrobble_data['scrobble_log'] = ""
scrobble = cls.objects.create( scrobble = cls.objects.create(
**scrobble_data, **scrobble_data,
) )
else:
for key, value in scrobble_data.items():
setattr(scrobble, key, value)
scrobble.save()
# If we hit our completion threshold, save it and get ready
# to scrobble again if we re-watch this.
scrobble = check_scrobble_for_finish(scrobble)
return scrobble return scrobble
def stop(self): def stop(self) -> None:
if not self.in_progress: if not self.in_progress:
logger.warning("Scrobble already stopped") logger.warning("Scrobble already stopped")
return return
self.in_progress = False self.in_progress = False
self.save(update_fields=['in_progress']) self.save(update_fields=['in_progress'])
return check_scrobble_for_finish(self) check_scrobble_for_finish(self)
def pause(self): def pause(self) -> None:
if self.is_paused: if self.is_paused:
logger.warning("Scrobble already paused") logger.warning("Scrobble already paused")
return return
self.is_paused = True self.is_paused = True
self.save(update_fields=["is_paused"]) self.save(update_fields=["is_paused"])
return check_scrobble_for_finish(self) check_scrobble_for_finish(self)
def resume(self): def resume(self) -> None:
if self.is_paused or not self.in_progress: if self.is_paused or not self.in_progress:
self.is_paused = False self.is_paused = False
self.in_progress = True self.in_progress = True
return self.save(update_fields=["is_paused", "in_progress"]) return self.save(update_fields=["is_paused", "in_progress"])
return self
def update_ticks(self, data): def update_ticks(self, data) -> None:
self.playback_position_ticks = data.get("playback_position_ticks") self.playback_position_ticks = data.get("playback_position_ticks")
self.playback_position = data.get("playback_position") self.playback_position = data.get("playback_position")
logger.debug( logger.debug(
@ -243,4 +241,3 @@ class Scrobble(TimeStampedModel):
self.save( self.save(
update_fields=['playback_position_ticks', 'playback_position'] update_fields=['playback_position_ticks', 'playback_position']
) )
return self

View File

@ -87,5 +87,3 @@ def check_scrobble_for_finish(scrobble: "Scrobble") -> None:
if getattr(settings, "KEEP_DETAILED_SCROBBLE_LOGS", False): if getattr(settings, "KEEP_DETAILED_SCROBBLE_LOGS", False):
scrobble.scrobble_log += f"\n{str(scrobble.timestamp)} - {scrobble.playback_position} - {str(scrobble.playback_position_ticks)} - {str(scrobble.percent_played)}%" scrobble.scrobble_log += f"\n{str(scrobble.timestamp)} - {scrobble.playback_position} - {str(scrobble.playback_position_ticks)} - {str(scrobble.percent_played)}%"
scrobble.save(update_fields=['scrobble_log']) scrobble.save(update_fields=['scrobble_log'])
return scrobble

View File

@ -308,6 +308,7 @@ LOGGING = {
"propagate": True, "propagate": True,
}, },
"django.db.backends": {"handlers": ["null"]}, "django.db.backends": {"handlers": ["null"]},
"django.server": {"handlers": ["null"]},
"vrobbler": { "vrobbler": {
"handlers": ["console", "file"], "handlers": ["console", "file"],
"propagate": True, "propagate": True,