Add SessionCollection for managing active watch sessions
MODIFY
plex_trakt_sync/commands/watch.py
plex_trakt_sync/commands/watch.py
@@ -18,6 +18,26 @@ def __missing__(self, key):
return value
+class SessionCollection(dict):
+ def __init__(self, plex: PlexApi):
+ super(dict, self).__init__()
+ self.plex = plex
+
+ def __missing__(self, key: str):
+ self.update_sessions()
+ if key not in self:
+ # Session probably ended
+ return None
+
+ return self[key]
+
+ def update_sessions(self):
+ sessions = self.plex.get_sessions()
+ self.clear()
+ for session in sessions:
+ self[str(session.sessionKey)] = session.usernames[0]
+
+
class WatchStateUpdater:
def __init__(self, plex: PlexApi, trakt: TraktApi, mf: MediaFactory):
self.plex = plex

MODIFY
plex_trakt_sync/plex_api.py
plex_trakt_sync/plex_api.py
@@ -524,3 +524,7 @@ def history(self, m, device=False, account=False):
@nocache
def mark_watched(self, m):
m.markWatched()
+
+ @nocache
+ def get_sessions(self):
+ return self.plex.sessions()

Add SessionCollection for managing active watch sessions