Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: pass progress from plex when calling scrobble api #761

Merged
merged 5 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions plextraktsync/commands/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -151,9 +152,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():
Expand Down
39 changes: 23 additions & 16 deletions plextraktsync/trakt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,33 +34,39 @@ 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}')
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
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
self.scrobbler.stop()
return self._post('stop', progress)
else:
self.logger.debug(f'pause({self.scrobbler.media}): {progress}')
self.scrobbler.progress = progress
self.scrobbler.pause()
return self._post('pause', progress)

# Copied method, until upstream is merged
# https://github.com/moogar0880/PyTrakt/pull/196
@nocache
@rate_limit()
@time_limit()
@post
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,
date=self.scrobbler.date
)
payload.update(self.scrobbler.media.to_json_singular())
response = yield uri, payload
yield response


class TraktRatingCollection(dict):
Expand Down