-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1378 from glensc/move-class
- Loading branch information
Showing
12 changed files
with
178 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from plextraktsync.decorators.cached_property import cached_property | ||
|
||
if TYPE_CHECKING: | ||
from plextraktsync.config.Config import Config | ||
|
||
|
||
class SyncConfig: | ||
def __init__(self, config: Config): | ||
self.config = dict(config["sync"]) | ||
|
||
def __getitem__(self, key): | ||
return self.config[key] | ||
|
||
def __contains__(self, key): | ||
return key in self.config | ||
|
||
def get(self, section, key): | ||
return self[key] if key in self else self[section][key] | ||
|
||
@cached_property | ||
def trakt_to_plex(self): | ||
return { | ||
"watched_status": self.get("trakt_to_plex", "watched_status"), | ||
"ratings": self.get("trakt_to_plex", "ratings"), | ||
"liked_lists": self.get("trakt_to_plex", "liked_lists"), | ||
"watchlist": self.get("trakt_to_plex", "watchlist"), | ||
"watchlist_as_playlist": self.get("trakt_to_plex", "watchlist_as_playlist"), | ||
} | ||
|
||
@cached_property | ||
def plex_to_trakt(self): | ||
return { | ||
"watched_status": self.get("plex_to_trakt", "watched_status"), | ||
"ratings": self.get("plex_to_trakt", "ratings"), | ||
"collection": self.get("plex_to_trakt", "collection"), | ||
"watchlist": self.get("plex_to_trakt", "watchlist"), | ||
} | ||
|
||
@cached_property | ||
def sync_ratings(self): | ||
return self.trakt_to_plex["ratings"] or self.plex_to_trakt["ratings"] | ||
|
||
@cached_property | ||
def sync_watchlists(self): | ||
return self.trakt_to_plex["watchlist"] or self.plex_to_trakt["watchlist"] | ||
|
||
@cached_property | ||
def clear_collected(self): | ||
return self.plex_to_trakt["collection"] and self["plex_to_trakt"]["clear_collected"] | ||
|
||
@cached_property | ||
def sync_watched_status(self): | ||
return self.trakt_to_plex["watched_status"] or self.plex_to_trakt["watched_status"] | ||
|
||
@cached_property | ||
def update_plex_wl(self): | ||
return self.trakt_to_plex["watchlist"] and not self.trakt_to_plex["watchlist_as_playlist"] | ||
|
||
@cached_property | ||
def update_plex_wl_as_pl(self): | ||
return self.trakt_to_plex["watchlist"] and self.trakt_to_plex["watchlist_as_playlist"] | ||
|
||
@cached_property | ||
def update_trakt_wl(self): | ||
return self.plex_to_trakt["watchlist"] | ||
|
||
@cached_property | ||
def sync_wl(self): | ||
return self.update_plex_wl or self.update_trakt_wl | ||
|
||
@cached_property | ||
def sync_liked_lists(self): | ||
return self.trakt_to_plex["liked_lists"] | ||
|
||
@cached_property | ||
def need_library_walk(self): | ||
return any([ | ||
self.update_plex_wl_as_pl, | ||
self.sync_watched_status, | ||
self.sync_ratings, | ||
self.plex_to_trakt["collection"], | ||
self.sync_liked_lists, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import importlib | ||
|
||
|
||
class EventFactory: | ||
EVENTS = { | ||
"account": "AccountUpdateNotification", | ||
"activity": "ActivityNotification", | ||
"backgroundProcessingQueue": "BackgroundProcessingQueueEventNotification", | ||
"playing": "PlaySessionStateNotification", | ||
"preference": "Setting", | ||
"progress": "ProgressNotification", | ||
"reachability": "ReachabilityNotification", | ||
"status": "StatusNotification", | ||
"timeline": "TimelineEntry", | ||
"transcodeSession.end": "TranscodeSession", | ||
"transcodeSession.start": "TranscodeSession", | ||
"transcodeSession.update": "TranscodeSession", | ||
} | ||
|
||
def __init__(self): | ||
self.module = importlib.import_module(self.__module__) | ||
|
||
def get_events(self, message): | ||
if message["size"] != 1: | ||
raise ValueError(f"Unexpected size: {message}") | ||
|
||
message_type = message["type"] | ||
if message_type not in self.EVENTS: | ||
return | ||
class_name = self.EVENTS[message_type] | ||
if class_name not in message: | ||
return | ||
for data in message[class_name]: | ||
event = self.create(class_name, **data) | ||
yield event | ||
|
||
def create(self, name, **kwargs): | ||
cls = getattr(self.module, name) | ||
return cls(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from time import sleep | ||
from typing import TYPE_CHECKING | ||
|
||
from plextraktsync.factory import logging | ||
from plextraktsync.watch.EventDispatcher import EventDispatcher | ||
from plextraktsync.watch.events import Error | ||
|
||
if TYPE_CHECKING: | ||
from plexapi.server import PlexServer | ||
|
||
|
||
class WebSocketListener: | ||
def __init__(self, plex: PlexServer, poll_interval=5, restart_interval=15): | ||
self.plex = plex | ||
self.poll_interval = poll_interval | ||
self.restart_interval = restart_interval | ||
self.dispatcher = EventDispatcher() | ||
self.logger = logging.getLogger("PlexTraktSync.WebSocketListener") | ||
|
||
def on(self, event_type, listener, **kwargs): | ||
self.dispatcher.on(event_type, listener, **kwargs) | ||
|
||
def listen(self): | ||
self.logger.info("Listening for events!") | ||
while True: | ||
notifier = self.plex.startAlertListener( | ||
callback=self.dispatcher.event_handler | ||
) | ||
while notifier.is_alive(): | ||
sleep(self.poll_interval) | ||
|
||
self.dispatcher.event_handler(Error(msg="Server closed connection")) | ||
self.logger.error( | ||
f"Listener finished. Restarting in {self.restart_interval} seconds" | ||
) | ||
sleep(self.restart_interval) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.