Add handling of pausing and resuming

This commit is contained in:
2023-01-12 14:29:12 -05:00
parent 4fbd7a0659
commit 42e146bd32

View File

@ -1,6 +1,7 @@
import logging import logging
import time import time
import json import json
from typing import Optional
import pykka import pykka
import requests import requests
@ -21,7 +22,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
self.webhook_urls = self.config["webhooks"]["urls"].split(",") self.webhook_urls = self.config["webhooks"]["urls"].split(",")
self.webhook_tokens = self.config["webhooks"]["tokens"].split(",") self.webhook_tokens = self.config["webhooks"]["tokens"].split(",")
def _build_post_data(self, track) -> dict: def _build_post_data(self, track, time_position: Optional[int]=None) -> dict:
artists = ", ".join(sorted([a.name for a in track.artists])) artists = ", ".join(sorted([a.name for a in track.artists]))
artists_list = [a for a in track.artists] artists_list = [a for a in track.artists]
try: try:
@ -36,6 +37,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
"track_number": track.track_no, "track_number": track.track_no,
"run_time_ticks": track.length, "run_time_ticks": track.length,
"run_time": str(duration), "run_time": str(duration),
"playback_time_ticks": time_position,
"musicbrainz_track_id": track.musicbrainz_id, "musicbrainz_track_id": track.musicbrainz_id,
"musicbrainz_album_id": track.album.musicbrainz_id, "musicbrainz_album_id": track.album.musicbrainz_id,
"musicbrainz_artist_id": musicbrainz_artist_id, "musicbrainz_artist_id": musicbrainz_artist_id,
@ -81,7 +83,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
duration = track.length and track.length // 1000 or 0 duration = track.length and track.length // 1000 or 0
time_position = time_position // 1000 time_position = time_position // 1000
post_data = self._build_post_data(tl_track.track) post_data = self._build_post_data(tl_track.track, time_position=time_position)
if time_position < duration // 2 and time_position < 240: if time_position < duration // 2 and time_position < 240:
logger.debug( logger.debug(
@ -96,3 +98,40 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
) )
self._post_update_to_webhooks(post_data, "stopped") self._post_update_to_webhooks(post_data, "stopped")
def track_playback_paused(self, tl_track, time_position):
track = tl_track.track
artists = ", ".join(sorted([a.name for a in track.artists]))
duration = track.length and track.length // 1000 or 0
time_position = time_position // 1000
post_data = self._build_post_data(tl_track.track, time_position=time_position)
if time_position < duration // 2 and time_position < 240:
logger.debug(
"Track not played long enough to scrobble. (50% or 240s)"
)
return
if self.last_start_time is None:
self.last_start_time = int(time.time()) - duration
logger.info(
f"Scrobbling paused via webhooks: {artists} - {track.name}"
)
self._post_update_to_webhooks(post_data, "paused")
def track_playback_resumed(self, tl_track, time_position):
track = tl_track.track
artists = ", ".join(sorted([a.name for a in track.artists]))
self.last_start_time = int(time.time())
logger.debug(f"Now resuming track: {artists} - {track.name}")
post_data = self._build_post_data(tl_track.track, time_position=time_position)
# Build post data to send to urls
if not self.webhook_urls:
logger.info("No webhook URLS are configured ")
return
logger.info(f"Scrobbling via webhooks: {artists} - {track.name}")
self._post_update_to_webhooks(post_data, "resumed")