From d7cf428267b1ceac71c7752cd45a623aed71d334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 31 Jan 2022 00:15:30 +0200 Subject: [PATCH 1/5] Return values for scrobble calls --- plextraktsync/commands/watch.py | 3 ++- plextraktsync/trakt_api.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/plextraktsync/commands/watch.py b/plextraktsync/commands/watch.py index eb1943c7ad..9b5fd9aa97 100644 --- a/plextraktsync/commands/watch.py +++ b/plextraktsync/commands/watch.py @@ -151,9 +151,10 @@ def scrobble(self, m: Media, percent: float, event: PlaySessionStateNotification return self.scrobblers[tm].pause(percent) if state == "stopped": - self.scrobblers[tm].stop(percent) + value = self.scrobblers[tm].stop(percent) del self.scrobblers[tm] del self.sessions[event.session_key] + return value def watch(): diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index 513337701d..78c3d836f8 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -38,7 +38,7 @@ def __init__(self, scrobbler: Scrobbler, threshold=80): @time_limit() def update(self, progress: float): self.logger.debug(f'update({self.scrobbler.media}): {progress}') - self.scrobbler.update(progress) + return self.scrobbler.update(progress) @nocache @rate_limit() @@ -46,7 +46,7 @@ def update(self, progress: float): def pause(self, progress: float): self.logger.debug(f'pause({self.scrobbler.media}): {progress}') self.scrobbler.progress = progress - self.scrobbler.pause() + return self.scrobbler.pause() @nocache @rate_limit() @@ -55,11 +55,11 @@ def stop(self, progress: float): if progress >= self.threshold: self.logger.debug(f'stop({self.scrobbler.media}): {progress}') self.scrobbler.progress = progress - self.scrobbler.stop() + return self.scrobbler.stop() else: self.logger.debug(f'pause({self.scrobbler.media}): {progress}') self.scrobbler.progress = progress - self.scrobbler.pause() + return self.scrobbler.pause() class TraktRatingCollection(dict): From 46627de7cd1b72fc4dcc63b3b592fb600cea7247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 31 Jan 2022 00:15:05 +0200 Subject: [PATCH 2/5] Print result of scrobble to debug log --- plextraktsync/commands/watch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plextraktsync/commands/watch.py b/plextraktsync/commands/watch.py index 9b5fd9aa97..63bf2213d5 100644 --- a/plextraktsync/commands/watch.py +++ b/plextraktsync/commands/watch.py @@ -132,7 +132,8 @@ def on_play(self, event: PlaySessionStateNotification): percent = m.plex.watch_progress(event.view_offset) self.logger.info(f"on_play: {movie}: {percent:.6F}% Watched: {movie.isWatched}, LastViewed: {movie.lastViewedAt}") - self.scrobble(m, percent, event) + scrobbled = self.scrobble(m, percent, event) + self.logger.debug(f"Scrobbled: {scrobbled}") def can_scrobble(self, event: PlaySessionStateNotification): if not self.username_filter: From 631d3a61da37a8ac4462027107e0b84e23fdf51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 3 Feb 2022 19:42:18 +0200 Subject: [PATCH 3/5] Add _post wrapper, copy from pytrakt pytrakt is slow accepting pull requests, so implement it here meanwhile. --- plextraktsync/trakt_api.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index 78c3d836f8..84d8ac850c 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -6,6 +6,7 @@ import trakt.movies import trakt.sync import trakt.users +from trakt import post from trakt.errors import ForbiddenException, OAuthException from trakt.movies import Movie from trakt.sync import Scrobbler @@ -61,6 +62,19 @@ def stop(self, progress: float): self.scrobbler.progress = progress return self.scrobbler.pause() + # Copied method, until upstream is merged + # https://github.com/moogar0880/PyTrakt/pull/196 + @post + def _post(self, uri): + payload = dict( + progress=self.scrobbler.progress, + app_version=self.scrobbler.version, + date=self.scrobbler.date + ) + payload.update(self.scrobbler.media.to_json_singular()) + response = yield uri, payload + yield response + class TraktRatingCollection(dict): def __init__(self, trakt: TraktApi): From 95aab88a4715e26121eb5c4e8ab7b4c3ffd1c42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 3 Feb 2022 19:43:45 +0200 Subject: [PATCH 4/5] Update _post to take progress and apply limits --- plextraktsync/trakt_api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index 84d8ac850c..ef1429dbc5 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -64,8 +64,13 @@ def stop(self, progress: float): # Copied method, until upstream is merged # https://github.com/moogar0880/PyTrakt/pull/196 + @nocache + @rate_limit() + @time_limit() @post - def _post(self, uri): + def _post(self, method: str, progress: float): + self.scrobbler.progress = progress + uri = f'scrobble/{method}' payload = dict( progress=self.scrobbler.progress, app_version=self.scrobbler.version, From 2a11a3d1243e3f21f5aa01fce12430cf2007b821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 3 Feb 2022 19:46:52 +0200 Subject: [PATCH 5/5] Use self._post wrapper --- plextraktsync/trakt_api.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index ef1429dbc5..c49e1c398b 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -34,33 +34,21 @@ def __init__(self, scrobbler: Scrobbler, threshold=80): self.threshold = threshold self.logger = logging.getLogger("PlexTraktSync.ScrobblerProxy") - @nocache - @rate_limit() - @time_limit() def update(self, progress: float): self.logger.debug(f'update({self.scrobbler.media}): {progress}') - return self.scrobbler.update(progress) + return self._post('start', progress) - @nocache - @rate_limit() - @time_limit() def pause(self, progress: float): self.logger.debug(f'pause({self.scrobbler.media}): {progress}') - self.scrobbler.progress = progress - return self.scrobbler.pause() + return self._post('pause', progress) - @nocache - @rate_limit() - @time_limit() def stop(self, progress: float): if progress >= self.threshold: self.logger.debug(f'stop({self.scrobbler.media}): {progress}') - self.scrobbler.progress = progress - return self.scrobbler.stop() + return self._post('stop', progress) else: self.logger.debug(f'pause({self.scrobbler.media}): {progress}') - self.scrobbler.progress = progress - return self.scrobbler.pause() + return self._post('pause', progress) # Copied method, until upstream is merged # https://github.com/moogar0880/PyTrakt/pull/196