def main(): start_time = time() load_dotenv() logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', filename='last_update.log', filemode='w', level=logging.INFO) listutil = TraktListUtil() # do not use the cache for account specific stuff as this is subject to change with requests_cache.disabled(): if CONFIG['sync']['liked_lists']: liked_lists = pytrakt_extensions.get_liked_lists() trakt_user = trakt.users.User(getenv('TRAKT_USERNAME')) trakt_watched_movies = set( map(lambda m: m.slug, trakt_user.watched_movies)) logging.debug( "Watched movies from trakt: {}".format(trakt_watched_movies)) trakt_movie_collection = set( map(lambda m: m.slug, trakt_user.movie_collection)) #logging.debug("Movie collection from trakt:", trakt_movie_collection) if CONFIG['sync']['watchlist']: listutil.addList(None, "Trakt Watchlist", list_set=set( map(lambda m: m.slug, trakt_user.watchlist_movies))) #logging.debug("Movie watchlist from trakt:", trakt_movie_watchlist) user_ratings = trakt_user.get_ratings(media_type='movies') if CONFIG['sync']['liked_lists']: for lst in liked_lists: listutil.addList(lst['username'], lst['listname']) ratings = {} for r in user_ratings: ratings[r['movie']['ids']['slug']] = r['rating'] logging.debug("Movie ratings from trakt: {}".format(ratings)) logging.info('Loaded Trakt lists.') plex_token = getenv("PLEX_TOKEN") if plex_token == '-': plex_token = "" with requests_cache.disabled(): plex = plexapi.server.PlexServer(token=plex_token) logging.info("Server version {} updated at: {}".format( plex.version, plex.updatedAt)) logging.info("Recently added: {}".format( plex.library.recentlyAdded()[:5])) with requests_cache.disabled(): sections = plex.library.sections() for section in sections: # process movie sections section_start_time = time() if type(section) is plexapi.library.MovieSection: #clean_collections_in_section(section) print("Processing section", section.title) process_movie_section(section, trakt_watched_movies, ratings, listutil, trakt_movie_collection) # process show sections elif type(section) is plexapi.library.ShowSection: print("Processing section", section.title) process_show_section(section) else: continue timedelta = time() - section_start_time logging.warning("Completed section sync in {} s".format(timedelta)) listutil.updatePlexLists(plex) logging.info("Updated plex watchlist") timedelta = time() - start_time logging.info("Completed full sync in {} seconds".format(timedelta)) print("Completed full sync in {} seconds".format(timedelta))
def main(): start_time = time() load_dotenv() if not getenv("PLEX_TOKEN") or not getenv("TRAKT_USERNAME"): print("First run, please follow those configuration instructions.") import get_env_data load_dotenv() logLevel = logging.DEBUG if CONFIG['log_debug_messages'] else logging.INFO logfile = path.join(path.dirname(path.abspath(__file__)), "last_update.log") logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', handlers=[logging.FileHandler(logfile, 'w', 'utf-8')], level=logLevel) listutil = TraktListUtil() # do not use the cache for account specific stuff as this is subject to change start_msg = "Starting sync Plex {} and Trakt {}".format( getenv('PLEX_USERNAME'), getenv('TRAKT_USERNAME')) print(start_msg) logging.info(start_msg) with requests_cache.disabled(): try: trakt_user = trakt.users.User('me') except trakt.errors.OAuthException as e: m = "Trakt authentication error: {}".format(str(e)) logging.info(m) print(m) exit(1) if CONFIG['sync']['liked_lists']: liked_lists = pytrakt_extensions.get_liked_lists() trakt_watched_movies = set( map(lambda m: m.trakt, trakt_user.watched_movies)) logging.debug( "Watched movies from trakt: {}".format(trakt_watched_movies)) trakt_movie_collection = set( map(lambda m: m.slug, trakt_user.movie_collection)) #logging.debug("Movie collection from trakt:", trakt_movie_collection) trakt_watched_shows = pytrakt_extensions.allwatched() if CONFIG['sync']['watchlist']: listutil.addList(None, "Trakt Watchlist", slug_list=list( map(lambda m: m.slug, trakt_user.watchlist_movies))) #logging.debug("Movie watchlist from trakt:", trakt_movie_watchlist) user_ratings = trakt_user.get_ratings(media_type='movies') if CONFIG['sync']['liked_lists']: for lst in liked_lists: listutil.addList(lst['username'], lst['listname']) ratings = {} for r in user_ratings: ratings[r['movie']['ids']['slug']] = r['rating'] logging.debug("Movie ratings from trakt: {}".format(ratings)) logging.info('Loaded Trakt lists.') plex_token = getenv("PLEX_TOKEN") plex_baseurl = getenv("PLEX_BASEURL") if plex_token == '-': plex_token = "" with requests_cache.disabled(): plex = plexapi.server.PlexServer(token=plex_token, baseurl=plex_baseurl) logging.info("Server version {} updated at: {}".format( plex.version, plex.updatedAt)) logging.info("Recently added: {}".format( plex.library.recentlyAdded()[:5])) with requests_cache.disabled(): sections = plex.library.sections() for section in sections: if section.title in CONFIG['excluded-libraries']: continue # process movie sections section_start_time = time() if type(section) is plexapi.library.MovieSection: # clean_collections_in_section(section) print("Processing section", section.title) process_movie_section(section, trakt_watched_movies, ratings, listutil, trakt_movie_collection) # process show sections elif type(section) is plexapi.library.ShowSection: print("Processing section", section.title) process_show_section(section, trakt_watched_shows) else: continue timedelta = time() - section_start_time logging.warning("Completed section sync in {:.1f} s".format(timedelta)) listutil.updatePlexLists(plex) logging.info("Updated plex watchlist") timedelta = time() - start_time logging.info("Completed full sync in {:.1f} seconds".format(timedelta)) print("Completed full sync in {:.1f} seconds".format(timedelta))