2021-02-24 06:42:58 +00:00
|
|
|
import logging, os, re, requests
|
|
|
|
from datetime import datetime, timedelta
|
2021-01-20 21:37:59 +00:00
|
|
|
from modules import util
|
|
|
|
from modules.util import Failed
|
|
|
|
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
|
2021-02-28 01:18:05 +00:00
|
|
|
from plexapi.library import MovieSection, ShowSection
|
|
|
|
from plexapi.collection import Collections
|
2021-01-20 21:37:59 +00:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
from plexapi.video import Movie, Show
|
|
|
|
from retrying import retry
|
|
|
|
from ruamel import yaml
|
|
|
|
|
|
|
|
logger = logging.getLogger("Plex Meta Manager")
|
|
|
|
|
2021-03-30 05:50:53 +00:00
|
|
|
builders = ["plex_all", "plex_collection", "plex_collectionless", "plex_search",]
|
|
|
|
search_translation = {
|
|
|
|
"audio_language": "audioLanguage",
|
|
|
|
"content_rating": "contentRating",
|
|
|
|
"subtitle_language": "subtitleLanguage",
|
|
|
|
"added": "addedAt",
|
|
|
|
"originally_available": "originallyAvailableAt",
|
|
|
|
"rating": "userRating"
|
|
|
|
}
|
|
|
|
episode_sorting_options = {"default": "-1", "oldest": "0", "newest": "1"}
|
|
|
|
keep_episodes_options = {"all": 0, "5_latest": 5, "3_latest": 3, "latest": 1, "past_3": -3, "past_7": -7, "past_30": -30}
|
|
|
|
delete_episodes_options = {"never": 0, "day": 1, "week": 7, "refresh": 100}
|
|
|
|
season_display_options = {"default": -1, "show": 0, "hide": 1}
|
|
|
|
episode_ordering_options = {"default": None, "tmdb_aired": "tmdbAiring", "tvdb_aired": "airing", "tvdb_dvd": "dvd", "tvdb_absolute": "absolute"}
|
|
|
|
plex_languages = ["default", "ar-SA", "ca-ES", "cs-CZ", "da-DK", "de-DE", "el-GR", "en-AU", "en-CA", "en-GB", "en-US",
|
|
|
|
"es-ES", "es-MX", "et-EE", "fa-IR", "fi-FI", "fr-CA", "fr-FR", "he-IL", "hi-IN", "hu-HU", "id-ID",
|
|
|
|
"it-IT", "ja-JP", "ko-KR", "lt-LT", "lv-LV", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO",
|
|
|
|
"ru-RU", "sk-SK", "sv-SE", "th-TH", "tr-TR", "uk-UA", "vi-VN", "zh-CN", "zh-HK", "zh-TW"]
|
|
|
|
metadata_language_options = {lang.lower(): lang for lang in plex_languages}
|
|
|
|
metadata_language_options["default"] = None
|
|
|
|
filter_alias = {
|
|
|
|
"actor": "actors",
|
|
|
|
"collection": "collections",
|
|
|
|
"content_rating": "contentRating",
|
|
|
|
"country": "countries",
|
|
|
|
"director": "directors",
|
|
|
|
"genre": "genres",
|
|
|
|
"originally_available": "originallyAvailableAt",
|
|
|
|
"tmdb_vote_count": "vote_count",
|
|
|
|
"writer": "writers"
|
|
|
|
}
|
|
|
|
searches = [
|
|
|
|
"title", "title.and", "title.not", "title.begins", "title.ends",
|
|
|
|
"studio", "studio.and", "studio.not", "studio.begins", "studio.ends",
|
|
|
|
"actor", "actor.and", "actor.not",
|
|
|
|
"audio_language", "audio_language.and", "audio_language.not",
|
|
|
|
"collection", "collection.and", "collection.not",
|
|
|
|
"content_rating", "content_rating.and", "content_rating.not",
|
|
|
|
"country", "country.and", "country.not",
|
|
|
|
"director", "director.and", "director.not",
|
|
|
|
"genre", "genre.and", "genre.not",
|
|
|
|
"label", "label.and", "label.not",
|
|
|
|
"producer", "producer.and", "producer.not",
|
|
|
|
"subtitle_language", "subtitle_language.and", "subtitle_language.not",
|
|
|
|
"writer", "writer.and", "writer.not",
|
|
|
|
"decade", "resolution",
|
|
|
|
"added.before", "added.after",
|
|
|
|
"originally_available.before", "originally_available.after",
|
|
|
|
"duration.greater", "duration.less",
|
|
|
|
"rating.greater", "rating.less",
|
|
|
|
"year", "year.not", "year.greater", "year.less"
|
|
|
|
]
|
|
|
|
movie_only_searches = [
|
|
|
|
"audio_language", "audio_language.and", "audio_language.not",
|
|
|
|
"country", "country.and", "country.not",
|
|
|
|
"subtitle_language", "subtitle_language.and", "subtitle_language.not",
|
|
|
|
"decade", "resolution",
|
|
|
|
"originally_available.before", "originally_available.after",
|
|
|
|
"duration.greater", "duration.less"
|
|
|
|
]
|
|
|
|
tmdb_searches = [
|
|
|
|
"actor", "actor.and", "actor.not",
|
|
|
|
"director", "director.and", "director.not",
|
|
|
|
"producer", "producer.and", "producer.not",
|
|
|
|
"writer", "writer.and", "writer.not"
|
|
|
|
]
|
|
|
|
sorts = {
|
|
|
|
"title.asc": "titleSort:asc", "title.desc": "titleSort:desc",
|
|
|
|
"originally_available.asc": "originallyAvailableAt:asc", "originally_available.desc": "originallyAvailableAt:desc",
|
|
|
|
"critic_rating.asc": "rating:asc", "critic_rating.desc": "rating:desc",
|
|
|
|
"audience_rating.asc": "audienceRating:asc", "audience_rating.desc": "audienceRating:desc",
|
|
|
|
"duration.asc": "duration:asc", "duration.desc": "duration:desc",
|
|
|
|
"added.asc": "addedAt:asc", "added.desc": "addedAt:desc"
|
|
|
|
}
|
|
|
|
modifiers = {
|
|
|
|
".and": "&",
|
|
|
|
".not": "!",
|
|
|
|
".begins": "<",
|
|
|
|
".ends": ">",
|
|
|
|
".before": "<<",
|
|
|
|
".after": ">>",
|
|
|
|
".greater": ">>",
|
|
|
|
".less": "<<"
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
class PlexAPI:
|
2021-02-21 17:01:10 +00:00
|
|
|
def __init__(self, params, TMDb, TVDb):
|
2021-03-26 05:43:11 +00:00
|
|
|
try:
|
|
|
|
self.PlexServer = PlexServer(params["plex"]["url"], params["plex"]["token"], timeout=params["plex"]["timeout"])
|
|
|
|
except Unauthorized:
|
|
|
|
raise Failed("Plex Error: Plex token is invalid")
|
|
|
|
except ValueError as e:
|
|
|
|
raise Failed(f"Plex Error: {e}")
|
2021-02-24 06:42:58 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
2021-01-20 21:37:59 +00:00
|
|
|
util.print_stacktrace()
|
|
|
|
raise Failed("Plex Error: Plex url is invalid")
|
|
|
|
self.is_movie = params["library_type"] == "movie"
|
|
|
|
self.is_show = params["library_type"] == "show"
|
|
|
|
self.Plex = next((s for s in self.PlexServer.library.sections() if s.title == params["name"] and ((self.is_movie and isinstance(s, MovieSection)) or (self.is_show and isinstance(s, ShowSection)))), None)
|
2021-03-26 05:43:11 +00:00
|
|
|
if not self.Plex:
|
|
|
|
raise Failed(f"Plex Error: Plex Library {params['name']} not found")
|
2021-03-31 04:20:20 +00:00
|
|
|
|
2021-03-31 00:33:08 +00:00
|
|
|
logger.info(f"Using Metadata File: {params['metadata_path']}")
|
2021-03-26 05:43:11 +00:00
|
|
|
try:
|
|
|
|
self.data, ind, bsi = yaml.util.load_yaml_guess_indent(open(params["metadata_path"], encoding="utf-8"))
|
2021-03-30 05:49:10 +00:00
|
|
|
except yaml.scanner.ScannerError as ye:
|
|
|
|
raise Failed(f"YAML Error: {util.tab_new_lines(ye)}")
|
|
|
|
except Exception as e:
|
|
|
|
util.print_stacktrace()
|
|
|
|
raise Failed(f"YAML Error: {e}")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-20 05:41:45 +00:00
|
|
|
def get_dict(attribute):
|
|
|
|
if attribute in self.data:
|
|
|
|
if self.data[attribute]:
|
2021-03-26 05:43:11 +00:00
|
|
|
if isinstance(self.data[attribute], dict):
|
|
|
|
return self.data[attribute]
|
|
|
|
else:
|
|
|
|
logger.warning(f"Config Warning: {attribute} must be a dictionary")
|
|
|
|
else:
|
|
|
|
logger.warning(f"Config Warning: {attribute} attribute is blank")
|
2021-02-20 05:41:45 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
self.metadata = get_dict("metadata")
|
|
|
|
self.templates = get_dict("templates")
|
|
|
|
self.collections = get_dict("collections")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
|
|
|
if self.metadata is None and self.collections is None:
|
|
|
|
raise Failed("YAML Error: metadata attributes or collections attribute required")
|
|
|
|
|
|
|
|
if params["asset_directory"]:
|
2021-02-21 08:13:07 +00:00
|
|
|
for ad in params["asset_directory"]:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.info(f"Using Asset Directory: {ad}")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-21 08:13:07 +00:00
|
|
|
self.TMDb = TMDb
|
|
|
|
self.TVDb = TVDb
|
2021-02-21 17:01:10 +00:00
|
|
|
self.Radarr = None
|
|
|
|
self.Sonarr = None
|
|
|
|
self.Tautulli = None
|
2021-01-20 21:37:59 +00:00
|
|
|
self.name = params["name"]
|
2021-02-24 06:44:06 +00:00
|
|
|
self.missing_path = os.path.join(os.path.dirname(os.path.abspath(params["metadata_path"])), f"{os.path.splitext(os.path.basename(params['metadata_path']))[0]}_missing.yml")
|
2021-01-20 21:37:59 +00:00
|
|
|
self.metadata_path = params["metadata_path"]
|
|
|
|
self.asset_directory = params["asset_directory"]
|
|
|
|
self.sync_mode = params["sync_mode"]
|
2021-02-13 05:26:40 +00:00
|
|
|
self.show_unmanaged = params["show_unmanaged"]
|
|
|
|
self.show_filtered = params["show_filtered"]
|
2021-02-20 06:41:40 +00:00
|
|
|
self.show_missing = params["show_missing"]
|
|
|
|
self.save_missing = params["save_missing"]
|
2021-03-05 16:04:28 +00:00
|
|
|
self.mass_genre_update = params["mass_genre_update"]
|
2021-01-20 21:37:59 +00:00
|
|
|
self.plex = params["plex"]
|
2021-02-22 01:56:31 +00:00
|
|
|
self.timeout = params["plex"]["timeout"]
|
2021-02-16 21:51:38 +00:00
|
|
|
self.missing = {}
|
2021-03-01 20:59:10 +00:00
|
|
|
self.run_again = []
|
2021-01-20 21:37:59 +00:00
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=10000)
|
|
|
|
def search(self, title, libtype=None, year=None):
|
|
|
|
if libtype is not None and year is not None: return self.Plex.search(title=title, year=year, libtype=libtype)
|
|
|
|
elif libtype is not None: return self.Plex.search(title=title, libtype=libtype)
|
|
|
|
elif year is not None: return self.Plex.search(title=title, year=year)
|
|
|
|
else: return self.Plex.search(title=title)
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=10000)
|
|
|
|
def fetchItem(self, data):
|
|
|
|
return self.PlexServer.fetchItem(data)
|
|
|
|
|
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=10000)
|
|
|
|
def server_search(self, data):
|
|
|
|
return self.PlexServer.search(data)
|
|
|
|
|
2021-03-26 05:43:11 +00:00
|
|
|
def get_search_choices(self, search_name, key=False):
|
|
|
|
if key: return {c.key.lower(): c.key for c in self.Plex.listFilterChoices(search_name)}
|
|
|
|
else: return {c.title.lower(): c.title for c in self.Plex.listFilterChoices(search_name)}
|
|
|
|
|
|
|
|
def validate_search_list(self, data, search_name):
|
2021-03-30 05:50:53 +00:00
|
|
|
final_search = search_translation[search_name] if search_name in search_translation else search_name
|
2021-03-26 05:43:11 +00:00
|
|
|
search_choices = self.get_search_choices(final_search, key=final_search.endswith("Language"))
|
|
|
|
valid_list = []
|
|
|
|
for value in util.get_list(data):
|
2021-03-26 22:17:24 +00:00
|
|
|
if str(value).lower() in search_choices:
|
|
|
|
valid_list.append(search_choices[str(value).lower()])
|
2021-03-26 05:43:11 +00:00
|
|
|
else:
|
2021-03-30 20:50:34 +00:00
|
|
|
logger.error(f"Plex Error: {search_name}: {value} not found")
|
2021-03-26 05:43:11 +00:00
|
|
|
return valid_list
|
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
def get_all_collections(self):
|
|
|
|
return self.Plex.search(libtype="collection")
|
|
|
|
|
|
|
|
def get_collection(self, data):
|
|
|
|
collection = util.choose_from_list(self.search(str(data), libtype="collection"), "collection", str(data), exact=True)
|
|
|
|
if collection: return collection
|
2021-02-24 06:44:06 +00:00
|
|
|
else: raise Failed(f"Plex Error: Collection {data} not found")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
|
|
|
def validate_collections(self, collections):
|
|
|
|
valid_collections = []
|
|
|
|
for collection in collections:
|
|
|
|
try: valid_collections.append(self.get_collection(collection))
|
|
|
|
except Failed as e: logger.error(e)
|
|
|
|
if len(valid_collections) == 0:
|
2021-02-24 06:44:06 +00:00
|
|
|
raise Failed(f"Collection Error: No valid Plex Collections in {collections}")
|
2021-01-20 21:37:59 +00:00
|
|
|
return valid_collections
|
|
|
|
|
2021-02-16 21:51:38 +00:00
|
|
|
def add_missing(self, collection, items, is_movie):
|
|
|
|
col_name = collection.encode("ascii", "replace").decode()
|
|
|
|
if col_name not in self.missing:
|
|
|
|
self.missing[col_name] = {}
|
|
|
|
section = "Movies Missing (TMDb IDs)" if is_movie else "Shows Missing (TVDb IDs)"
|
|
|
|
if section not in self.missing[col_name]:
|
|
|
|
self.missing[col_name][section] = {}
|
|
|
|
for title, item_id in items:
|
|
|
|
self.missing[col_name][section][int(item_id)] = str(title).encode("ascii", "replace").decode()
|
|
|
|
with open(self.missing_path, "w"): pass
|
2021-01-20 21:37:59 +00:00
|
|
|
try:
|
2021-02-16 21:51:38 +00:00
|
|
|
yaml.round_trip_dump(self.missing, open(self.missing_path, "w"))
|
2021-01-20 21:37:59 +00:00
|
|
|
except yaml.scanner.ScannerError as e:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"YAML Error: {util.tab_new_lines(e)}")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-24 06:42:58 +00:00
|
|
|
def add_to_collection(self, collection, items, filters, show_filtered, rating_key_map, movie_map, show_map):
|
2021-01-20 21:37:59 +00:00
|
|
|
name = collection.title if isinstance(collection, Collections) else collection
|
2021-01-26 06:25:05 +00:00
|
|
|
collection_items = collection.items() if isinstance(collection, Collections) else []
|
2021-01-20 21:37:59 +00:00
|
|
|
total = len(items)
|
|
|
|
max_length = len(str(total))
|
|
|
|
length = 0
|
|
|
|
for i, item in enumerate(items, 1):
|
2021-02-20 05:41:45 +00:00
|
|
|
try:
|
|
|
|
current = self.fetchItem(item.ratingKey if isinstance(item, (Movie, Show)) else int(item))
|
2021-03-15 18:11:44 +00:00
|
|
|
if not isinstance(current, (Movie, Show)):
|
|
|
|
raise NotFound
|
2021-02-15 06:51:15 +00:00
|
|
|
except (BadRequest, NotFound):
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Plex Error: Item {item} not found")
|
2021-02-15 06:51:15 +00:00
|
|
|
continue
|
2021-01-20 21:37:59 +00:00
|
|
|
match = True
|
|
|
|
if filters:
|
2021-02-24 06:44:06 +00:00
|
|
|
length = util.print_return(length, f"Filtering {(' ' * (max_length - len(str(i)))) + str(i)}/{total} {current.title}")
|
2021-02-28 08:36:40 +00:00
|
|
|
for filter_method, filter_data in filters:
|
|
|
|
modifier = filter_method[-4:]
|
2021-03-11 20:03:32 +00:00
|
|
|
method = filter_method[:-4] if modifier in [".not", ".lte", ".gte"] else filter_method
|
2021-03-30 05:50:53 +00:00
|
|
|
method_name = filter_alias[method] if method in filter_alias else method
|
2021-03-11 20:03:32 +00:00
|
|
|
if method_name == "max_age":
|
2021-02-28 08:36:40 +00:00
|
|
|
threshold_date = datetime.now() - timedelta(days=filter_data)
|
2021-02-25 21:59:56 +00:00
|
|
|
if current.originallyAvailableAt is None or current.originallyAvailableAt < threshold_date:
|
2021-01-20 21:37:59 +00:00
|
|
|
match = False
|
|
|
|
break
|
2021-03-11 20:03:32 +00:00
|
|
|
elif method_name == "original_language":
|
2021-02-12 15:37:04 +00:00
|
|
|
movie = None
|
|
|
|
for key, value in movie_map.items():
|
|
|
|
if current.ratingKey == value:
|
|
|
|
try:
|
|
|
|
movie = self.TMDb.get_movie(key)
|
|
|
|
break
|
|
|
|
except Failed:
|
|
|
|
pass
|
|
|
|
if movie is None:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.warning(f"Filter Error: No TMDb ID found for {current.title}")
|
2021-02-12 15:37:04 +00:00
|
|
|
continue
|
2021-02-28 08:36:40 +00:00
|
|
|
if (modifier == ".not" and movie.original_language in filter_data) or (modifier != ".not" and movie.original_language not in filter_data):
|
2021-02-12 15:37:04 +00:00
|
|
|
match = False
|
|
|
|
break
|
2021-03-11 20:03:32 +00:00
|
|
|
elif method_name == "audio_track_title":
|
2021-03-11 05:51:01 +00:00
|
|
|
jailbreak = False
|
2021-03-10 23:21:41 +00:00
|
|
|
for media in current.media:
|
|
|
|
for part in media.parts:
|
|
|
|
for audio in part.audioStreams():
|
|
|
|
for check_title in filter_data:
|
2021-03-11 05:51:01 +00:00
|
|
|
title = audio.title if audio.title else ""
|
|
|
|
if check_title.lower() in title.lower():
|
|
|
|
jailbreak = True
|
2021-03-10 23:21:41 +00:00
|
|
|
break
|
2021-03-11 05:51:01 +00:00
|
|
|
if jailbreak: break
|
|
|
|
if jailbreak: break
|
|
|
|
if jailbreak: break
|
|
|
|
if (jailbreak and modifier == ".not") or (not jailbreak and modifier != ".not"):
|
|
|
|
match = False
|
|
|
|
break
|
2021-01-20 21:37:59 +00:00
|
|
|
elif modifier in [".gte", ".lte"]:
|
2021-03-11 20:03:32 +00:00
|
|
|
if method_name == "vote_count":
|
2021-02-28 08:36:40 +00:00
|
|
|
tmdb_item = None
|
|
|
|
for key, value in movie_map.items():
|
|
|
|
if current.ratingKey == value:
|
|
|
|
try:
|
|
|
|
tmdb_item = self.TMDb.get_movie(key) if self.is_movie else self.TMDb.get_show(key)
|
|
|
|
break
|
|
|
|
except Failed:
|
|
|
|
pass
|
|
|
|
if tmdb_item is None:
|
|
|
|
logger.warning(f"Filter Error: No TMDb ID found for {current.title}")
|
|
|
|
continue
|
|
|
|
attr = tmdb_item.vote_count
|
|
|
|
else:
|
2021-03-11 20:03:32 +00:00
|
|
|
attr = getattr(current, method_name) / 60000 if method_name == "duration" else getattr(current, method_name)
|
2021-03-28 19:24:02 +00:00
|
|
|
if attr is None:
|
|
|
|
attr = 0
|
2021-02-28 08:36:40 +00:00
|
|
|
if (modifier == ".lte" and attr > filter_data) or (modifier == ".gte" and attr < filter_data):
|
2021-02-25 21:59:56 +00:00
|
|
|
match = False
|
|
|
|
break
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-02-24 06:42:58 +00:00
|
|
|
attrs = []
|
2021-03-11 20:03:32 +00:00
|
|
|
if method_name in ["video_resolution", "audio_language", "subtitle_language"]:
|
2021-01-20 21:37:59 +00:00
|
|
|
for media in current.media:
|
2021-03-21 23:39:09 +00:00
|
|
|
if method_name == "video_resolution":
|
|
|
|
attrs.extend([media.videoResolution])
|
2021-01-20 21:37:59 +00:00
|
|
|
for part in media.parts:
|
2021-03-21 23:39:09 +00:00
|
|
|
if method_name == "audio_language":
|
|
|
|
attrs.extend([a.language for a in part.audioStreams()])
|
|
|
|
if method_name == "subtitle_language":
|
|
|
|
attrs.extend([s.language for s in part.subtitleStreams()])
|
|
|
|
elif method_name in ["contentRating", "studio", "year", "rating", "originallyAvailableAt"]:
|
|
|
|
attrs = [str(getattr(current, method_name))]
|
|
|
|
elif method_name in ["actors", "countries", "directors", "genres", "writers", "collections"]:
|
|
|
|
attrs = [getattr(x, "tag") for x in getattr(current, method_name)]
|
|
|
|
else:
|
|
|
|
raise Failed(f"Filter Error: filter: {method_name} not supported")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-28 08:36:40 +00:00
|
|
|
if (not list(set(filter_data) & set(attrs)) and modifier != ".not") or (list(set(filter_data) & set(attrs)) and modifier == ".not"):
|
2021-01-20 21:37:59 +00:00
|
|
|
match = False
|
|
|
|
break
|
2021-02-24 06:44:06 +00:00
|
|
|
length = util.print_return(length, f"Filtering {(' ' * (max_length - len(str(i)))) + str(i)}/{total} {current.title}")
|
2021-01-20 21:37:59 +00:00
|
|
|
if match:
|
2021-02-24 06:44:06 +00:00
|
|
|
util.print_end(length, f"{name} Collection | {'=' if current in collection_items else '+'} | {current.title}")
|
2021-02-24 06:42:58 +00:00
|
|
|
if current in collection_items: rating_key_map[current.ratingKey] = None
|
2021-01-20 21:37:59 +00:00
|
|
|
else: current.addCollection(name)
|
2021-02-13 04:28:46 +00:00
|
|
|
elif show_filtered is True:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.info(f"{name} Collection | X | {current.title}")
|
|
|
|
media_type = f"{'Movie' if self.is_movie else 'Show'}{'s' if total > 1 else ''}"
|
|
|
|
util.print_end(length, f"{total} {media_type} Processed")
|
2021-02-24 06:42:58 +00:00
|
|
|
return rating_key_map
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-05 14:56:05 +00:00
|
|
|
def search_item(self, data, year=None):
|
|
|
|
return util.choose_from_list(self.search(data, year=year), "movie" if self.is_movie else "show", str(data), exact=True)
|
|
|
|
|
2021-02-15 06:51:15 +00:00
|
|
|
def update_metadata(self, TMDb, test):
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
2021-02-24 06:44:06 +00:00
|
|
|
util.separator(f"{self.name} Library Metadata")
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
|
|
|
if not self.metadata:
|
|
|
|
raise Failed("No metadata to edit")
|
2021-03-25 15:07:54 +00:00
|
|
|
for mapping_name, meta in self.metadata.items():
|
|
|
|
methods = {mm.lower(): mm for mm in meta}
|
|
|
|
if test and ("test" not in methods or meta[methods["test"]] is not True):
|
2021-02-15 06:51:15 +00:00
|
|
|
continue
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
2021-02-24 06:42:58 +00:00
|
|
|
util.separator()
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
|
|
|
year = None
|
2021-03-22 03:38:10 +00:00
|
|
|
if "year" in methods:
|
2021-03-25 15:07:54 +00:00
|
|
|
year = util.check_number(meta[methods["year"]], "year", minimum=1800, maximum=datetime.now().year + 1)
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-03-25 15:07:54 +00:00
|
|
|
title = mapping_name
|
2021-03-22 03:38:10 +00:00
|
|
|
if "title" in methods:
|
2021-03-25 15:07:54 +00:00
|
|
|
if meta[methods["title"]] is None: logger.error("Metadata Error: title attribute is blank")
|
|
|
|
else: title = meta[methods["title"]]
|
2021-02-05 14:56:05 +00:00
|
|
|
|
|
|
|
item = self.search_item(title, year=year)
|
|
|
|
|
|
|
|
if item is None:
|
2021-02-24 06:44:06 +00:00
|
|
|
item = self.search_item(f"{title} (SUB)", year=year)
|
2021-02-05 14:56:05 +00:00
|
|
|
|
2021-03-22 03:38:10 +00:00
|
|
|
if item is None and "alt_title" in methods:
|
2021-03-25 15:07:54 +00:00
|
|
|
if meta[methods["alt_title"]] is None:
|
2021-02-05 14:56:05 +00:00
|
|
|
logger.error("Metadata Error: alt_title attribute is blank")
|
|
|
|
else:
|
2021-03-25 15:07:54 +00:00
|
|
|
alt_title = meta["alt_title"]
|
2021-02-05 14:56:05 +00:00
|
|
|
item = self.search_item(alt_title, year=year)
|
|
|
|
|
|
|
|
if item is None:
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.error(f"Plex Error: Item {mapping_name} not found")
|
|
|
|
logger.error(f"Skipping {mapping_name}")
|
2021-02-05 14:56:05 +00:00
|
|
|
continue
|
|
|
|
|
2021-02-24 06:44:06 +00:00
|
|
|
item_type = "Movie" if self.is_movie else "Show"
|
|
|
|
logger.info(f"Updating {item_type}: {title}...")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-05 14:56:05 +00:00
|
|
|
tmdb_item = None
|
2021-01-20 21:37:59 +00:00
|
|
|
try:
|
2021-03-22 03:38:10 +00:00
|
|
|
if "tmdb_id" in methods:
|
2021-03-25 15:07:54 +00:00
|
|
|
if meta[methods["tmdb_id"]] is None: logger.error("Metadata Error: tmdb_id attribute is blank")
|
|
|
|
elif self.is_show: logger.error("Metadata Error: tmdb_id attribute only works with movie libraries")
|
|
|
|
else: tmdb_item = TMDb.get_show(util.regex_first_int(meta[methods["tmdb_id"]], "Show"))
|
2021-01-20 21:37:59 +00:00
|
|
|
except Failed as e:
|
2021-02-05 14:56:05 +00:00
|
|
|
logger.error(e)
|
|
|
|
|
|
|
|
originally_available = tmdb_item.first_air_date if tmdb_item else None
|
|
|
|
rating = tmdb_item.vote_average if tmdb_item else None
|
|
|
|
original_title = tmdb_item.original_name if tmdb_item and tmdb_item.original_name != tmdb_item.name else None
|
|
|
|
studio = tmdb_item.networks[0].name if tmdb_item else None
|
|
|
|
tagline = tmdb_item.tagline if tmdb_item and len(tmdb_item.tagline) > 0 else None
|
|
|
|
summary = tmdb_item.overview if tmdb_item else None
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-03-30 05:49:10 +00:00
|
|
|
updated = False
|
2021-03-27 21:51:35 +00:00
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
edits = {}
|
2021-03-22 03:38:10 +00:00
|
|
|
def add_edit(name, current, group, alias, key=None, value=None):
|
|
|
|
if value or name in alias:
|
|
|
|
if value or group[alias[name]]:
|
2021-01-20 21:37:59 +00:00
|
|
|
if key is None: key = name
|
2021-03-22 03:38:10 +00:00
|
|
|
if value is None: value = group[alias[name]]
|
2021-02-05 14:56:05 +00:00
|
|
|
if str(current) != str(value):
|
2021-02-24 06:44:06 +00:00
|
|
|
edits[f"{key}.value"] = value
|
|
|
|
edits[f"{key}.locked"] = 1
|
2021-03-01 04:16:08 +00:00
|
|
|
logger.info(f"Detail: {name} updated to {value}")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Metadata Error: {name} attribute is blank")
|
2021-03-25 15:07:54 +00:00
|
|
|
add_edit("title", item.title, meta, methods, value=title)
|
|
|
|
add_edit("sort_title", item.titleSort, meta, methods, key="titleSort")
|
|
|
|
add_edit("originally_available", str(item.originallyAvailableAt)[:-9], meta, methods, key="originallyAvailableAt", value=originally_available)
|
|
|
|
add_edit("rating", item.rating, meta, methods, value=rating)
|
|
|
|
add_edit("content_rating", item.contentRating, meta, methods, key="contentRating")
|
|
|
|
add_edit("original_title", item.originalTitle, meta, methods, key="originalTitle", value=original_title)
|
|
|
|
add_edit("studio", item.studio, meta, methods, value=studio)
|
|
|
|
add_edit("tagline", item.tagline, meta, methods, value=tagline)
|
|
|
|
add_edit("summary", item.summary, meta, methods, value=summary)
|
2021-02-05 14:56:05 +00:00
|
|
|
if len(edits) > 0:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.debug(f"Details Update: {edits}")
|
2021-03-30 05:49:10 +00:00
|
|
|
updated = True
|
2021-02-05 14:56:05 +00:00
|
|
|
try:
|
|
|
|
item.edit(**edits)
|
|
|
|
item.reload()
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.info(f"{item_type}: {mapping_name} Details Update Successful")
|
2021-02-05 14:56:05 +00:00
|
|
|
except BadRequest:
|
|
|
|
util.print_stacktrace()
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.error(f"{item_type}: {mapping_name} Details Update Failed")
|
2021-02-05 14:56:05 +00:00
|
|
|
|
2021-03-24 01:35:39 +00:00
|
|
|
advance_edits = {}
|
2021-03-30 05:49:10 +00:00
|
|
|
def add_advanced_edit(attr, options, key=None, show_library=False):
|
|
|
|
if key is None:
|
|
|
|
key = attr
|
|
|
|
if show_library and not self.is_show:
|
|
|
|
logger.error(f"Metadata Error: {attr} attribute only works for show libraries")
|
|
|
|
elif attr in methods:
|
|
|
|
if meta[methods[attr]]:
|
|
|
|
method_data = str(meta[methods[attr]]).lower()
|
|
|
|
if method_data in options and getattr(item, key) != options[method_data]:
|
|
|
|
advance_edits[key] = options[method_data]
|
|
|
|
logger.info(f"Detail: {attr} updated to {method_data}")
|
2021-03-22 06:50:37 +00:00
|
|
|
else:
|
2021-03-30 05:49:10 +00:00
|
|
|
logger.error(f"Metadata Error: {meta[methods[attr]]} {attr} attribute invalid")
|
2021-03-22 06:50:37 +00:00
|
|
|
else:
|
2021-03-30 05:49:10 +00:00
|
|
|
logger.error(f"Metadata Error: {attr} attribute is blank")
|
|
|
|
|
|
|
|
add_advanced_edit("episode_sorting", episode_sorting_options, key="episodeSort", show_library=True)
|
|
|
|
add_advanced_edit("keep_episodes", keep_episodes_options, key="autoDeletionItemPolicyUnwatchedLibrary", show_library=True)
|
|
|
|
add_advanced_edit("delete_episodes", delete_episodes_options, key="autoDeletionItemPolicyWatchedLibrary", show_library=True)
|
|
|
|
add_advanced_edit("season_display", season_display_options, key="flattenSeasons", show_library=True)
|
|
|
|
add_advanced_edit("episode_ordering", episode_ordering_options, key="showOrdering", show_library=True)
|
|
|
|
add_advanced_edit("metadata_language", metadata_language_options, key="languageOverride")
|
|
|
|
|
|
|
|
use_original_title_options = {"default": -1, "no": 0, "yes": 1}
|
|
|
|
add_advanced_edit("use_original_title", use_original_title_options, key="useOriginalTitle")
|
2021-03-22 06:50:37 +00:00
|
|
|
|
|
|
|
if len(advance_edits) > 0:
|
|
|
|
logger.debug(f"Details Update: {advance_edits}")
|
2021-03-30 05:49:10 +00:00
|
|
|
updated = True
|
2021-03-22 06:50:37 +00:00
|
|
|
try:
|
2021-03-24 15:24:59 +00:00
|
|
|
check_dict = {pref.id: list(pref.enumValues.keys()) for pref in item.preferences()}
|
|
|
|
logger.info(check_dict)
|
2021-03-22 06:50:37 +00:00
|
|
|
item.editAdvanced(**advance_edits)
|
|
|
|
item.reload()
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.info(f"{item_type}: {mapping_name} Advanced Details Update Successful")
|
2021-03-22 06:50:37 +00:00
|
|
|
except BadRequest:
|
|
|
|
util.print_stacktrace()
|
2021-03-27 21:51:35 +00:00
|
|
|
logger.error(f"{item_type}: {mapping_name} Advanced Details Update Failed")
|
2021-03-22 06:50:37 +00:00
|
|
|
|
2021-03-30 05:49:10 +00:00
|
|
|
def edit_tags(attr, obj, key=None, extra=None, movie_library=False):
|
|
|
|
if key is None:
|
|
|
|
key = f"{attr}s"
|
|
|
|
if attr in methods and f"{attr}.sync" in methods:
|
|
|
|
logger.error(f"Metadata Error: Cannot use {attr} and {attr}.sync together")
|
|
|
|
elif attr in methods or f"{attr}.sync" in methods:
|
|
|
|
attr_key = attr if attr in methods else f"{attr}.sync"
|
|
|
|
if movie_library and not self.is_movie:
|
|
|
|
logger.error(f"Metadata Error: {attr_key} attribute only works for movie libraries")
|
|
|
|
elif meta[methods[attr_key]] or extra:
|
|
|
|
item_tags = [item_tag.tag for item_tag in getattr(obj, key)]
|
|
|
|
input_tags = []
|
|
|
|
if meta[methods[attr_key]]:
|
|
|
|
input_tags.extend(util.get_list(meta[methods[attr_key]]))
|
|
|
|
if extra:
|
|
|
|
input_tags.extend(extra)
|
|
|
|
if f"{attr}.sync" in methods:
|
|
|
|
remove_method = getattr(obj, f"remove{attr.capitalize()}")
|
|
|
|
for tag in (t for t in item_tags if t not in input_tags):
|
|
|
|
updated = True
|
|
|
|
remove_method(tag)
|
|
|
|
logger.info(f"Detail: {attr.capitalize()} {tag} removed")
|
|
|
|
add_method = getattr(obj, f"add{attr.capitalize()}")
|
|
|
|
for tag in (t for t in input_tags if t not in item_tags):
|
|
|
|
updated = True
|
|
|
|
add_method(tag)
|
|
|
|
logger.info(f"Detail: {attr.capitalize()} {tag} added")
|
|
|
|
else:
|
|
|
|
logger.error(f"Metadata Error: {attr} attribute is blank")
|
|
|
|
|
|
|
|
genres = [genre.name for genre in tmdb_item.genres] if tmdb_item else []
|
|
|
|
edit_tags("genre", item, extra=genres)
|
|
|
|
edit_tags("label", item)
|
|
|
|
edit_tags("collection", item)
|
|
|
|
edit_tags("country", item, key="countries", movie_library=True)
|
|
|
|
edit_tags("director", item, movie_library=True)
|
|
|
|
edit_tags("producer", item, movie_library=True)
|
|
|
|
edit_tags("writer", item, movie_library=True)
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-03-22 03:38:10 +00:00
|
|
|
if "seasons" in methods and self.is_show:
|
2021-03-25 15:07:54 +00:00
|
|
|
if meta[methods["seasons"]]:
|
|
|
|
for season_id in meta[methods["seasons"]]:
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.info(f"Updating season {season_id} of {mapping_name}...")
|
2021-01-20 21:37:59 +00:00
|
|
|
if isinstance(season_id, int):
|
|
|
|
try: season = item.season(season_id)
|
2021-02-24 06:44:06 +00:00
|
|
|
except NotFound: logger.error(f"Metadata Error: Season: {season_id} not found")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-03-25 15:07:54 +00:00
|
|
|
season_dict = meta[methods["seasons"]][season_id]
|
2021-03-22 03:38:10 +00:00
|
|
|
season_methods = {sm.lower(): sm for sm in season_dict}
|
2021-02-05 14:56:05 +00:00
|
|
|
|
2021-03-22 03:38:10 +00:00
|
|
|
if "title" in season_methods and season_dict[season_methods["title"]]:
|
|
|
|
title = season_dict[season_methods["title"]]
|
2021-02-05 14:56:05 +00:00
|
|
|
else:
|
|
|
|
title = season.title
|
2021-03-22 03:38:10 +00:00
|
|
|
if "sub" in season_methods:
|
|
|
|
if season_dict[season_methods["sub"]] is None:
|
2021-02-05 14:56:05 +00:00
|
|
|
logger.error("Metadata Error: sub attribute is blank")
|
2021-03-22 03:38:10 +00:00
|
|
|
elif season_dict[season_methods["sub"]] is True and "(SUB)" not in title:
|
2021-02-24 06:44:06 +00:00
|
|
|
title = f"{title} (SUB)"
|
2021-03-22 03:38:10 +00:00
|
|
|
elif season_dict[season_methods["sub"]] is False and title.endswith(" (SUB)"):
|
2021-02-05 14:56:05 +00:00
|
|
|
title = title[:-6]
|
|
|
|
else:
|
|
|
|
logger.error("Metadata Error: sub attribute must be True or False")
|
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
edits = {}
|
2021-03-22 03:38:10 +00:00
|
|
|
add_edit("title", season.title, season_dict, season_methods, value=title)
|
|
|
|
add_edit("summary", season.summary, season_methods, season_dict)
|
2021-02-05 14:56:05 +00:00
|
|
|
if len(edits) > 0:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.debug(f"Season: {season_id} Details Update: {edits}")
|
2021-03-30 05:49:10 +00:00
|
|
|
updated = True
|
2021-02-05 14:56:05 +00:00
|
|
|
try:
|
|
|
|
season.edit(**edits)
|
|
|
|
season.reload()
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.info(f"Season: {season_id} Details Update Successful")
|
2021-02-05 14:56:05 +00:00
|
|
|
except BadRequest:
|
|
|
|
util.print_stacktrace()
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Season: {season_id} Details Update Failed")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Metadata Error: Season: {season_id} invalid, it must be an integer")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
|
|
|
logger.error("Metadata Error: seasons attribute is blank")
|
2021-03-30 05:49:10 +00:00
|
|
|
elif "seasons" in methods:
|
|
|
|
logger.error("Metadata Error: seasons attribute only works for show libraries")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-03-22 03:38:10 +00:00
|
|
|
if "episodes" in methods and self.is_show:
|
2021-03-25 15:07:54 +00:00
|
|
|
if meta[methods["episodes"]]:
|
|
|
|
for episode_str in meta[methods["episodes"]]:
|
2021-01-20 21:37:59 +00:00
|
|
|
logger.info("")
|
2021-02-24 06:42:58 +00:00
|
|
|
match = re.search("[Ss]\\d+[Ee]\\d+", episode_str)
|
2021-01-20 21:37:59 +00:00
|
|
|
if match:
|
2021-03-17 23:50:18 +00:00
|
|
|
output = match.group(0)[1:].split("E" if "E" in match.group(0) else "e")
|
2021-01-20 21:37:59 +00:00
|
|
|
episode_id = int(output[0])
|
|
|
|
season_id = int(output[1])
|
2021-03-25 15:07:54 +00:00
|
|
|
logger.info(f"Updating episode S{episode_id}E{season_id} of {mapping_name}...")
|
2021-01-20 21:37:59 +00:00
|
|
|
try: episode = item.episode(season=season_id, episode=episode_id)
|
2021-02-24 06:44:06 +00:00
|
|
|
except NotFound: logger.error(f"Metadata Error: episode {episode_id} of season {season_id} not found")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-03-25 15:07:54 +00:00
|
|
|
episode_dict = meta[methods["episodes"]][episode_str]
|
2021-03-22 03:38:10 +00:00
|
|
|
episode_methods = {em.lower(): em for em in episode_dict}
|
|
|
|
|
|
|
|
if "title" in episode_methods and episode_dict[episode_methods["title"]]:
|
|
|
|
title = episode_dict[episode_methods["title"]]
|
2021-02-05 14:56:05 +00:00
|
|
|
else:
|
|
|
|
title = episode.title
|
2021-03-22 03:38:10 +00:00
|
|
|
if "sub" in episode_dict:
|
|
|
|
if episode_dict[episode_methods["sub"]] is None:
|
2021-02-05 14:56:05 +00:00
|
|
|
logger.error("Metadata Error: sub attribute is blank")
|
2021-03-22 03:38:10 +00:00
|
|
|
elif episode_dict[episode_methods["sub"]] is True and "(SUB)" not in title:
|
2021-02-24 06:44:06 +00:00
|
|
|
title = f"{title} (SUB)"
|
2021-03-22 03:38:10 +00:00
|
|
|
elif episode_dict[episode_methods["sub"]] is False and title.endswith(" (SUB)"):
|
2021-02-05 14:56:05 +00:00
|
|
|
title = title[:-6]
|
|
|
|
else:
|
|
|
|
logger.error("Metadata Error: sub attribute must be True or False")
|
2021-01-20 21:37:59 +00:00
|
|
|
edits = {}
|
2021-03-22 03:38:10 +00:00
|
|
|
add_edit("title", episode.title, episode_dict, episode_methods, value=title)
|
|
|
|
add_edit("sort_title", episode.titleSort, episode_dict, episode_methods, key="titleSort")
|
|
|
|
add_edit("rating", episode.rating, episode_dict, episode_methods)
|
|
|
|
add_edit("originally_available", str(episode.originallyAvailableAt)[:-9], episode_dict, episode_methods, key="originallyAvailableAt")
|
|
|
|
add_edit("summary", episode.summary, episode_dict, episode_methods)
|
2021-02-05 14:56:05 +00:00
|
|
|
if len(edits) > 0:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.debug(f"Season: {season_id} Episode: {episode_id} Details Update: {edits}")
|
2021-03-30 05:49:10 +00:00
|
|
|
updated = True
|
2021-02-05 14:56:05 +00:00
|
|
|
try:
|
|
|
|
episode.edit(**edits)
|
|
|
|
episode.reload()
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.info(
|
|
|
|
f"Season: {season_id} Episode: {episode_id} Details Update Successful")
|
2021-02-05 14:56:05 +00:00
|
|
|
except BadRequest:
|
|
|
|
util.print_stacktrace()
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Season: {season_id} Episode: {episode_id} Details Update Failed")
|
2021-03-30 05:49:10 +00:00
|
|
|
edit_tags("director", episode)
|
|
|
|
edit_tags("writer", episode)
|
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Metadata Error: episode {episode_str} invalid must have S##E## format")
|
2021-01-20 21:37:59 +00:00
|
|
|
else:
|
|
|
|
logger.error("Metadata Error: episodes attribute is blank")
|
2021-03-30 05:49:10 +00:00
|
|
|
elif "episodes" in methods:
|
|
|
|
logger.error("Metadata Error: episodes attribute only works for show libraries")
|
2021-03-27 21:51:35 +00:00
|
|
|
|
2021-03-30 05:49:10 +00:00
|
|
|
if not updated:
|
2021-03-27 21:51:35 +00:00
|
|
|
logger.info(f"{item_type}: {mapping_name} Details Update Not Needed")
|