update
This commit is contained in:
parent
6e82e61c8e
commit
2417b23ff2
97
PlexAutoCollectionUpdater.py
Normal file
97
PlexAutoCollectionUpdater.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
# Instantiate Tmdb object
|
||||||
|
tmdb = TMDb()
|
||||||
|
tmdb.api_key = Config.tmdbApiKey
|
||||||
|
tmdb.language = Config.tmdbLanguage
|
||||||
|
|
||||||
|
# Connect to Plex Server
|
||||||
|
baseurl = Config.plexBaseUrl
|
||||||
|
token = Config.plexToken
|
||||||
|
plex = PlexServer(baseurl, token)
|
||||||
|
|
||||||
|
plexMoviesLibrary = plex.library.section('Movies')
|
||||||
|
|
||||||
|
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():
|
||||||
|
movie = UpdatedMovie()
|
||||||
|
tmdbMovie = Movie()
|
||||||
|
|
||||||
|
movie.PlexTitle = item.title
|
||||||
|
for guid in item.guids:
|
||||||
|
if "tmdb://" in guid.id:
|
||||||
|
movie.TMDbId = guid.id[7:]
|
||||||
|
elif "imdb://" in guid.id:
|
||||||
|
movie.ImdbId = guid.id[7:]
|
||||||
|
|
||||||
|
# Search for the movie on TMDb
|
||||||
|
try:
|
||||||
|
tmdbMovieDetails = tmdbMovie.details(movie_id=movie.TMDbId)
|
||||||
|
except:
|
||||||
|
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: 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]
|
||||||
|
movie.MovieTMDbCollectionTMDbId = tmdbMovieDetails['belongs_to_collection'].id
|
||||||
|
movie.MovieTMDbCollectionPosterPath = tmdbMovieDetails['belongs_to_collection'].poster_path
|
||||||
|
|
||||||
|
movie.TMDbTitle = tmdbMovieDetails.title
|
||||||
|
|
||||||
|
# Add movie to the list of Movies
|
||||||
|
if movie.MovieTMDbCollectionTMDbId:
|
||||||
|
print(f":: {dt.now().strftime('%d-%m-%Y %H:%M:%S')} :: CollectionUpdater :: Adding movie {movie.TMDbTitle} from collection {movie.MovieTMDbCollectionName} to the list.")
|
||||||
|
MoviesList.append(movie)
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
count = 0
|
||||||
|
MovieListFinal = []
|
||||||
|
ExistingCollectionId = []
|
||||||
|
|
||||||
|
while i < len(MoviesList):
|
||||||
|
for movie in MoviesList:
|
||||||
|
if MoviesList[i].MovieTMDbCollectionTMDbId == movie.MovieTMDbCollectionTMDbId:
|
||||||
|
count += 1
|
||||||
|
if count > 2:
|
||||||
|
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:
|
||||||
|
with open('test.yaml', '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 MovieListFinal:
|
||||||
|
if collection.MovieTMDbCollectionName not in currentYamlCollections:
|
||||||
|
with open('test.yaml', 'r') as f:
|
||||||
|
new_coll = {f'{collection.MovieTMDbCollectionName}': {'sync_mode': 'sync', 'tmdb_collection': collection.MovieTMDbCollectionTMDbId, 'tmdb_summary': collection.MovieTMDbCollectionTMDbId}}
|
||||||
|
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...")
|
||||||
|
with open('test.yaml', '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}...")
|
||||||
|
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...")
|
Loading…
x
Reference in New Issue
Block a user