From 18d21e665106e69d0f79d9149abfb63f135f8a87 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sat, 14 Mar 2026 13:19:18 -0400 Subject: [PATCH] [scrobbles] Add auto stopping --- vrobbler/apps/scrobbles/constants.py | 7 +++++++ vrobbler/apps/scrobbles/models.py | 25 ++++++++++++++++++++++++- vrobbler/apps/scrobbles/utils.py | 7 ++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/vrobbler/apps/scrobbles/constants.py b/vrobbler/apps/scrobbles/constants.py index 91d1684..919c0e1 100644 --- a/vrobbler/apps/scrobbles/constants.py +++ b/vrobbler/apps/scrobbles/constants.py @@ -10,6 +10,13 @@ LONG_PLAY_MEDIA = { "tasks": "Task", } +# Media types that should just be finished if they go over time +AUTO_FINISH_MEDIA = { + "webpages": "WebPage", + "tracks": "Track", + "videos": "Video", +} + PLAY_AGAIN_MEDIA = { "videogames": "VideoGame", "books": "Book", diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 8c45ea7..fc407b2 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -41,7 +41,11 @@ from profiles.utils import ( ) from puzzles.models import Puzzle from scrobbles import dataclasses as logdata -from scrobbles.constants import LONG_PLAY_MEDIA, MEDIA_END_PADDING_SECONDS +from scrobbles.constants import ( + LONG_PLAY_MEDIA, + MEDIA_END_PADDING_SECONDS, + AUTO_FINISH_MEDIA, +) from scrobbles.importers.lastfm import LastFM from scrobbles.notifications import ScrobbleNtfyNotification from scrobbles.stats import build_charts @@ -1556,6 +1560,25 @@ class Scrobble(TimeStampedModel): return beyond_completion + @property + def is_over_time(self) -> bool: + """Has the scrobble overrun it's time?""" + elapsed_scrobble_seconds = (timezone.now() - self.timestamp).seconds + + if elapsed_scrobble_seconds > self.media_obj.run_time_seconds: + return True + return False + + def auto_finish(self) -> bool: + """Check if media type should auto finish. + + Return True if scrobble is finished, False if not. + """ + if self.is_over_time and self.media_type in AUTO_FINISH_MEDIA.values(): + self.stop(force_finish=True) + return True + return False + def calculate_reading_stats(self, commit=True): # --- Sort safely by numeric page_number --- def safe_page_number(entry): diff --git a/vrobbler/apps/scrobbles/utils.py b/vrobbler/apps/scrobbles/utils.py index e220130..b2bcbed 100644 --- a/vrobbler/apps/scrobbles/utils.py +++ b/vrobbler/apps/scrobbles/utils.py @@ -324,11 +324,8 @@ def send_stop_notifications_for_in_progress_scrobbles() -> int: notifications_sent = 0 for scrobble in scrobbles_in_progress_qs: - elapsed_scrobble_seconds = ( - timezone.now() - scrobble.timestamp - ).seconds - - if elapsed_scrobble_seconds > scrobble.media_obj.run_time_seconds: + finished = scrobble.auto_finish() + if scrobble.is_over_time and not finished: ScrobbleNtfyNotification(scrobble, end=True).send() notifications_sent += 1