[scrobbles] Add auto stopping
This commit is contained in:
@ -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",
|
||||
|
||||
@ -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):
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user