From 48c211887c8473b7cc4a09b9ebaee52f43561bf3 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Sun, 2 May 2021 03:40:39 -0400 Subject: [PATCH] #225 - _background, _Season##, and _S##E## added --- modules/builder.py | 49 ++++++++++------------------------------------ modules/config.py | 33 ++----------------------------- modules/plex.py | 45 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 71 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 45063fb5..48bda622 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -1297,48 +1297,19 @@ class CollectionBuilder: if self.library.asset_folders: if not os.path.isdir(path): continue - poster_path = os.path.join(ad, f"{name_mapping}", "poster.*") + poster_filter = os.path.join(ad, name_mapping, "poster.*") + background_filter = os.path.join(ad, name_mapping, "background.*") else: - poster_path = os.path.join(ad, f"{name_mapping}.*") - matches = glob.glob(poster_path) + poster_filter = os.path.join(ad, f"{name_mapping}.*") + background_filter = os.path.join(ad, f"{name_mapping}_background.*") + matches = glob.glob(poster_filter) if len(matches) > 0: self.posters["asset_directory"] = os.path.abspath(matches[0]) - if self.library.asset_folders: - matches = glob.glob(os.path.join(ad, f"{name_mapping}", "background.*")) - if len(matches) > 0: - self.backgrounds["asset_directory"] = os.path.abspath(matches[0]) - dirs = [folder for folder in os.listdir(path) if os.path.isdir(os.path.join(path, folder))] - if len(dirs) > 0: - for item in collection.items(): - folder = os.path.basename(os.path.dirname(item.locations[0]) if self.library.is_movie else item.locations[0]) - if folder in dirs: - matches = glob.glob(os.path.join(path, folder, "poster.*")) - poster_path = os.path.abspath(matches[0]) if len(matches) > 0 else None - matches = glob.glob(os.path.join(path, folder, "background.*")) - background_path = os.path.abspath(matches[0]) if len(matches) > 0 else None - if poster_path: - self.library.upload_image(item, poster_path, url=False) - logger.info(f"Detail: asset_directory updated {item.title}'s poster to [file] {poster_path}") - if background_path: - self.library.upload_image(item, background_path, poster=False, url=False) - logger.info(f"Detail: asset_directory updated {item.title}'s background to [file] {background_path}") - if poster_path is None and background_path is None: - logger.warning(f"No Files Found: {os.path.join(path, folder)}") - if self.library.is_show: - for season in item.seasons(): - matches = glob.glob(os.path.join(path, folder, f"Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*")) - if len(matches) > 0: - season_path = os.path.abspath(matches[0]) - self.library.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}") - for episode in season.episodes(): - matches = glob.glob(os.path.join(path, folder, f"{episode.seasonEpisode.upper()}.*")) - if len(matches) > 0: - episode_path = os.path.abspath(matches[0]) - self.library.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}") - else: - logger.warning(f"No Folder: {os.path.join(path, folder)}") + matches = glob.glob(background_filter) + if len(matches) > 0: + self.backgrounds["asset_directory"] = os.path.abspath(matches[0]) + for item in self.library.query(collection.items): + self.library.update_item_from_assets(item, dirs=[path]) 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]}" diff --git a/modules/config.py b/modules/config.py index fabeada5..b9ff0e7a 100644 --- a/modules/config.py +++ b/modules/config.py @@ -1,4 +1,4 @@ -import glob, logging, os, re, requests, time +import logging, os, re, requests, time from modules import util from modules.anidb import AniDBAPI from modules.anilist import AniListAPI @@ -498,36 +498,7 @@ class Config: util.separator(f"All {'Movies' if library.is_movie else 'Shows'} Assets Check for {library.name} Library") logger.info("") for item in library.get_all(): - folder = os.path.basename(os.path.dirname(item.locations[0]) if library.is_movie else item.locations[0]) - for ad in library.asset_directory: - if library.asset_folders: - if not os.path.isdir(os.path.join(ad, folder)): - continue - poster_path = os.path.join(ad, folder, "poster.*") - else: - poster_path = os.path.join(ad, f"{folder}.*") - matches = glob.glob(poster_path) - if len(matches) > 0: - library.upload_image(item, os.path.abspath(matches[0]), url=False) - logger.info(f"Detail: asset_directory updated {item.title}'s poster to [file] {os.path.abspath(matches[0])}") - if library.asset_folders: - matches = glob.glob(os.path.join(ad, folder, "background.*")) - if len(matches) > 0: - library.upload_image(item, os.path.abspath(matches[0]), poster=False, url=False) - logger.info(f"Detail: asset_directory updated {item.title}'s background to [file] {os.path.abspath(matches[0])}") - if library.is_show: - for season in item.seasons(): - matches = glob.glob(os.path.join(ad, folder, f"Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*")) - if len(matches) > 0: - season_path = os.path.abspath(matches[0]) - library.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}") - for episode in season.episodes(): - matches = glob.glob(os.path.join(ad, folder, f"{episode.seasonEpisode.upper()}.*")) - if len(matches) > 0: - episode_path = os.path.abspath(matches[0]) - library.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}") + library.update_item_from_assets(item) has_run_again = False for library in self.libraries: diff --git a/modules/plex.py b/modules/plex.py index 639709ae..c7b68146 100644 --- a/modules/plex.py +++ b/modules/plex.py @@ -1,4 +1,4 @@ -import logging, os, re, requests +import glob, logging, os, re, requests from datetime import datetime, timedelta from modules import util from modules.util import Failed @@ -938,3 +938,46 @@ class PlexAPI: logger.error("Metadata Error: episodes attribute is blank") elif "episodes" in methods: logger.error("Metadata Error: episodes attribute only works for show libraries") + + def update_item_from_assets(self, item, dirs=None): + if dirs is None: + dirs = self.asset_directory + name = os.path.basename(os.path.dirname(item.locations[0]) if self.is_movie else item.locations[0]) + for ad in dirs: + if self.asset_folders: + if not os.path.isdir(os.path.join(ad, name)): + continue + poster_filter = os.path.join(ad, name, "poster.*") + background_filter = os.path.join(ad, name, "background.*") + else: + poster_filter = os.path.join(ad, f"{name}.*") + background_filter = os.path.join(ad, f"{name}_background.*") + matches = glob.glob(poster_filter) + if len(matches) > 0: + self.upload_image(item, os.path.abspath(matches[0]), url=False) + logger.info(f"Detail: asset_directory updated {item.title}'s poster to [file] {os.path.abspath(matches[0])}") + matches = glob.glob(background_filter) + if len(matches) > 0: + self.upload_image(item, os.path.abspath(matches[0]), poster=False, url=False) + logger.info(f"Detail: asset_directory updated {item.title}'s background to [file] {os.path.abspath(matches[0])}") + if self.is_show: + for season in self.query(item.seasons): + if self.asset_folders: + season_filter = os.path.join(ad, name, f"Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*") + else: + season_filter = os.path.join(ad, f"{name}_Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*") + 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}") + for episode in self.query(season.episodes): + if self.asset_folders: + episode_filter = os.path.join(ad, name, f"{episode.seasonEpisode.upper()}.*") + else: + episode_filter = os.path.join(ad, f"{name}_{episode.seasonEpisode.upper()}.*") + 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}") \ No newline at end of file