From 371cb567e73faf0e5571929d4595f5745e05f558 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Tue, 20 Jul 2021 16:52:07 -0400 Subject: [PATCH] fixed overlay --- modules/builder.py | 9 +---- modules/cache.py | 94 ++++++++++++++++++++++++++++++++-------------- modules/plex.py | 22 +++-------- 3 files changed, 74 insertions(+), 51 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 5e8d34cc..964759c9 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -1726,18 +1726,15 @@ class CollectionBuilder: 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.original_mapping_name, "poster", overlay_name) + rating_keys = self.config.Cache.query_image_map_overlay(self.library.image_table_name, overlay_name) overlay_folder = os.path.join(self.config.default_dir, "overlays", overlay_name) overlay_image = Image.open(os.path.join(overlay_folder, "overlay.png")) temp_image = os.path.join(overlay_folder, f"temp.png") overlay = (overlay_name, overlay_folder, overlay_image, temp_image) - logger.debug(rating_keys) - tmdb_ids = [] tvdb_ids = [] for item in items: - logger.debug(item.ratingKey) if int(item.ratingKey) in rating_keys: rating_keys.remove(int(item.ratingKey)) if self.details["item_assets"] or overlay is not None: @@ -1764,8 +1761,6 @@ class CollectionBuilder: if len(tvdb_ids) > 0: self.library.Sonarr.edit_tags(tvdb_ids, self.item_details["item_sonarr_tag"], self.item_details["apply_tags"]) - logger.debug(rating_keys) - for rating_key in rating_keys: try: item = self.fetch_item(rating_key) @@ -1776,7 +1771,7 @@ class CollectionBuilder: 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.original_mapping_name, "poster", "", "", "") + 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 95c004ce..ecf1865c 100644 --- a/modules/cache.py +++ b/modules/cache.py @@ -6,22 +6,23 @@ logger = logging.getLogger("Plex Meta Manager") class Cache: def __init__(self, config_path, expiration): - cache = f"{os.path.splitext(config_path)[0]}.cache" - with sqlite3.connect(cache) as connection: + self.cache_path = f"{os.path.splitext(config_path)[0]}.cache" + self.expiration = expiration + with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row with closing(connection.cursor()) as cursor: cursor.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='guid_map'") if cursor.fetchone()[0] == 0: - logger.info(f"Initializing cache database at {cache}") + logger.info(f"Initializing cache database at {self.cache_path}") else: - logger.info(f"Using cache database at {cache}") + logger.info(f"Using cache database at {self.cache_path}") cursor.execute("DROP TABLE IF EXISTS guids") cursor.execute("DROP TABLE IF EXISTS imdb_to_tvdb_map") cursor.execute("DROP TABLE IF EXISTS tmdb_to_tvdb_map") cursor.execute("DROP TABLE IF EXISTS imdb_map") cursor.execute( """CREATE TABLE IF NOT EXISTS guid_map ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, plex_guid TEXT UNIQUE, t_id TEXT, media_type TEXT, @@ -29,7 +30,7 @@ class Cache: ) cursor.execute( """CREATE TABLE IF NOT EXISTS imdb_to_tmdb_map ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, imdb_id TEXT UNIQUE, tmdb_id TEXT, media_type TEXT, @@ -37,28 +38,28 @@ class Cache: ) cursor.execute( """CREATE TABLE IF NOT EXISTS imdb_to_tvdb_map2 ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, imdb_id TEXT UNIQUE, tvdb_id TEXT, expiration_date TEXT)""" ) cursor.execute( """CREATE TABLE IF NOT EXISTS tmdb_to_tvdb_map2 ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, tmdb_id TEXT UNIQUE, tvdb_id TEXT, expiration_date TEXT)""" ) cursor.execute( """CREATE TABLE IF NOT EXISTS letterboxd_map ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, letterboxd_id TEXT UNIQUE, tmdb_id TEXT, expiration_date TEXT)""" ) cursor.execute( """CREATE TABLE IF NOT EXISTS omdb_data ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, imdb_id TEXT UNIQUE, title TEXT, year INTEGER, @@ -72,7 +73,7 @@ class Cache: ) cursor.execute( """CREATE TABLE IF NOT EXISTS anime_map ( - INTEGER PRIMARY KEY, + key INTEGER PRIMARY KEY, anidb TEXT UNIQUE, anilist TEXT, myanimelist TEXT, @@ -80,17 +81,21 @@ class Cache: expiration_date TEXT)""" ) cursor.execute( - """CREATE TABLE IF NOT EXISTS image_map ( - INTEGER PRIMARY KEY, - rating_key TEXT, - library TEXT, - type TEXT, - overlay TEXT, - compare TEXT, - location TEXT)""" + """CREATE TABLE IF NOT EXISTS image_maps ( + key INTEGER PRIMARY KEY, + library TEXT UNIQUE)""" ) - self.expiration = expiration - self.cache_path = cache + cursor.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='image_map'") + if cursor.fetchone()[0] > 0: + cursor.execute(f"SELECT DISTINCT library FROM image_map") + for library in cursor.fetchall(): + table_name = self.get_image_table_name(library["library"]) + cursor.execute(f"SELECT DISTINCT * FROM image_map WHERE library='{library['library']}'") + for row in cursor.fetchall(): + if row["type"] == "poster": + final_table = table_name if row["type"] == "poster" else f"{table_name}_backgrounds" + self.update_image_map(row["rating_key"], final_table, row["location"], row["compare"], row["overlay"]) + cursor.execute("DROP TABLE IF EXISTS image_map") def query_guid_map(self, plex_guid): id_to_return = None @@ -233,30 +238,63 @@ class Cache: cursor.execute("INSERT OR IGNORE INTO anime_map(anidb) VALUES(?)", (anime_ids["anidb"],)) cursor.execute("UPDATE anime_map SET anilist = ?, myanimelist = ?, kitsu = ?, expiration_date = ? WHERE anidb = ?", (anime_ids["anidb"], anime_ids["myanimelist"], anime_ids["kitsu"], expiration_date.strftime("%Y-%m-%d"), anime_ids["anidb"])) - def query_image_map_overlay(self, library, image_type, overlay): + def get_image_table_name(self, library): + table_name = None + with sqlite3.connect(self.cache_path) as connection: + connection.row_factory = sqlite3.Row + with closing(connection.cursor()) as cursor: + cursor.execute(f"SELECT * FROM image_maps WHERE library = ?", (library,)) + row = cursor.fetchone() + if row and row["key"]: + table_name = f"image_map_{row['key']}" + else: + cursor.execute("INSERT OR IGNORE INTO image_maps(library) VALUES(?)", (library,)) + cursor.execute(f"SELECT * FROM image_maps WHERE library = ?", (library,)) + row = cursor.fetchone() + if row and row["key"]: + table_name = f"image_map_{row['key']}" + cursor.execute( + f"""CREATE TABLE IF NOT EXISTS {table_name} ( + key INTEGER PRIMARY KEY, + rating_key TEXT UNIQUE, + overlay TEXT, + compare TEXT, + location TEXT)""" + ) + cursor.execute( + f"""CREATE TABLE IF NOT EXISTS {table_name}_backgrounds ( + key INTEGER PRIMARY KEY, + rating_key TEXT UNIQUE, + overlay TEXT, + compare TEXT, + location TEXT)""" + ) + return table_name + + def query_image_map_overlay(self, table_name, overlay): rks = [] with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row with closing(connection.cursor()) as cursor: - cursor.execute(f"SELECT * FROM image_map WHERE overlay = ? AND library = ? AND type = ?", (overlay, library, image_type)) + cursor.execute(f"SELECT * FROM {table_name} WHERE overlay = ?", (overlay,)) rows = cursor.fetchall() for row in rows: rks.append(int(row["rating_key"])) return rks - def query_image_map(self, rating_key, library, image_type): + def query_image_map(self, rating_key, table_name): with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row with closing(connection.cursor()) as cursor: - cursor.execute(f"SELECT * FROM image_map WHERE rating_key = ? AND library = ? AND type = ?", (rating_key, library, image_type)) + 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 - def update_image_map(self, rating_key, library, image_type, location, compare, overlay): + def update_image_map(self, rating_key, table_name, location, compare, overlay): with sqlite3.connect(self.cache_path) as connection: connection.row_factory = sqlite3.Row with closing(connection.cursor()) as cursor: - cursor.execute("INSERT OR IGNORE INTO image_map(rating_key, library, type) VALUES(?, ?, ?)", (rating_key, library, image_type)) - cursor.execute("UPDATE image_map SET location = ?, compare = ?, overlay = ? WHERE rating_key = ? AND library = ? AND type = ?", (location, compare, overlay, rating_key, library, image_type)) + 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)) diff --git a/modules/plex.py b/modules/plex.py index a3962583..511602c9 100644 --- a/modules/plex.py +++ b/modules/plex.py @@ -309,6 +309,7 @@ class Plex: self.mapping_name, output = util.validate_filename(self.original_mapping_name) if output: logger.info(output) + self.image_table_name = self.config.Cache.get_image_table_name(self.original_mapping_name) if self.config.Cache else None self.missing_path = os.path.join(params["default_dir"], f"{self.name}_missing.yml") self.metadata_path = params["metadata_path"] self.asset_directory = params["asset_directory"] @@ -416,8 +417,6 @@ class Plex: @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_plex) def _upload_image(self, item, image): - logger.debug(item) - logger.debug(image) if image.is_poster and image.is_url: item.uploadPoster(url=image.location) elif image.is_poster: @@ -430,8 +429,6 @@ class Plex: @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_plex) def upload_file_poster(self, item, image): - logger.debug(item) - logger.debug(image) item.uploadPoster(filepath=image) self.reload(item) @@ -441,13 +438,9 @@ class Plex: try: image = None if self.config.Cache: - image, image_compare, _ = self.config.Cache.query_image_map(item.ratingKey, self.original_mapping_name, "poster") - logger.debug(poster.compare) - logger.debug(image_compare) + image, image_compare, _ = self.config.Cache.query_image_map(item.ratingKey, self.image_table_name) if str(poster.compare) != str(image_compare): image = None - logger.debug(image) - logger.debug(item.thumb) if image is None or image != item.thumb: self._upload_image(item, poster) poster_uploaded = True @@ -463,11 +456,8 @@ class Plex: 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.original_mapping_name, "poster") + _, _, image_overlay = self.config.Cache.query_image_map(item.ratingKey, self.image_table_name) if poster_uploaded or not image_overlay or image_overlay != overlay_name: - logger.debug(poster_uploaded) - logger.debug(image_overlay) - logger.debug(overlay_name) response = requests.get(item.posterUrl) if response.status_code >= 400: raise Failed(f"Overlay Error: Overlay Failed for {item.title}") @@ -490,7 +480,7 @@ class Plex: try: image = None if self.config.Cache: - image, image_compare, _ = self.config.Cache.query_image_map(item.ratingKey, self.original_mapping_name, "background") + image, image_compare, _ = self.config.Cache.query_image_map(item.ratingKey, f"{self.image_table_name}_background") if str(background.compare) != str(image_compare): image = None if image is None or image != item.art: @@ -505,9 +495,9 @@ class Plex: if self.config.Cache: if poster_uploaded: - self.config.Cache.update_image_map(item.ratingKey, self.original_mapping_name, "poster", 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 "", overlay_name) if background_uploaded: - self.config.Cache.update_image_map(item.ratingKey, self.original_mapping_name, "background", 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):