PlexAutoCollectionUpdater/plexAutoCollectionUpdater.py

88 lines
3.1 KiB
Python
Raw Normal View History

2021-12-06 21:02:14 +07:00
from config.config import Config
from tmdbv3api import TMDb, Movie, Collection
from plexapi.server import PlexServer
from fuzzywuzzy import fuzz
2021-12-08 17:00:52 +07:00
from models.movie_collection import MovieCollection
2021-12-08 18:18:19 +07:00
from datetime import datetime as dt
2021-12-08 19:24:33 +07:00
import yaml
2021-12-06 21:02:14 +07:00
# Instantiate Tmdb object
tmdb = TMDb()
tmdb.api_key = Config.tmdbApiKey
tmdb.language = Config.tmdbLanguage
2021-12-08 17:00:52 +07:00
# Instantiate TMDb movie & collection object
2021-12-06 21:02:14 +07:00
tmdbMovie = Movie()
tmdbCollection = Collection()
2021-12-08 17:00:52 +07:00
# Connect to Plex Server
2021-12-06 21:02:14 +07:00
baseurl = Config.plexBaseUrl
token = Config.plexToken
plex = PlexServer(baseurl, token)
2021-12-08 17:00:52 +07:00
# Create empty lists
AvailablePlexCollectionsMovies = []
2021-12-06 21:02:14 +07:00
PlexMovieList = []
MatchingList = []
TMDbCollection = []
MovieCollectionFinal = []
2021-12-08 18:18:19 +07:00
print(":: CollectionUpdater :: Creating Empty lists")
2021-12-06 21:02:14 +07:00
2021-12-08 17:00:52 +07:00
# Retrieve the library Movies from Plex
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Retrieving Movies section from Plex")
2021-12-08 17:00:52 +07:00
plexMoviesLibrary = plex.library.section('Movies')
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Movies section from Plex retrieved")
2021-12-08 17:00:52 +07:00
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Creating Movies list and Collection Available in Plex")
2021-12-08 17:52:36 +07:00
for movie in plexMoviesLibrary.search():
2021-12-08 17:00:52 +07:00
for collection in movie.collections:
if collection.tag and collection.tag not in AvailablePlexCollectionsMovies:
AvailablePlexCollectionsMovies.append(collection.tag)
PlexMovieList.append(movie.title)
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Lists created")
2021-12-06 21:02:14 +07:00
i = 0
length = len(PlexMovieList)
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Comparing Movies in Plex to find similar movies...")
2021-12-08 18:01:23 +07:00
while i < length:
2021-12-06 21:02:14 +07:00
for movie in PlexMovieList:
2021-12-08 17:24:29 +07:00
if 85 < fuzz.ratio(PlexMovieList[i], movie) < 100:
2021-12-06 21:02:14 +07:00
if PlexMovieList[i] not in MatchingList:
MatchingList.append(PlexMovieList[i])
i += 1
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Comparison is finished...")
2021-12-08 18:18:19 +07:00
2021-12-08 19:14:40 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Searching TMDb for movies in the Matching List")
2021-12-06 21:02:14 +07:00
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))
2021-12-08 18:10:02 +07:00
for movie in MovieCollectionFinal:
2021-12-08 19:14:40 +07:00
coll = MovieCollection()
2021-12-08 18:19:35 +07:00
nameLength = len(movie)
2021-12-08 19:14:40 +07:00
coll.collectionTitle = movie.name[:nameLength - 13]
coll.collectionId = movie.id
2021-12-08 19:24:33 +07:00
2021-12-08 19:36:49 +07:00
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Checking Yaml File...")
2021-12-08 19:24:33 +07:00
2021-12-08 19:28:11 +07:00
with open(Config.PlexAutoCollectionConfigFilePath, 'r') as f:
try:
2021-12-08 19:34:32 +07:00
currentYaml = yaml.safe_load(f)
2021-12-08 19:28:11 +07:00
except yaml.YAMLError as exc:
2021-12-08 19:34:32 +07:00
print(exc)
2021-12-08 19:36:49 +07:00
for collection, movies in currentYaml.items():
print(movies)