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 # 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 AvailablePlexCollectionsMovies = [] PlexMovieList = [] MatchingList = [] TMDbCollection = [] MovieCollectionFinal = [] print(":: CollectionUpdater :: Creating Empty lists") # 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(): for collection in movie.collections: if collection.tag and collection.tag not in AvailablePlexCollectionsMovies: AvailablePlexCollectionsMovies.append(collection.tag) print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Adding {movie.title} in the list of movies Available in Plex") 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...") 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)) for movie in MovieCollectionFinal: coll = MovieCollection() nameLength = len(movie) coll.collectionTitle = movie.name[:nameLength - 13] coll.collectionId = movie.id print(f"Collection {coll.collectionTitle} with Id: {coll.collectionId} has been added")