2022-02-13 16:33:57 +00:00
|
|
|
import os, shutil, time
|
2021-09-21 03:57:16 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from modules import util
|
2021-12-13 07:30:19 +00:00
|
|
|
from modules.meta import MetadataFile
|
2021-12-19 00:48:11 +00:00
|
|
|
from modules.util import Failed
|
2021-09-21 03:57:16 +00:00
|
|
|
from PIL import Image
|
2022-02-10 15:13:14 +00:00
|
|
|
from plexapi.exceptions import BadRequest
|
2021-09-21 03:57:16 +00:00
|
|
|
from ruamel import yaml
|
|
|
|
|
2022-02-13 16:33:57 +00:00
|
|
|
logger = util.logger
|
2021-09-21 03:57:16 +00:00
|
|
|
|
|
|
|
class Library(ABC):
|
|
|
|
def __init__(self, config, params):
|
|
|
|
self.Radarr = None
|
|
|
|
self.Sonarr = None
|
|
|
|
self.Tautulli = None
|
2021-11-03 14:36:11 +00:00
|
|
|
self.Webhooks = None
|
2021-10-04 17:51:32 +00:00
|
|
|
self.Notifiarr = None
|
2021-09-21 03:57:16 +00:00
|
|
|
self.collections = []
|
|
|
|
self.metadatas = []
|
|
|
|
self.metadata_files = []
|
|
|
|
self.missing = {}
|
|
|
|
self.movie_map = {}
|
|
|
|
self.show_map = {}
|
|
|
|
self.imdb_map = {}
|
|
|
|
self.anidb_map = {}
|
|
|
|
self.mal_map = {}
|
|
|
|
self.movie_rating_key_map = {}
|
|
|
|
self.show_rating_key_map = {}
|
|
|
|
self.run_again = []
|
|
|
|
self.run_sort = []
|
|
|
|
self.overlays = []
|
|
|
|
self.type = ""
|
|
|
|
self.config = config
|
|
|
|
self.name = params["name"]
|
|
|
|
self.original_mapping_name = params["mapping_name"]
|
|
|
|
self.metadata_path = params["metadata_path"]
|
2021-12-18 03:02:24 +00:00
|
|
|
self.skip_library = params["skip_library"]
|
2021-12-13 04:57:38 +00:00
|
|
|
self.asset_depth = params["asset_depth"]
|
2021-12-03 22:05:24 +00:00
|
|
|
self.asset_directory = params["asset_directory"] if params["asset_directory"] else []
|
2021-09-21 03:57:16 +00:00
|
|
|
self.default_dir = params["default_dir"]
|
|
|
|
self.mapping_name, output = util.validate_filename(self.original_mapping_name)
|
|
|
|
self.image_table_name = self.config.Cache.get_image_table_name(self.original_mapping_name) if self.config.Cache else None
|
2022-01-02 05:06:53 +00:00
|
|
|
self.missing_path = params["missing_path"] if params["missing_path"] else os.path.join(self.default_dir, f"{self.mapping_name}_missing.yml")
|
2021-09-21 03:57:16 +00:00
|
|
|
self.asset_folders = params["asset_folders"]
|
2021-12-06 07:52:08 +00:00
|
|
|
self.create_asset_folders = params["create_asset_folders"]
|
2021-12-19 00:48:11 +00:00
|
|
|
self.dimensional_asset_rename = params["dimensional_asset_rename"]
|
2022-01-06 20:23:50 +00:00
|
|
|
self.download_url_assets = params["download_url_assets"]
|
2021-12-08 06:23:23 +00:00
|
|
|
self.show_missing_season_assets = params["show_missing_season_assets"]
|
2022-01-25 06:54:44 +00:00
|
|
|
self.show_missing_episode_assets = params["show_missing_episode_assets"]
|
2022-01-25 07:02:34 +00:00
|
|
|
self.show_asset_not_needed = params["show_asset_not_needed"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.sync_mode = params["sync_mode"]
|
2021-12-29 19:51:58 +00:00
|
|
|
self.default_collection_order = params["default_collection_order"]
|
2021-12-29 19:51:07 +00:00
|
|
|
self.minimum_items = params["minimum_items"]
|
2022-01-11 19:37:48 +00:00
|
|
|
self.item_refresh_delay = params["item_refresh_delay"]
|
2021-12-06 07:52:08 +00:00
|
|
|
self.delete_below_minimum = params["delete_below_minimum"]
|
2021-12-06 22:30:38 +00:00
|
|
|
self.delete_not_scheduled = params["delete_not_scheduled"]
|
2021-12-06 07:52:08 +00:00
|
|
|
self.missing_only_released = params["missing_only_released"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.show_unmanaged = params["show_unmanaged"]
|
|
|
|
self.show_filtered = params["show_filtered"]
|
2021-12-17 00:16:08 +00:00
|
|
|
self.show_options = params["show_options"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.show_missing = params["show_missing"]
|
2021-11-16 15:07:09 +00:00
|
|
|
self.show_missing_assets = params["show_missing_assets"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.save_missing = params["save_missing"]
|
2021-12-01 16:59:49 +00:00
|
|
|
self.only_filter_missing = params["only_filter_missing"]
|
2021-12-08 05:17:35 +00:00
|
|
|
self.ignore_ids = params["ignore_ids"]
|
|
|
|
self.ignore_imdb_ids = params["ignore_imdb_ids"]
|
2021-10-27 06:07:33 +00:00
|
|
|
self.assets_for_all = params["assets_for_all"]
|
|
|
|
self.delete_unmanaged_collections = params["delete_unmanaged_collections"]
|
|
|
|
self.delete_collections_with_less = params["delete_collections_with_less"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.mass_genre_update = params["mass_genre_update"]
|
|
|
|
self.mass_audience_rating_update = params["mass_audience_rating_update"]
|
|
|
|
self.mass_critic_rating_update = params["mass_critic_rating_update"]
|
2022-02-07 22:46:24 +00:00
|
|
|
self.mass_content_rating_update = params["mass_content_rating_update"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.mass_trakt_rating_update = params["mass_trakt_rating_update"]
|
2021-12-30 18:59:54 +00:00
|
|
|
self.radarr_add_all_existing = params["radarr_add_all_existing"]
|
2021-12-18 22:45:09 +00:00
|
|
|
self.radarr_remove_by_tag = params["radarr_remove_by_tag"]
|
2021-12-30 18:59:54 +00:00
|
|
|
self.sonarr_add_all_existing = params["sonarr_add_all_existing"]
|
2021-12-18 22:45:09 +00:00
|
|
|
self.sonarr_remove_by_tag = params["sonarr_remove_by_tag"]
|
2022-02-04 22:00:38 +00:00
|
|
|
self.update_blank_track_titles = params["update_blank_track_titles"]
|
2021-12-19 21:02:10 +00:00
|
|
|
self.mass_collection_mode = params["mass_collection_mode"]
|
2022-01-27 15:22:58 +00:00
|
|
|
self.metadata_backup = params["metadata_backup"]
|
2021-12-07 07:10:07 +00:00
|
|
|
self.tmdb_collections = params["tmdb_collections"]
|
2021-12-31 04:51:46 +00:00
|
|
|
self.genre_collections = params["genre_collections"]
|
2021-12-07 07:10:07 +00:00
|
|
|
self.genre_mapper = params["genre_mapper"]
|
2022-03-13 20:44:42 +00:00
|
|
|
self.content_rating_mapper = params["content_rating_mapper"]
|
2021-11-03 14:36:11 +00:00
|
|
|
self.error_webhooks = params["error_webhooks"]
|
2021-12-26 15:48:52 +00:00
|
|
|
self.changes_webhooks = params["changes_webhooks"]
|
2021-09-21 03:57:16 +00:00
|
|
|
self.split_duplicates = params["split_duplicates"] # TODO: Here or just in Plex?
|
|
|
|
self.clean_bundles = params["plex"]["clean_bundles"] # TODO: Here or just in Plex?
|
|
|
|
self.empty_trash = params["plex"]["empty_trash"] # TODO: Here or just in Plex?
|
|
|
|
self.optimize = params["plex"]["optimize"] # TODO: Here or just in Plex?
|
2022-02-20 00:08:41 +00:00
|
|
|
self.stats = {"created": 0, "modified": 0, "deleted": 0, "added": 0, "unchanged": 0, "removed": 0, "radarr": 0, "sonarr": 0, "names": []}
|
2021-12-30 21:29:16 +00:00
|
|
|
self.status = {}
|
2021-12-30 18:59:54 +00:00
|
|
|
|
2022-02-07 22:46:24 +00:00
|
|
|
self.items_library_operation = True if self.assets_for_all or self.mass_genre_update or self.mass_audience_rating_update \
|
|
|
|
or self.mass_critic_rating_update or self.mass_content_rating_update or self.mass_trakt_rating_update \
|
2022-03-13 20:44:42 +00:00
|
|
|
or self.genre_mapper or self.content_rating_mapper or self.tmdb_collections or self.radarr_add_all_existing or self.sonarr_add_all_existing else False
|
2022-02-07 22:46:24 +00:00
|
|
|
self.library_operation = True if self.items_library_operation or self.delete_unmanaged_collections or self.delete_collections_with_less \
|
2021-12-30 18:59:54 +00:00
|
|
|
or self.radarr_remove_by_tag or self.sonarr_remove_by_tag or self.mass_collection_mode \
|
2022-02-07 22:46:24 +00:00
|
|
|
or self.genre_collections or self.show_unmanaged or self.metadata_backup or self.update_blank_track_titles else False
|
2022-01-28 18:36:21 +00:00
|
|
|
|
|
|
|
if self.asset_directory:
|
|
|
|
logger.info("")
|
|
|
|
for ad in self.asset_directory:
|
|
|
|
logger.info(f"Using Asset Directory: {ad}")
|
|
|
|
|
|
|
|
if output:
|
|
|
|
logger.info("")
|
|
|
|
logger.info(output)
|
|
|
|
|
|
|
|
def scan_metadata_files(self):
|
2021-09-21 03:57:16 +00:00
|
|
|
metadata = []
|
|
|
|
for file_type, metadata_file in self.metadata_path:
|
|
|
|
if file_type == "Folder":
|
|
|
|
if os.path.isdir(metadata_file):
|
|
|
|
yml_files = util.glob_filter(os.path.join(metadata_file, "*.yml"))
|
|
|
|
if yml_files:
|
|
|
|
metadata.extend([("File", yml) for yml in yml_files])
|
|
|
|
else:
|
|
|
|
logger.error(f"Config Error: No YAML (.yml) files found in {metadata_file}")
|
|
|
|
else:
|
|
|
|
logger.error(f"Config Error: Folder not found: {metadata_file}")
|
|
|
|
else:
|
|
|
|
metadata.append((file_type, metadata_file))
|
|
|
|
for file_type, metadata_file in metadata:
|
|
|
|
try:
|
2022-01-28 18:36:21 +00:00
|
|
|
meta_obj = MetadataFile(self.config, self, file_type, metadata_file)
|
2021-09-21 03:57:16 +00:00
|
|
|
if meta_obj.collections:
|
|
|
|
self.collections.extend([c for c in meta_obj.collections])
|
|
|
|
if meta_obj.metadata:
|
|
|
|
self.metadatas.extend([c for c in meta_obj.metadata])
|
|
|
|
self.metadata_files.append(meta_obj)
|
|
|
|
except Failed as e:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.error(e)
|
2021-09-21 03:57:16 +00:00
|
|
|
|
2021-12-22 16:19:23 +00:00
|
|
|
if len(self.metadata_files) == 0 and not self.library_operation and not self.config.playlist_files:
|
2021-09-21 03:57:16 +00:00
|
|
|
logger.info("")
|
2021-12-22 16:19:23 +00:00
|
|
|
raise Failed("Config Error: No valid metadata files, playlist files, or library operations found")
|
2021-09-21 03:57:16 +00:00
|
|
|
|
|
|
|
def upload_images(self, item, poster=None, background=None, overlay=None):
|
|
|
|
image = None
|
|
|
|
image_compare = None
|
|
|
|
poster_uploaded = False
|
|
|
|
if self.config.Cache:
|
|
|
|
image, image_compare = self.config.Cache.query_image_map(item.ratingKey, self.image_table_name)
|
|
|
|
|
|
|
|
if poster is not None:
|
|
|
|
try:
|
|
|
|
if image_compare and str(poster.compare) != str(image_compare):
|
|
|
|
image = None
|
|
|
|
if image is None or image != item.thumb:
|
|
|
|
self._upload_image(item, poster)
|
|
|
|
poster_uploaded = True
|
|
|
|
logger.info(f"Detail: {poster.attribute} updated {poster.message}")
|
2022-01-25 07:02:34 +00:00
|
|
|
elif self.show_asset_not_needed:
|
2021-09-21 03:57:16 +00:00
|
|
|
logger.info(f"Detail: {poster.prefix}poster update not needed")
|
|
|
|
except Failed:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.stacktrace()
|
2021-09-21 03:57:16 +00:00
|
|
|
logger.error(f"Detail: {poster.attribute} failed to update {poster.message}")
|
|
|
|
|
|
|
|
if overlay is not None:
|
2022-02-13 22:47:08 +00:00
|
|
|
overlay_name, overlay_folder, overlay_image = overlay
|
2021-09-21 03:57:16 +00:00
|
|
|
self.reload(item)
|
|
|
|
item_labels = {item_tag.tag.lower(): item_tag.tag for item_tag in item.labels}
|
|
|
|
for item_label in item_labels:
|
|
|
|
if item_label.endswith(" overlay") and item_label != f"{overlay_name.lower()} overlay":
|
|
|
|
raise Failed(f"Overlay Error: Poster already has an existing Overlay: {item_labels[item_label]}")
|
|
|
|
if poster_uploaded or image is None or image != item.thumb or f"{overlay_name.lower()} overlay" not in item_labels:
|
|
|
|
if not item.posterUrl:
|
|
|
|
raise Failed(f"Overlay Error: No existing poster to Overlay for {item.title}")
|
2022-01-05 15:42:38 +00:00
|
|
|
response = self.config.get(item.posterUrl)
|
2021-09-21 03:57:16 +00:00
|
|
|
if response.status_code >= 400:
|
|
|
|
raise Failed(f"Overlay Error: Overlay Failed for {item.title}")
|
|
|
|
og_image = response.content
|
2022-02-21 20:56:38 +00:00
|
|
|
ext = "jpg" if response.headers["Content-Type"] == "image/jpegss" else "png"
|
2022-02-13 22:47:08 +00:00
|
|
|
temp_image = os.path.join(overlay_folder, f"temp.{ext}")
|
2021-09-21 03:57:16 +00:00
|
|
|
with open(temp_image, "wb") as handler:
|
|
|
|
handler.write(og_image)
|
2022-02-13 22:47:08 +00:00
|
|
|
shutil.copyfile(temp_image, os.path.join(overlay_folder, f"{item.ratingKey}.{ext}"))
|
2021-09-21 03:57:16 +00:00
|
|
|
while util.is_locked(temp_image):
|
|
|
|
time.sleep(1)
|
|
|
|
try:
|
|
|
|
new_poster = Image.open(temp_image).convert("RGBA")
|
|
|
|
new_poster = new_poster.resize(overlay_image.size, Image.ANTIALIAS)
|
|
|
|
new_poster.paste(overlay_image, (0, 0), overlay_image)
|
|
|
|
new_poster.save(temp_image)
|
|
|
|
self.upload_file_poster(item, temp_image)
|
|
|
|
self.edit_tags("label", item, add_tags=[f"{overlay_name} Overlay"])
|
|
|
|
poster_uploaded = True
|
|
|
|
logger.info(f"Detail: Overlay: {overlay_name} applied to {item.title}")
|
2022-02-10 15:13:14 +00:00
|
|
|
except (OSError, BadRequest) as e:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.stacktrace()
|
2022-02-10 15:13:14 +00:00
|
|
|
raise Failed(f"Overlay Error: {e}")
|
2021-09-21 03:57:16 +00:00
|
|
|
|
|
|
|
background_uploaded = False
|
|
|
|
if background is not None:
|
|
|
|
try:
|
|
|
|
image = None
|
|
|
|
if self.config.Cache:
|
|
|
|
image, image_compare = self.config.Cache.query_image_map(item.ratingKey, f"{self.image_table_name}_backgrounds")
|
|
|
|
if str(background.compare) != str(image_compare):
|
|
|
|
image = None
|
|
|
|
if image is None or image != item.art:
|
|
|
|
self._upload_image(item, background)
|
|
|
|
background_uploaded = True
|
|
|
|
logger.info(f"Detail: {background.attribute} updated {background.message}")
|
2022-01-25 07:02:34 +00:00
|
|
|
elif self.show_asset_not_needed:
|
2021-09-21 03:57:16 +00:00
|
|
|
logger.info(f"Detail: {background.prefix}background update not needed")
|
|
|
|
except Failed:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.stacktrace()
|
2021-09-21 03:57:16 +00:00
|
|
|
logger.error(f"Detail: {background.attribute} failed to update {background.message}")
|
|
|
|
|
|
|
|
if self.config.Cache:
|
|
|
|
if poster_uploaded:
|
|
|
|
self.config.Cache.update_image_map(item.ratingKey, self.image_table_name, item.thumb, poster.compare if poster else "")
|
|
|
|
if background_uploaded:
|
|
|
|
self.config.Cache.update_image_map(item.ratingKey, f"{self.image_table_name}_backgrounds", item.art, background.compare)
|
|
|
|
|
2021-12-17 14:24:46 +00:00
|
|
|
@abstractmethod
|
2021-10-04 17:51:32 +00:00
|
|
|
def notify(self, text, collection=None, critical=True):
|
2021-12-17 14:24:46 +00:00
|
|
|
pass
|
2021-10-04 17:51:32 +00:00
|
|
|
|
2021-09-21 03:57:16 +00:00
|
|
|
@abstractmethod
|
|
|
|
def _upload_image(self, item, image):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def upload_file_poster(self, item, image):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def reload(self, item):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def edit_tags(self, attr, obj, add_tags=None, remove_tags=None, sync_tags=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-01-11 01:23:56 +00:00
|
|
|
def get_all(self, collection_level=None):
|
2021-09-21 03:57:16 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def add_missing(self, collection, items, is_movie):
|
2021-10-26 13:39:30 +00:00
|
|
|
if collection not in self.missing:
|
|
|
|
self.missing[collection] = {}
|
2021-09-21 03:57:16 +00:00
|
|
|
section = "Movies Missing (TMDb IDs)" if is_movie else "Shows Missing (TVDb IDs)"
|
2021-10-26 13:39:30 +00:00
|
|
|
if section not in self.missing[collection]:
|
|
|
|
self.missing[collection][section] = {}
|
2021-09-21 03:57:16 +00:00
|
|
|
for title, item_id in items:
|
2021-10-26 13:39:30 +00:00
|
|
|
self.missing[collection][section][int(item_id)] = title
|
2021-09-21 03:57:16 +00:00
|
|
|
with open(self.missing_path, "w"): pass
|
|
|
|
try:
|
2021-10-26 13:39:30 +00:00
|
|
|
yaml.round_trip_dump(self.missing, open(self.missing_path, "w", encoding="utf-8"))
|
2021-09-21 03:57:16 +00:00
|
|
|
except yaml.scanner.ScannerError as e:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.error(f"YAML Error: {util.tab_new_lines(e)}")
|
2021-09-21 03:57:16 +00:00
|
|
|
|
|
|
|
def map_guids(self):
|
|
|
|
items = self.get_all()
|
|
|
|
logger.info(f"Mapping {self.type} Library: {self.name}")
|
|
|
|
logger.info("")
|
|
|
|
for i, item in enumerate(items, 1):
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.ghost(f"Processing: {i}/{len(items)} {item.title}")
|
2021-09-21 03:57:16 +00:00
|
|
|
if item.ratingKey not in self.movie_rating_key_map and item.ratingKey not in self.show_rating_key_map:
|
|
|
|
id_type, main_id, imdb_id = self.config.Convert.get_id(item, self)
|
|
|
|
if main_id:
|
|
|
|
if id_type == "movie":
|
|
|
|
self.movie_rating_key_map[item.ratingKey] = main_id[0]
|
|
|
|
util.add_dict_list(main_id, item.ratingKey, self.movie_map)
|
|
|
|
elif id_type == "show":
|
|
|
|
self.show_rating_key_map[item.ratingKey] = main_id[0]
|
|
|
|
util.add_dict_list(main_id, item.ratingKey, self.show_map)
|
|
|
|
if imdb_id:
|
|
|
|
util.add_dict_list(imdb_id, item.ratingKey, self.imdb_map)
|
|
|
|
logger.info("")
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.info(f"Processed {len(items)} {self.type}s")
|
2021-09-21 03:57:16 +00:00
|
|
|
return items
|