Skip to content

Commit

Permalink
Merge pull request #1562 from glensc/watch-title
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Aug 31, 2023
2 parents 442dda4 + 6469631 commit f70d598
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
3 changes: 2 additions & 1 deletion plextraktsync/commands/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from plextraktsync.factory import factory
from plextraktsync.watch.events import (ActivityNotification, Error,
PlaySessionStateNotification,
TimelineEntry)
ServerStarted, TimelineEntry)


def watch(server: str):
Expand All @@ -13,6 +13,7 @@ def watch(server: str):
ws = factory.web_socket_listener
updater = factory.watch_state_updater

ws.on(ServerStarted, updater.on_start)
ws.on(
PlaySessionStateNotification,
updater.on_play,
Expand Down
3 changes: 3 additions & 0 deletions plextraktsync/mixin/SetWindowTitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ def console(self):

return factory.console

def clear_window_title(self):
self.console.set_window_title("PlexTraktSync")

def set_window_title(self, title: str):
self.console.set_window_title(f"PlexTraktSync: {title}")
4 changes: 2 additions & 2 deletions plextraktsync/watch/EventDispatcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from plextraktsync.factory import logging
from plextraktsync.watch.EventFactory import EventFactory
from plextraktsync.watch.events import Error
from plextraktsync.watch.events import Error, ServerStarted


class EventDispatcher:
Expand All @@ -21,7 +21,7 @@ def on(self, event_type, listener, **kwargs):

def event_handler(self, data):
self.logger.debug(data)
if isinstance(data, Error):
if isinstance(data, (Error, ServerStarted)):
return self.dispatch(data)

events = self.event_factory.get_events(data)
Expand Down
18 changes: 16 additions & 2 deletions plextraktsync/watch/WatchStateUpdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from typing import TYPE_CHECKING

from plextraktsync.factory import logging
from plextraktsync.mixin.SetWindowTitle import SetWindowTitle
from plextraktsync.watch.events import (ActivityNotification, Error,
PlaySessionStateNotification,
TimelineEntry)
ServerStarted, TimelineEntry)

if TYPE_CHECKING:
from plextraktsync.config.Config import Config
Expand All @@ -16,7 +17,7 @@
from plextraktsync.trakt.TraktApi import TraktApi


class WatchStateUpdater:
class WatchStateUpdater(SetWindowTitle):
def __init__(
self,
plex: PlexApi,
Expand Down Expand Up @@ -92,6 +93,16 @@ def find_by_key(self, key: str, reload=False):

return m

@property
def server(self):
return self.plex.plex

def on_start(self, event: ServerStarted):
self.reset_title()

def reset_title(self):
self.set_window_title(f"watch: {self.server.friendlyName} ({self.server.version})")

def on_error(self, error: Error):
self.logger.error(error.msg)
self.scrobblers.clear()
Expand Down Expand Up @@ -153,18 +164,21 @@ def scrobble(self, m: Media, percent: float, event: PlaySessionStateNotification
if state == "playing":
if self.progressbar is not None:
self.progressbar.play(m.plex, percent)
self.set_window_title(f"Watching {m.title}")

return self.scrobblers[tm].update(percent)

if state == "paused":
if self.progressbar is not None:
self.progressbar.pause(m.plex, percent)
self.reset_title()

return self.scrobblers[tm].pause(percent)

if state == "stopped":
if self.progressbar is not None:
self.progressbar.stop(m.plex)
self.reset_title()

value = self.scrobblers[tm].stop(percent)
del self.scrobblers[tm]
Expand Down
4 changes: 3 additions & 1 deletion plextraktsync/watch/WebSocketListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from plextraktsync.factory import logging
from plextraktsync.watch.EventDispatcher import EventDispatcher
from plextraktsync.watch.events import Error
from plextraktsync.watch.events import Error, ServerStarted

if TYPE_CHECKING:
from plexapi.server import PlexServer
Expand All @@ -28,6 +28,8 @@ def listen(self):
notifier = self.plex.startAlertListener(
callback=self.dispatcher.event_handler
)
self.dispatcher.event_handler(ServerStarted(notifier=notifier))

while notifier.is_alive():
sleep(self.poll_interval)

Expand Down
18 changes: 18 additions & 0 deletions plextraktsync/watch/events.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from plexapi.server import PlexServer


class Event(dict):
def __str__(self):
return f"{self.__class__}:{str(self.copy())}"


class ServerStarted(Event):
@property
def notifier(self):
return self["notifier"]

@property
def server(self) -> PlexServer:
return self.notifier._server


class Error(Event):
@property
def msg(self):
Expand Down

0 comments on commit f70d598

Please sign in to comment.