Extract EventDispatcher to own class
PLAYING = "playing"
-class WebSocketListener:
- def __init__(self, plex: PlexServer, interval=1):
- self.plex = plex
- self.interval = interval
+class EventDispatcher:
+ def __init__(self):
self.event_listeners = list()
self.event_factory = EventFactory()
- self.logger = logging.getLogger("PlexTraktSync.WebSocketListener")
+ self.logger = logging.getLogger("PlexTraktSync.EventDispatcher")
def on(self, event_type, listener, **kwargs):
self.event_listeners.append({
@@ -23,6 +21,12 @@ def on(self, event_type, listener, **kwargs):
"filters": kwargs,
})
+ def event_handler(self, data):
+ self.logger.debug(data)
+ events = self.event_factory.get_events(data)
+ for event in events:
+ self.dispatch(event)
+
def dispatch(self, event):
for listener in self.event_listeners:
if not self.is_candidate(event, listener):
@@ -44,15 +48,20 @@ def is_candidate(event, listener):
return True
- def listen(self):
- def handler(data):
- self.logger.debug(data)
- events = self.event_factory.get_events(data)
- for event in events:
- self.dispatch(event)
+class WebSocketListener:
+ def __init__(self, plex: PlexServer, interval=1):
+ self.plex = plex
+ self.interval = 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):
while True:
- notifier = self.plex.startAlertListener(callback=handler)
+ notifier = self.plex.startAlertListener(callback=self.dispatcher.event_handler)
while notifier.is_alive():
sleep(self.interval)
