|
|
|
@ -18,9 +18,9 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
self.last_start_time = None
|
|
|
|
self.last_start_time = None
|
|
|
|
|
|
|
|
|
|
|
|
def on_start(self):
|
|
|
|
def on_start(self):
|
|
|
|
logger.info("Parsing webhook URLs and tokens")
|
|
|
|
|
|
|
|
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(",")
|
|
|
|
|
|
|
|
logger.info(f"Parsing webhook URLs and tokens: {self.webhook_urls}")
|
|
|
|
|
|
|
|
|
|
|
|
def _build_post_data(self, track, time_position: Optional[int]=None) -> 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]))
|
|
|
|
@ -33,6 +33,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
album_name = ""
|
|
|
|
album_name = ""
|
|
|
|
if track.album:
|
|
|
|
if track.album:
|
|
|
|
album_name = getattr(track.album, "name")
|
|
|
|
album_name = getattr(track.album, "name")
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
"name": track.name,
|
|
|
|
"name": track.name,
|
|
|
|
"artist": artists,
|
|
|
|
"artist": artists,
|
|
|
|
@ -45,6 +46,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
"musicbrainz_album_id": track.album.musicbrainz_id if track.album else "",
|
|
|
|
"musicbrainz_album_id": track.album.musicbrainz_id if track.album else "",
|
|
|
|
"musicbrainz_artist_id": musicbrainz_artist_id,
|
|
|
|
"musicbrainz_artist_id": musicbrainz_artist_id,
|
|
|
|
"mopidy_uri": track.uri,
|
|
|
|
"mopidy_uri": track.uri,
|
|
|
|
|
|
|
|
"primary_artist_mopidy_uri": artists_list[0].uri,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _post_update_to_webhooks(self, post_data: dict, status: str):
|
|
|
|
def _post_update_to_webhooks(self, post_data: dict, status: str):
|
|
|
|
@ -59,7 +61,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
logger.info(f"No token found for Webhook URL: {webhook_url}")
|
|
|
|
logger.info(f"No token found for Webhook URL: {webhook_url}")
|
|
|
|
|
|
|
|
|
|
|
|
if token:
|
|
|
|
if token:
|
|
|
|
headers["Authorization"] = "Token {token}"
|
|
|
|
headers["Authorization"] = f"Token {token}"
|
|
|
|
|
|
|
|
|
|
|
|
response = requests.post(
|
|
|
|
response = requests.post(
|
|
|
|
webhook_url, json=json.dumps(post_data), headers=headers
|
|
|
|
webhook_url, json=json.dumps(post_data), headers=headers
|
|
|
|
@ -71,7 +73,7 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
self.last_start_time = int(time.time())
|
|
|
|
self.last_start_time = int(time.time())
|
|
|
|
logger.debug(f"Now playing track: {artists} - {track.name}")
|
|
|
|
logger.debug(f"Now playing track: {artists} - {track.name}")
|
|
|
|
post_data = self._build_post_data(tl_track.track)
|
|
|
|
post_data = self._build_post_data(tl_track.track, time_position=0)
|
|
|
|
|
|
|
|
|
|
|
|
# Build post data to send to urls
|
|
|
|
# Build post data to send to urls
|
|
|
|
if not self.webhook_urls:
|
|
|
|
if not self.webhook_urls:
|
|
|
|
@ -84,11 +86,11 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
track = tl_track.track
|
|
|
|
track = tl_track.track
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
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_sec = time_position // 1000
|
|
|
|
|
|
|
|
|
|
|
|
post_data = self._build_post_data(tl_track.track, time_position=time_position)
|
|
|
|
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_sec < duration // 2 and time_position_sec < 240:
|
|
|
|
logger.debug(
|
|
|
|
logger.debug(
|
|
|
|
"Track not played long enough to scrobble. (50% or 240s)"
|
|
|
|
"Track not played long enough to scrobble. (50% or 240s)"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@ -106,16 +108,10 @@ class WebhooksFrontend(pykka.ThreadingActor, CoreListener):
|
|
|
|
track = tl_track.track
|
|
|
|
track = tl_track.track
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
artists = ", ".join(sorted([a.name for a in track.artists]))
|
|
|
|
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_sec = time_position // 1000
|
|
|
|
|
|
|
|
|
|
|
|
post_data = self._build_post_data(tl_track.track, time_position=time_position)
|
|
|
|
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:
|
|
|
|
if self.last_start_time is None:
|
|
|
|
self.last_start_time = int(time.time()) - duration
|
|
|
|
self.last_start_time = int(time.time()) - duration
|
|
|
|
logger.info(
|
|
|
|
logger.info(
|
|
|
|
|