Add better property for multiple media types

This adds a fun helper method on Scrobble instances to get whatever the
type should be based on media_obj
This commit is contained in:
2023-01-12 15:42:05 -05:00
parent cf55c9b464
commit 69f596039d
2 changed files with 20 additions and 35 deletions

View File

@ -52,28 +52,21 @@ class Scrobble(TimeStampedModel):
@property @property
def percent_played(self) -> int: def percent_played(self) -> int:
if self.playback_position_ticks and self.media_run_time_ticks: if self.playback_position_ticks and self.media_obj.run_time_ticks:
return int( return int(
(self.playback_position_ticks / self.media_run_time_ticks) (self.playback_position_ticks / self.media_obj.run_time_ticks)
* 100 * 100
) )
# If we don't have media_run_time_ticks, let's guess from created time # If we don't have media_obj.run_time_ticks, let's guess from created time
now = timezone.now() now = timezone.now()
playback_duration = (now - self.created).seconds playback_duration = (now - self.created).seconds
if playback_duration and self.track.run_time: if playback_duration and self.media_obj.run_time:
return int((playback_duration / int(self.track.run_time)) * 100) return int(
(playback_duration / int(self.media_obj.run_time)) * 100
)
return 0 return 0
@property
def media_run_time_ticks(self) -> int:
if self.video:
return self.video.run_time_ticks
if self.track:
return self.track.run_time_ticks
# this is hacky, but want to avoid divide by zero
return 1
def is_stale(self, backoff, wait_period) -> bool: def is_stale(self, backoff, wait_period) -> bool:
scrobble_is_stale = self.in_progress and self.modified > wait_period scrobble_is_stale = self.in_progress and self.modified > wait_period
@ -94,16 +87,19 @@ class Scrobble(TimeStampedModel):
return scrobble_is_stale return scrobble_is_stale
def __str__(self): @property
media = None def media_obj(self):
media_obj = None
if self.video: if self.video:
media = self.video media_obj = self.video
if self.track: if self.track:
media = self.track media_obj = self.track
if self.podcast_episode:
media_obj = self.podcast_episode
return media_obj
return ( def __str__(self):
f"Scrobble of {media} {self.timestamp.year}-{self.timestamp.month}" return f"Scrobble of {self.media_obj} {self.timestamp.year}-{self.timestamp.month}"
)
@classmethod @classmethod
def create_or_update_for_video( def create_or_update_for_video(

View File

@ -194,27 +194,16 @@
<ul style="padding-right:10px;"> <ul style="padding-right:10px;">
<b>Now playing</b> <b>Now playing</b>
{% for scrobble in now_playing_list %} {% for scrobble in now_playing_list %}
{% if scrobble.video %}
<div> <div>
{{scrobble.video.title}}<br/> {{scrobble.media_obj.title}}<br/>
<small>{{scrobble.created|naturaltime}}<br/> {% if scrobble.track %}<em>{{scrobble.track.artist}}</em><br/>{% endif %}
from {{scrobble.source}}</small> {% if scrobble.podcast_episode%}<em>{{scrobble.podcast_episode.podcast}}</em><br/>{% endif %}
<div class="progress-bar">
<span class="progress-bar-fill" style="width: {{scrobble.percent_played}}%;"></span>
</div>
</div>
{% endif %}
{% if scrobble.track %}
<div>
{{scrobble.track.title}}<br/>
<em>{{scrobble.track.artist}}</em><br/>
<small>{{scrobble.created|naturaltime}}<br/> <small>{{scrobble.created|naturaltime}}<br/>
from {{scrobble.source}}</small> from {{scrobble.source}}</small>
<div class="progress-bar" style="margin-right:5px;"> <div class="progress-bar" style="margin-right:5px;">
<span class="progress-bar-fill" style="width: {{scrobble.percent_played}}%;"></span> <span class="progress-bar-fill" style="width: {{scrobble.percent_played}}%;"></span>
</div> </div>
</div> </div>
{% endif %}
<hr/> <hr/>
{% endfor %} {% endfor %}
</ul> </ul>