From 98d1146e610236c15fa052aaba0e907f3983cb18 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Thu, 12 Aug 2021 16:36:38 -0400 Subject: [PATCH] changes overlay record keeping to labels --- modules/builder.py | 16 ++++++++++++++-- modules/cache.py | 14 ++++++++++---- modules/plex.py | 41 ++++++++++++++++++++++------------------- plex_meta_manager.py | 6 +++++- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 816d1107..d2f528df 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -1638,11 +1638,22 @@ class CollectionBuilder: logger.info("") overlay = None overlay_folder = None + overlay_name = "" rating_keys = [] if "item_overlay" in self.item_details: overlay_name = self.item_details["item_overlay"] if self.config.Cache: - rating_keys = self.config.Cache.query_image_map_overlay(self.library.image_table_name, overlay_name) + cache_keys = self.config.Cache.query_image_map_overlay(self.library.image_table_name, overlay_name) + if cache_keys: + for rating_key in cache_keys: + try: + item = self.fetch_item(rating_key) + except Failed as e: + logger.error(e) + continue + self.library.edit_tags("label", item, add_tags=[f"{overlay_name} Overlay"]) + self.config.Cache.update_remove_overlay(self.library.image_table_name, overlay_name) + rating_keys = [int(item.ratingKey) for item in self.library.get_labeled_items(f"{overlay_name} Overlay")] overlay_folder = os.path.join(self.config.default_dir, "overlays", overlay_name) overlay_image = Image.open(os.path.join(overlay_folder, "overlay.png")).convert("RGBA") temp_image = os.path.join(overlay_folder, f"temp.png") @@ -1683,11 +1694,12 @@ class CollectionBuilder: except Failed as e: logger.error(e) continue + self.library.edit_tags("label", item, remove_tags=[f"{overlay_name} Overlay"]) og_image = os.path.join(overlay_folder, f"{rating_key}.png") if os.path.exists(og_image): self.library.upload_file_poster(item, og_image) os.remove(og_image) - self.config.Cache.update_image_map(item.ratingKey, self.library.image_table_name, "", "", "") + self.config.Cache.update_image_map(item.ratingKey, self.library.image_table_name, "", "") def update_details(self): if not self.obj and self.smart_url: diff --git a/modules/cache.py b/modules/cache.py index 1f93a3b0..1c0e0fcd 100644 --- a/modules/cache.py +++ b/modules/cache.py @@ -302,6 +302,12 @@ class Cache: rks.append(int(row["rating_key"])) return rks + def update_remove_overlay(self, table_name, overlay): + with sqlite3.connect(self.cache_path) as connection: + connection.row_factory = sqlite3.Row + with closing(connection.cursor()) as cursor: + cursor.execute(f"UPDATE {table_name} SET overlay = ? WHERE overlay = ?", ("", overlay)) + def query_image_map(self, rating_key, table_name): with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row @@ -309,12 +315,12 @@ class Cache: cursor.execute(f"SELECT * FROM {table_name} WHERE rating_key = ?", (rating_key,)) row = cursor.fetchone() if row and row["location"]: - return row["location"], row["compare"], row["overlay"] - return None, None, None + return row["location"], row["compare"] + return None, None - def update_image_map(self, rating_key, table_name, location, compare, overlay): + def update_image_map(self, rating_key, table_name, location, compare): with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row with closing(connection.cursor()) as cursor: cursor.execute(f"INSERT OR IGNORE INTO {table_name}(rating_key) VALUES(?)", (rating_key,)) - cursor.execute(f"UPDATE {table_name} SET location = ?, compare = ?, overlay = ? WHERE rating_key = ?", (location, compare, overlay, rating_key)) + cursor.execute(f"UPDATE {table_name} SET location = ?, compare = ?, overlay = ? WHERE rating_key = ?", (location, compare, "", rating_key)) diff --git a/modules/plex.py b/modules/plex.py index c071d089..10204d0b 100644 --- a/modules/plex.py +++ b/modules/plex.py @@ -1,4 +1,4 @@ -import glob, logging, os, plexapi, requests, shutil, time +import logging, os, plexapi, requests, shutil, time from modules import builder, util from modules.meta import Metadata from modules.util import Failed, ImageData @@ -365,11 +365,15 @@ class Plex: @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_plex) def reload(self, item): - item.reload(checkFiles=False, includeAllConcerts=False, includeBandwidths=False, includeChapters=False, - includeChildren=False, includeConcerts=False, includeExternalMedia=False, includeExtras=False, - includeFields=False, includeGeolocation=False, includeLoudnessRamps=False, includeMarkers=False, - includeOnDeck=False, includePopularLeaves=False, includeRelated=False, - includeRelatedCount=0, includeReviews=False, includeStations=False) + try: + item.reload(checkFiles=False, includeAllConcerts=False, includeBandwidths=False, includeChapters=False, + includeChildren=False, includeConcerts=False, includeExternalMedia=False, includeExtras=False, + includeFields=False, includeGeolocation=False, includeLoudnessRamps=False, includeMarkers=False, + includeOnDeck=False, includePopularLeaves=False, includeRelated=False, + includeRelatedCount=0, includeReviews=False, includeStations=False) + except (BadRequest, NotFound) as e: + util.print_stacktrace() + raise Failed(f"Item Failed to Load: {e}") @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_plex) def edit_query(self, item, edits, advanced=False): @@ -407,7 +411,7 @@ class Plex: 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) + image, image_compare = self.config.Cache.query_image_map(item.ratingKey, self.image_table_name) if poster is not None: try: @@ -423,15 +427,13 @@ class Plex: util.print_stacktrace() logger.error(f"Detail: {poster.attribute} failed to update {poster.message}") - overlay_name = "" if overlay is not None: overlay_name, overlay_folder, overlay_image, temp_image = overlay - image_overlay = None - if self.config.Cache: - _, _, image_overlay = self.config.Cache.query_image_map(item.ratingKey, self.image_table_name) - if image is None or image != item.thumb: - image_overlay = None - if poster_uploaded or not image_overlay or image_overlay != overlay_name: + 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}") response = requests.get(item.posterUrl) @@ -448,6 +450,7 @@ class Plex: 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}") @@ -456,7 +459,7 @@ class Plex: try: image = None if self.config.Cache: - image, image_compare, _ = self.config.Cache.query_image_map(item.ratingKey, f"{self.image_table_name}_backgrounds") + 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: @@ -471,9 +474,9 @@ class Plex: 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 "", overlay_name) + 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, "") + self.config.Cache.update_image_map(item.ratingKey, f"{self.image_table_name}_backgrounds", item.art, background.compare) @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_failed) def get_search_choices(self, search_name, title=True): @@ -743,11 +746,11 @@ class Plex: if _add: updated = True self.query_data(getattr(obj, f"add{attr.capitalize()}"), _add) - logger.info(f"Detail: {attr.capitalize()} {util.compile_list(_add)} added") + logger.info(f"Detail: {attr.capitalize()} {util.compile_list(_add)} added to {obj.title}") if _remove: updated = True self.query_data(getattr(obj, f"remove{attr.capitalize()}"), _remove) - logger.info(f"Detail: {attr.capitalize()} {util.compile_list(_remove)} removed") + logger.info(f"Detail: {attr.capitalize()} {util.compile_list(_remove)} removed to {obj.title}") return updated def update_item_from_assets(self, item, overlay=None, create=False): diff --git a/plex_meta_manager.py b/plex_meta_manager.py index 1efca2e1..b1bdf942 100644 --- a/plex_meta_manager.py +++ b/plex_meta_manager.py @@ -262,7 +262,11 @@ def mass_metadata(config, library, items): trakt_ratings = config.Trakt.user_ratings(library.is_movie) if library.mass_trakt_rating_update else [] for i, item in enumerate(items, 1): - library.reload(item) + try: + library.reload(item) + except Failed as e: + logger.error(e) + continue util.print_return(f"Processing: {i}/{len(items)} {item.title}") tmdb_id = None tvdb_id = None