From 8e0cc85a129e0049468dac12cd2a411634e53235 Mon Sep 17 00:00:00 2001 From: rouggy Date: Wed, 15 Dec 2021 16:55:14 +0100 Subject: [PATCH] update --- PlexAutoCollectionUpdater.py | 46 ++++++++++----- backup.py | 108 ----------------------------------- 2 files changed, 32 insertions(+), 122 deletions(-) delete mode 100644 backup.py diff --git a/PlexAutoCollectionUpdater.py b/PlexAutoCollectionUpdater.py index 3cca835..2f74074 100644 --- a/PlexAutoCollectionUpdater.py +++ b/PlexAutoCollectionUpdater.py @@ -2,9 +2,15 @@ from plexapi.server import PlexServer from config.config import Config from models.movie import UpdatedMovie from tmdbv3api import TMDb, Movie -import os from datetime import datetime as dt -import yaml, os, urllib.request +import yaml, os, urllib.request, logging + +logging.basicConfig(filename='plexAutoCollectionUpdater.log', level=logging.INFO) +logger = logging.getLogger("CollectionUpdater") +ch = logging.StreamHandler() +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +ch.setFormatter(formatter) +logger.addHandler(ch) # Instantiate Tmdb object tmdb = TMDb() @@ -17,11 +23,13 @@ token = Config.plexToken plex = PlexServer(baseurl, token) plexMoviesLibrary = plex.library.section('Movies') +plexMoviesLibrary = plexMoviesLibrary.search() MoviesList = [] -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Retrieving Movies from Plex and Updating TMDb information...") -for item in plexMoviesLibrary.search(): +logger.info(f"Retrieving {len(plexMoviesLibrary)} Movies from Plex and Updating TMDb information...") + +for item in plexMoviesLibrary: movie = UpdatedMovie() tmdbMovie = Movie() @@ -35,8 +43,9 @@ for item in plexMoviesLibrary.search(): # Search for the movie on TMDb try: tmdbMovieDetails = tmdbMovie.details(movie_id=movie.TMDbId) + logger.debug(f'Getting TMDb details for {movie.PlexTitle}') except: - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Cannot get details of {movie.PlexTitle} from TMDb") + logger.warning(f"CollectionUpdater :: Cannot get details of {movie.PlexTitle} from TMDb") if tmdbMovieDetails['belongs_to_collection']: movie.MovieTMDbCollectionName = tmdbMovieDetails['belongs_to_collection'].name[:len(tmdbMovieDetails['belongs_to_collection'].name) - 7] @@ -47,8 +56,9 @@ for item in plexMoviesLibrary.search(): # Add movie to the list of Movies if movie.MovieTMDbCollectionTMDbId: + logger.info(f'Movie {movie.PlexTitle} has been added to Collection List') MoviesList.append(movie) -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Retrieved Movies from Plex and Updated TMDb information for {len(MoviesList)} movies...") +logger.info("Retrieved Movies from Plex and Updated TMDb information for {len(MoviesList)} movies...") i = 0 count = 0 @@ -60,17 +70,25 @@ while i < len(MoviesList): if MoviesList[i].MovieTMDbCollectionTMDbId == movie.MovieTMDbCollectionTMDbId: count += 1 if count > 2: + logger.info(f"Keeping collection {MoviesList[i].MovieTMDbCollectionName} which contains 3 or more movies") if not MoviesList[i].MovieTMDbCollectionTMDbId in ExistingCollectionId: ExistingCollectionId.append(MoviesList[i].MovieTMDbCollectionTMDbId) MovieListFinal.append(MoviesList[i]) i += 1 count = 0 -with open(Config.PlexAutoCollectionConfigFilePath, 'r') as f: - try: - currentYaml = yaml.safe_load(f) - except yaml.YAMLError as exc: - print(exc) +logger.info(f"Found a total of {len(MovieListFinal)} collections, updating config file now !") + +currentYaml = object() + +try: + with open(Config.PlexAutoCollectionConfigFilePath, 'r') as f: + try: + currentYaml = yaml.safe_load(f) + except yaml.YAMLError as exc: + print(exc) +except: + logger.warning(f"Could not find the config file with path: {Config.PlexAutoCollectionConfigFilePath}") currentYamlCollections = [] @@ -84,13 +102,13 @@ for collection in MovieListFinal: currentYaml['collections'].update(new_coll) if currentYaml: - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Adding new collection: {collection.MovieTMDbCollectionName} to Yaml file...") + logger.info(f"Adding new collection: {collection.MovieTMDbCollectionName} to Yaml file...") with open(Config.PlexAutoCollectionConfigFilePath, 'w') as f: yaml.safe_dump(currentYaml, f) if not os.path.exists(Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.MovieTMDbCollectionName}"): - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Download poster for new collection {collection.MovieTMDbCollectionName}...") + logger.info(f"Download poster for new collection {collection.MovieTMDbCollectionName}...") os.mkdir(Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.MovieTMDbCollectionName}") urllib.request.urlretrieve(Config.tmdbImgUrl + f"{collection.MovieTMDbCollectionPosterPath}", Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.MovieTMDbCollectionName}/poster.jpeg") -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: All steps are completed...") \ No newline at end of file +logger.info(f"All collections have been updated in plex...") \ No newline at end of file diff --git a/backup.py b/backup.py deleted file mode 100644 index 719f231..0000000 --- a/backup.py +++ /dev/null @@ -1,108 +0,0 @@ -from config.config import Config -from tmdbv3api import TMDb, Movie, Collection -from plexapi.server import PlexServer -from fuzzywuzzy import fuzz -from models.movie_collection import MovieCollection -from datetime import datetime as dt -import yaml, os, urllib.request - -# Instantiate Tmdb object -tmdb = TMDb() -tmdb.api_key = Config.tmdbApiKey -tmdb.language = Config.tmdbLanguage - -# Instantiate TMDb movie & collection object -tmdbMovie = Movie() -tmdbCollection = Collection() - -# Connect to Plex Server -baseurl = Config.plexBaseUrl -token = Config.plexToken -plex = PlexServer(baseurl, token) - -# Create empty lists -PlexMovieList = [] -MatchingList = [] -TMDbCollection = [] -MovieCollectionFinal = [] - -# Retrieve the library Movies from Plex -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Retrieving Movies section from Plex") -plexMoviesLibrary = plex.library.section('Movies') -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Movies section from Plex retrieved") - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Creating Movies list and Collection Available in Plex") -for movie in plexMoviesLibrary.search(): - PlexMovieList.append(movie.title) -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Lists created, found {len(PlexMovieList)} movies") - -i = 0 - -length = len(PlexMovieList) - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Comparing Movies in Plex to find similar movies...") -while i < length: - for movie in PlexMovieList: - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Checking matching between {PlexMovieList[i]} and {movie}, score is: {fuzz.ratio(PlexMovieList[i], movie)}") - if 85 < fuzz.ratio(PlexMovieList[i], movie) < 100: - if PlexMovieList[i] not in MatchingList: - MatchingList.append(PlexMovieList[i]) - i += 1 - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Comparison is finished...found {len(MatchingList)} similar movies") - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Searching TMDb for movies in the Matching List") -for movie in MatchingList: - movieSearch = tmdbMovie.search(movie) - movieId = movieSearch[0].id - movieDetails = tmdbMovie.details(movie_id=movieId) - - if movieDetails.belongs_to_collection is not None: - collectionId = movieDetails.belongs_to_collection - - if collectionId.id not in TMDbCollection: - TMDbCollection.append(collectionId.id) - -for id in TMDbCollection: - MovieCollectionFinal.append(tmdbCollection.details(id)) - -MovieCollectionList = [] - -for movie in MovieCollectionFinal: - coll = MovieCollection() - nameLength = len(movie) - coll.collectionTitle = movie.name[:nameLength - 13] - coll.collectionId = movie.id - coll.collectionImgPath = movie.poster_path - MovieCollectionList.append(coll) - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Checking and Updating Yaml File...") - -with open(Config.PlexAutoCollectionConfigFilePath, 'r') as f: - try: - currentYaml = yaml.safe_load(f) - except yaml.YAMLError as exc: - print(exc) - -currentYamlCollections = [] - -for movies in currentYaml['collections']: - currentYamlCollections.append(movies) - -for collection in MovieCollectionList: - if collection.collectionTitle not in currentYamlCollections: - with open(Config.PlexAutoCollectionConfigFilePath, 'r') as f: - new_coll = {f'{collection.collectionTitle}': {'sync_mode': 'sync', 'tmdb_collection': collection.collectionId, 'tmdb_summary': collection.collectionId}} - currentYaml['collections'].update(new_coll) - - if currentYaml: - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Adding new collection: {collection.collectionTitle} to Yaml file...") - with open(Config.PlexAutoCollectionConfigFilePath, 'w') as f: - yaml.safe_dump(currentYaml, f) - - if not os.path.exists(Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.collectionTitle}"): - print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Download poster for new collection {collection.collectionTitle}...") - os.mkdir(Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.collectionTitle}") - urllib.request.urlretrieve(Config.tmdbImgUrl + f"{collection.collectionImgPath}", Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.collectionTitle}/poster.jpeg") - -print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: All steps are completed...") \ No newline at end of file