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") 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: 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")