#263 cache image details and upload only as needed

pull/309/head
meisnate12 4 years ago
parent 87d582658b
commit ff0bc2665c

@ -1682,36 +1682,28 @@ class CollectionBuilder:
if background_image:
self.backgrounds["asset_directory"] = background_image
def set_image(image_method, images, is_background=False):
message = f"{'background' if is_background else 'poster'} to [{'File' if image_method in image_file_details else 'URL'}] {images[image_method]}"
try:
self.library.upload_image(self.obj, images[image_method], poster=not is_background, url=image_method not in image_file_details)
logger.info(f"Detail: {image_method} updated collection {message}")
except BadRequest:
logger.error(f"Detail: {image_method} failed to update {message}")
if len(self.posters) > 1:
logger.info(f"{len(self.posters)} posters found:")
for p in self.posters:
logger.info(f"Method: {p} Poster: {self.posters[p]}")
if "url_poster" in self.posters: set_image("url_poster", self.posters)
elif "file_poster" in self.posters: set_image("file_poster", self.posters)
elif "tmdb_poster" in self.posters: set_image("tmdb_poster", self.posters)
elif "tmdb_profile" in self.posters: set_image("tmdb_profile", self.posters)
elif "tvdb_poster" in self.posters: set_image("tvdb_poster", self.posters)
elif "asset_directory" in self.posters: set_image("asset_directory", self.posters)
elif "tmdb_person" in self.posters: set_image("tmdb_person", self.posters)
elif "tmdb_collection_details" in self.posters: set_image("tmdb_collection_details", self.posters)
elif "tmdb_actor_details" in self.posters: set_image("tmdb_actor_details", self.posters)
elif "tmdb_crew_details" in self.posters: set_image("tmdb_crew_details", self.posters)
elif "tmdb_director_details" in self.posters: set_image("tmdb_director_details", self.posters)
elif "tmdb_producer_details" in self.posters: set_image("tmdb_producer_details", self.posters)
elif "tmdb_writer_details" in self.posters: set_image("tmdb_writer_details", self.posters)
elif "tmdb_movie_details" in self.posters: set_image("tmdb_movie_details", self.posters)
elif "tvdb_movie_details" in self.posters: set_image("tvdb_movie_details", self.posters)
elif "tvdb_show_details" in self.posters: set_image("tvdb_show_details", self.posters)
elif "tmdb_show_details" in self.posters: set_image("tmdb_show_details", self.posters)
if "url_poster" in self.posters: self.library.upload_image("url_poster", self.obj, self.posters["url_poster"])
elif "file_poster" in self.posters: self.library.upload_image("file_poster", self.obj, self.posters["file_poster"], url=False)
elif "tmdb_poster" in self.posters: self.library.upload_image("tmdb_poster", self.obj, self.posters["tmdb_poster"])
elif "tmdb_profile" in self.posters: self.library.upload_image("tmdb_poster", self.obj, self.posters["tmdb_profile"])
elif "tvdb_poster" in self.posters: self.library.upload_image("tvdb_poster", self.obj, self.posters["tvdb_poster"])
elif "asset_directory" in self.posters: self.library.upload_image("asset_directory", self.obj, self.posters["asset_directory"], url=False)
elif "tmdb_person" in self.posters: self.library.upload_image("tmdb_person", self.obj, self.posters["tmdb_person"])
elif "tmdb_collection_details" in self.posters: self.library.upload_image("tmdb_collection_details", self.obj, self.posters["tmdb_collection_details"])
elif "tmdb_actor_details" in self.posters: self.library.upload_image("tmdb_actor_details", self.obj, self.posters["tmdb_actor_details"])
elif "tmdb_crew_details" in self.posters: self.library.upload_image("tmdb_crew_details", self.obj, self.posters["tmdb_crew_details"])
elif "tmdb_director_details" in self.posters: self.library.upload_image("tmdb_director_details", self.obj, self.posters["tmdb_director_details"])
elif "tmdb_producer_details" in self.posters: self.library.upload_image("tmdb_producer_details", self.obj, self.posters["tmdb_producer_details"])
elif "tmdb_writer_details" in self.posters: self.library.upload_image("tmdb_writer_details", self.obj, self.posters["tmdb_writer_details"])
elif "tmdb_movie_details" in self.posters: self.library.upload_image("tmdb_movie_details", self.obj, self.posters["tmdb_movie_details"])
elif "tvdb_movie_details" in self.posters: self.library.upload_image("tvdb_movie_details", self.obj, self.posters["tvdb_movie_details"])
elif "tvdb_show_details" in self.posters: self.library.upload_image("tvdb_show_details", self.obj, self.posters["tvdb_show_details"])
elif "tmdb_show_details" in self.posters: self.library.upload_image("tmdb_show_details", self.obj, self.posters["tmdb_show_details"])
else: logger.info("No poster to update")
if len(self.backgrounds) > 1:
@ -1719,16 +1711,16 @@ class CollectionBuilder:
for b in self.backgrounds:
logger.info(f"Method: {b} Background: {self.backgrounds[b]}")
if "url_background" in self.backgrounds: set_image("url_background", self.backgrounds, is_background=True)
elif "file_background" in self.backgrounds: set_image("file_background", self.backgrounds, is_background=True)
elif "tmdb_background" in self.backgrounds: set_image("tmdb_background", self.backgrounds, is_background=True)
elif "tvdb_background" in self.backgrounds: set_image("tvdb_background", self.backgrounds, is_background=True)
elif "asset_directory" in self.backgrounds: set_image("asset_directory", self.backgrounds, is_background=True)
elif "tmdb_collection_details" in self.backgrounds: set_image("tmdb_collection_details", self.backgrounds, is_background=True)
elif "tmdb_movie_details" in self.backgrounds: set_image("tmdb_movie_details", self.backgrounds, is_background=True)
elif "tvdb_movie_details" in self.backgrounds: set_image("tvdb_movie_details", self.backgrounds, is_background=True)
elif "tvdb_show_details" in self.backgrounds: set_image("tvdb_show_details", self.backgrounds, is_background=True)
elif "tmdb_show_details" in self.backgrounds: set_image("tmdb_show_details", self.backgrounds, is_background=True)
if "url_background" in self.backgrounds: self.library.upload_image("url_background", self.obj, self.backgrounds["url_background"], poster=False)
elif "file_background" in self.backgrounds: self.library.upload_image("file_background", self.obj, self.backgrounds["file_background"], poster=False, url=False)
elif "tmdb_background" in self.backgrounds: self.library.upload_image("tmdb_background", self.obj, self.backgrounds["tmdb_background"], poster=False)
elif "tvdb_background" in self.backgrounds: self.library.upload_image("tvdb_background", self.obj, self.backgrounds["tvdb_background"], poster=False)
elif "asset_directory" in self.backgrounds: self.library.upload_image("asset_directory", self.obj, self.backgrounds["asset_directory"], poster=False, url=False)
elif "tmdb_collection_details" in self.backgrounds: self.library.upload_image("tmdb_collection_details", self.obj, self.backgrounds["tmdb_collection_details"], poster=False)
elif "tmdb_movie_details" in self.backgrounds: self.library.upload_image("tmdb_movie_details", self.obj, self.backgrounds["tmdb_movie_details"], poster=False)
elif "tvdb_movie_details" in self.backgrounds: self.library.upload_image("tvdb_movie_details", self.obj, self.backgrounds["tvdb_movie_details"], poster=False)
elif "tvdb_show_details" in self.backgrounds: self.library.upload_image("tvdb_show_details", self.obj, self.backgrounds["tvdb_show_details"], poster=False)
elif "tmdb_show_details" in self.backgrounds: self.library.upload_image("tmdb_show_details", self.obj, self.backgrounds["tmdb_show_details"], poster=False)
else: logger.info("No background to update")
def run_collections_again(self):

