-
Notifications
You must be signed in to change notification settings - Fork 113
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 #166 from glensc/sync-cli
- Loading branch information
Showing
5 changed files
with
158 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import click | ||
|
||
from plex_trakt_sync.requests_cache import requests_cache | ||
from plex_trakt_sync.main import get_plex_server | ||
from plex_trakt_sync.config import CONFIG | ||
from plex_trakt_sync.decorators import measure_time | ||
from plex_trakt_sync.main import process_movie_section, process_show_section | ||
from plex_trakt_sync.plex_api import PlexApi | ||
from plex_trakt_sync.trakt_api import TraktApi | ||
from plex_trakt_sync.trakt_list_util import TraktListUtil | ||
from plex_trakt_sync.logging import logging | ||
|
||
|
||
def sync_all(): | ||
with requests_cache.disabled(): | ||
server = get_plex_server() | ||
listutil = TraktListUtil() | ||
plex = PlexApi(server) | ||
trakt = TraktApi() | ||
|
||
with measure_time("Loaded Trakt lists"): | ||
trakt_watched_movies = trakt.watched_movies | ||
trakt_watched_shows = trakt.watched_shows | ||
trakt_movie_collection = trakt.movie_collection | ||
trakt_ratings = trakt.ratings | ||
trakt_watchlist_movies = trakt.watchlist_movies | ||
trakt_liked_lists = trakt.liked_lists | ||
|
||
if trakt_watchlist_movies: | ||
listutil.addList(None, "Trakt Watchlist", traktid_list=trakt_watchlist_movies) | ||
|
||
for lst in trakt_liked_lists: | ||
listutil.addList(lst['username'], lst['listname']) | ||
|
||
with requests_cache.disabled(): | ||
logging.info("Server version {} updated at: {}".format(server.version, server.updatedAt)) | ||
logging.info("Recently added: {}".format(server.library.recentlyAdded()[:5])) | ||
|
||
for section in plex.library_sections: | ||
if PlexApi.is_movie(section): | ||
with measure_time("Processing section %s" % section.title): | ||
process_movie_section(section, trakt_watched_movies, trakt_ratings, listutil, trakt_movie_collection) | ||
elif PlexApi.is_show(section): | ||
with measure_time("Processing section %s" % section.title): | ||
process_show_section(section, trakt_watched_shows, listutil) | ||
else: | ||
continue | ||
|
||
with measure_time("Updated plex watchlist"): | ||
listutil.updatePlexLists(server) | ||
|
||
|
||
@click.command() | ||
def sync(): | ||
""" | ||
Perform sync between Plex and Trakt | ||
""" | ||
|
||
logging.info("Starting sync Plex {} and Trakt {}".format(CONFIG['PLEX_USERNAME'], CONFIG['TRAKT_USERNAME'])) | ||
|
||
with measure_time("Completed full sync"): | ||
sync_all() |
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,84 @@ | ||
import trakt | ||
|
||
from plex_trakt_sync import pytrakt_extensions | ||
from plex_trakt_sync.path import pytrakt_file | ||
|
||
trakt.core.CONFIG_PATH = pytrakt_file | ||
import trakt.users | ||
from trakt.errors import OAuthException, ForbiddenException | ||
|
||
from plex_trakt_sync.logging import logging | ||
from plex_trakt_sync.decorators import memoize, nocache | ||
from plex_trakt_sync.config import CONFIG | ||
|
||
|
||
class TraktApi: | ||
""" | ||
Trakt API class abstracting common data access and dealing with requests cache. | ||
""" | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def me(self): | ||
try: | ||
return trakt.users.User('me') | ||
except (OAuthException, ForbiddenException) as e: | ||
logging.fatal("Trakt authentication error: {}".format(str(e))) | ||
raise e | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def liked_lists(self): | ||
if not CONFIG['sync']['liked_lists']: | ||
return [] | ||
return pytrakt_extensions.get_liked_lists() | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def watched_movies(self): | ||
return set( | ||
map(lambda m: m.trakt, self.me.watched_movies) | ||
) | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def movie_collection(self): | ||
return set( | ||
map(lambda m: m.trakt, self.me.movie_collection) | ||
) | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def watched_shows(self): | ||
return pytrakt_extensions.allwatched() | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def watchlist_movies(self): | ||
if not CONFIG['sync']['watchlist']: | ||
return [] | ||
|
||
return list( | ||
map(lambda m: m.trakt, self.me.watchlist_movies) | ||
) | ||
|
||
@property | ||
@memoize | ||
@nocache | ||
def movie_ratings(self): | ||
return self.me.get_ratings(media_type='movies') | ||
|
||
@property | ||
@memoize | ||
def ratings(self): | ||
ratings = {} | ||
for r in self.movie_ratings: | ||
ratings[r['movie']['ids']['slug']] = r['rating'] | ||
|
||
return ratings |