Refactor logic to smaller methods
MODIFY
plex_trakt_sync/commands/watch.py
plex_trakt_sync/commands/watch.py
import click
from plexapi.server import PlexServer
-from plex_trakt_sync.decorators import memoize
from plex_trakt_sync.listener import WebSocketListener, PLAYING
from plex_trakt_sync.config import CONFIG
-from plex_trakt_sync.plex_api import PlexApi, PlexLibraryItem
+from plex_trakt_sync.plex_api import PlexApi
from plex_trakt_sync.trakt_api import TraktApi
@@ -14,6 +13,30 @@ def __init__(self, plex: PlexApi, trakt: TraktApi):
self.trakt = trakt
def __call__(self, message):
+ for pm, tm, item in self.filter_media(message):
+ movie = pm.item
+ percent = pm.watch_progress(item["viewOffset"])
+
+ print("%r: %.6F%% Watched: %s, LastViewed: %s" % (
+ movie, percent, movie.isWatched, movie.lastViewedAt
+ ))
+
+ self.trakt.scrobble(tm, percent)
+
+ def filter_media(self, message):
+ for item in self.filter_playing(message):
+ pm = self.plex.fetch_item(item["ratingKey"])
+ print(f"Found {pm}")
+ if not pm:
+ continue
+
+ tm = self.trakt.find_movie(pm)
+ if not tm:
+ continue
+
+ yield pm, tm, item
+
+ def filter_playing(self, message):
"""
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 0, 'playQueueItemID': 17679, 'state': 'playing'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 10000, 'playQueueItemID': 17679, 'state': 'playing', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
@@ -22,33 +45,18 @@ def __call__(self, message):
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 30000, 'playQueueItemID': 17679, 'state': 'paused', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 30000, 'playQueueItemID': 17679, 'state': 'paused', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
"""
+
if message["size"] != 1:
raise ValueError("Unexpected size: %r" % message)
for item in message["PlaySessionStateNotification"]:
- print(item)
state = item["state"]
+ print(f"State: {state}")
# "playing", 'buffering', 'stopped'
if state == 'buffering':
continue
- pm = self.plex.fetch_item(item["ratingKey"])
- print(f"Found {pm} for {item['ratingKey']}")
- if not pm:
- continue
-
- movie = pm.item
- percent = pm.watch_progress(item["viewOffset"])
-
- print("%r: %.6F%% Duration: %s, Watched: %s, LastViewed: %s" % (
- movie, percent, movie.duration, movie.isWatched, movie.lastViewedAt
- ))
-
- tm = self.trakt.find_movie(pm)
- if not tm:
- continue
-
- self.trakt.scrobble(tm, percent)
+ yield item
@click.command()

Refactor logic to smaller methods