Skip to content

Commit

Permalink
Merge pull request #1912 from glensc/trakt_lists-plugin
Browse files Browse the repository at this point in the history
Refactor: Move trakt lists logic to TraktListsPlugin plugin
  • Loading branch information
glensc authored Apr 28, 2024
2 parents 67df748 + 6091725 commit 28476ec
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
12 changes: 0 additions & 12 deletions plextraktsync/sync/Sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import TYPE_CHECKING

from plextraktsync.decorators.measure_time import measure_time
from plextraktsync.factory import logging
from plextraktsync.trakt.TraktUserListCollection import TraktUserListCollection

Expand Down Expand Up @@ -33,22 +32,11 @@ def sync(self, walker: Walker, dry_run=False):

pm.hook.init(sync=self, trakt_lists=trakt_lists, is_partial=is_partial, dry_run=dry_run)

# Skip updating lists if it's empty
add_to_lists = not trakt_lists.is_empty

if self.config.need_library_walk:
for movie in walker.find_movies():
pm.hook.walk_movie(movie=movie, dry_run=dry_run)
if add_to_lists:
trakt_lists.add_to_lists(movie)

for episode in walker.find_episodes():
pm.hook.walk_episode(episode=episode, dry_run=dry_run)
if add_to_lists:
trakt_lists.add_to_lists(episode)

if not dry_run and not trakt_lists.is_empty:
with measure_time("Updated liked list"):
trakt_lists.sync()

pm.hook.fini(walker=walker, trakt_lists=trakt_lists, dry_run=dry_run)
60 changes: 60 additions & 0 deletions plextraktsync/sync/TraktListsPlugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from plextraktsync.decorators.measure_time import measure_time
from plextraktsync.factory import logging
from plextraktsync.plugin import hookimpl

if TYPE_CHECKING:
from plextraktsync.config.SyncConfig import SyncConfig
from plextraktsync.media.Media import Media
from plextraktsync.sync.Sync import Sync
from plextraktsync.trakt.TraktUserListCollection import \
TraktUserListCollection


class TraktListsPlugin:
"""
Plugin handling syncing of Trakt lists.
"""
logger = logging.getLogger(__name__)

def __init__(self):
self.trakt_lists = None
self.add_to_lists = None

@staticmethod
def enabled(config: SyncConfig):
# Use True for now, would need to keep in sync with other plugins
return True

@classmethod
def factory(cls, sync: Sync):
return cls()

@hookimpl(trylast=True)
def init(self, trakt_lists: TraktUserListCollection):
self.trakt_lists = trakt_lists
# Skip updating lists if it's empty
self.add_to_lists = not trakt_lists.is_empty

@hookimpl
def fini(self, dry_run: bool):
if dry_run or self.trakt_lists.is_empty:
return

with measure_time("Updated liked list"):
self.trakt_lists.sync()

@hookimpl
def walk_movie(self, movie: Media):
if not self.add_to_lists:
return
self.trakt_lists.add_to_lists(movie)

@hookimpl
def walk_episode(self, episode: Media):
if not self.add_to_lists:
return
self.trakt_lists.add_to_lists(episode)
2 changes: 2 additions & 0 deletions plextraktsync/sync/plugin/SyncPluginManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def plugins(self):
from ..LikedListsPlugin import LikedListsPlugin
from ..SyncRatingsPlugin import SyncRatingsPlugin
from ..SyncWatchedPlugin import SyncWatchedPlugin
from ..TraktListsPlugin import TraktListsPlugin
from ..WatchListPlugin import WatchListPlugin
from ..WatchProgressPlugin import WatchProgressPlugin
yield AddCollectionPlugin
yield ClearCollectedPlugin
yield LikedListsPlugin
yield SyncRatingsPlugin
yield SyncWatchedPlugin
yield TraktListsPlugin
yield WatchListPlugin
yield WatchProgressPlugin

Expand Down

0 comments on commit 28476ec

Please sign in to comment.