@ -79,6 +79,15 @@ class Cache:
kitsu TEXT,
expiration_date TEXT)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS image_map (
INTEGER PRIMARY KEY,
rating_key TEXT,
library TEXT,
type TEXT,
compare TEXT,
location TEXT)"""
)
self.expiration = expiration
self.cache_path = cache
@ -221,3 +230,20 @@ class Cache:
with closing(connection.cursor()) as cursor:
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(self, rating_key, library, image_type):
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))
row = cursor.fetchone()
if row and row["location"]:
return row["location"], row["compare"]
return None, None
def update_image_map(self, rating_key, library, image_type, location, compare):
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) VALUES(?, ?)", (rating_key, library))
cursor.execute("UPDATE poster_map SET location = ?, compare = ? WHERE rating_key = ? AND library = ? AND type = ?", (location, compare, rating_key, library, image_type))

@ -143,9 +143,7 @@ class Metadata:
def set_image(attr, obj, group, alias, poster=True, url=True):
if group[alias[attr]]:
message = f"{'poster' if poster else 'background'} to [{'URL' if url else 'File'}] {group[alias[attr]]}"
self.library.upload_image(obj, group[alias[attr]], poster=poster, url=url)
logger.info(f"Detail: {attr} updated {message}")
self.library.upload_image(attr, obj, group[alias[attr]], poster=poster, url=url)
else:
logger.error(f"Metadata Error: {attr} attribute is blank")

@ -308,7 +308,8 @@ class PlexAPI:
self.Sonarr = None
self.Tautulli = None
self.name = params["name"]
self.mapping_name, output = util.validate_filename(params["mapping_name"])
self.original_mapping_name = params["mapping_name"]
self.mapping_name, output = util.validate_filename(self.original_mapping_name)
if output:
logger.info(output)
self.missing_path = os.path.join(params["default_dir"], f"{self.name}_missing.yml")
@ -425,7 +426,7 @@ class PlexAPI:
self.reload(item)
@retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_plex)
def upload_image(self, item, location, poster=True, url=True):
def _upload_image(self, item, location, poster=True, url=True):
if poster and url:
item.uploadPoster(url=location)
elif poster:
@ -435,6 +436,25 @@ class PlexAPI:
else:
item.uploadArt(filepath=location)
def upload_image(self, attr, item, location, name="", poster=True, url=True):
image_type = "poster" if poster else "background"
message = f"{name}{image_type} to [{'URL' if url else 'File'}] {location}"
try:
image = None
if self.config.Cache:
image = self.config.Cache.query_image_map(item.ratingKey, self.original_mapping_name, image_type)
if image is None or (image_type == "poster" and image != item.thumb) or (image_type == "background" and image != item.art):
self._upload_image(item, location, poster=poster, url=url)
if self.config.Cache:
self.reload(item)
compare = location if url else os.stat(location).st_size
self.config.Cache.update_image_map(item.ratingKey, self.original_mapping_name, image_type, item.thumb if image_type == "poster" else item.art, compare)
logger.info(f"Detail: {attr} updated {message}")
else:
logger.info(f"Detail: {name}{image_type} update not needed")
except BadRequest:
logger.error(f"Detail: {attr} failed to update {message}")
@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):
try:
@ -725,14 +745,12 @@ class PlexAPI:
if len(matches) > 0:
poster_image = os.path.abspath(matches[0])
if upload:
self.upload_image(item, poster_image, url=False)
logger.info(f"Detail: asset_directory updated {item.title}'s poster to [file] {poster_image}")
self.upload_image("asset_directory", item, poster_image, name=f"{item.title}'s ", url=False)
matches = glob.glob(background_filter)
if len(matches) > 0:
background_image = os.path.abspath(matches[0])
if upload:
self.upload_image(item, background_image, poster=False, url=False)
logger.info(f"Detail: asset_directory updated {item.title}'s background to [file] {background_image}")
self.upload_image("asset_directory", item, background_image, name=f"{item.title}'s ", poster=False, url=False)
if collection_mode:
for ite in self.query(item.items):
self.update_item_from_assets(ite, dirs=[os.path.join(ad, name)])
@ -747,8 +765,7 @@ class PlexAPI:
matches = glob.glob(season_filter)
if len(matches) > 0:
season_path = os.path.abspath(matches[0])
self.upload_image(season, season_path, url=False)
logger.info(f"Detail: asset_directory updated {item.title} Season {season.seasonNumber}'s poster to [file] {season_path}")
self.upload_image("asset_directory", season, season_path, name=f"{item.title} Season {season.seasonNumber}'s ", url=False)
for episode in self.query(season.episodes):
if self.asset_folders:
episode_filter = os.path.join(ad, name, f"{episode.seasonEpisode.upper()}.*")
@ -757,6 +774,5 @@ class PlexAPI:
matches = glob.glob(episode_filter)
if len(matches) > 0:
episode_path = os.path.abspath(matches[0])
self.upload_image(episode, episode_path, url=False)
logger.info(f"Detail: asset_directory updated {item.title} {episode.seasonEpisode.upper()}'s poster to [file] {episode_path}")
self.upload_image("asset_directory", episode, episode_path, name=f"{item.title} {episode.seasonEpisode.upper()}'s ", url=False)
return None, None

Loading…
Cancel
Save