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 21:12:34 +07:00
|
|
|
import yaml, os, urllib.request
|
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
|
2021-12-06 21:02:14 +07:00
|
|
|
PlexMovieList = []
|
|
|
|
MatchingList = []
|
|
|
|
TMDbCollection = []
|
|
|
|
MovieCollectionFinal = []
|
|
|
|
|
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
|
|
|
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 22:47:05 +07:00
|
|
|
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Comparison is finished...found {len(MatchingList)} similar movies")
|
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 20:46:42 +07:00
|
|
|
MovieCollectionList = []
|
|
|
|
|
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 21:12:34 +07:00
|
|
|
coll.collectionImgPath = movie.poster_path
|
2021-12-08 20:46:42 +07:00
|
|
|
MovieCollectionList.append(coll)
|
2021-12-08 19:24:33 +07:00
|
|
|
|
2021-12-08 21:12:34 +07:00
|
|
|
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Checking and Updating 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 20:46:42 +07:00
|
|
|
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:
|
2021-12-08 21:12:34 +07:00
|
|
|
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Adding new collection: {collection.collectionTitle} to Yaml file...")
|
2021-12-08 20:46:42 +07:00
|
|
|
with open(Config.PlexAutoCollectionConfigFilePath, 'w') as f:
|
2021-12-08 21:12:34 +07:00
|
|
|
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}")
|
2021-12-08 21:16:16 +07:00
|
|
|
urllib.request.urlretrieve(Config.tmdbImgUrl + f"{collection.collectionImgPath}", Config.PlexAutoCollectionConfigFileImagesPath + f"/{collection.collectionTitle}/poster.jpeg")
|