From 67e599c20bba3e1997506fd90c5704ebae839675 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Tue, 10 Aug 2021 11:33:32 -0400 Subject: [PATCH] fix for [] in filenames --- modules/plex.py | 32 +++++++++++++++----------------- modules/util.py | 9 +++++++-- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/modules/plex.py b/modules/plex.py index d38e149d..bc9494af 100644 --- a/modules/plex.py +++ b/modules/plex.py @@ -748,7 +748,6 @@ class Plex: def update_item_from_assets(self, item, overlay=None, create=False): name = os.path.basename(os.path.dirname(str(item.locations[0])) if self.is_movie else str(item.locations[0])) - glob_name = name.translate({ord("["): "[[]", ord("]"): "[]]"}) if "[" in name else name logger.debug(name) found_folder = False poster = None @@ -759,7 +758,7 @@ class Plex: if os.path.isdir(os.path.join(ad, name)): item_dir = os.path.join(ad, name) else: - matches = glob.glob(os.path.join(ad, "*", glob_name)) + matches = util.glob_filter(os.path.join(ad, "*", name)) if len(matches) > 0: item_dir = os.path.abspath(matches[0]) if item_dir is None: @@ -768,12 +767,12 @@ class Plex: poster_filter = os.path.join(item_dir, "poster.*") background_filter = os.path.join(item_dir, "background.*") else: - poster_filter = os.path.join(ad, f"{glob_name}.*") - background_filter = os.path.join(ad, f"{glob_name}_background.*") - matches = glob.glob(poster_filter) + poster_filter = os.path.join(ad, f"{name}.*") + background_filter = os.path.join(ad, f"{name}_background.*") + matches = util.glob_filter(poster_filter) if len(matches) > 0: poster = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title}'s ", is_url=False) - matches = glob.glob(background_filter) + matches = util.glob_filter(background_filter) if len(matches) > 0: background = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title}'s ", is_poster=False, is_url=False) if poster or background: @@ -783,8 +782,8 @@ class Plex: if item_dir: season_filter = os.path.join(item_dir, f"Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*") else: - season_filter = os.path.join(ad, f"{glob_name}_Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*") - matches = glob.glob(season_filter) + season_filter = os.path.join(ad, f"{name}_Season{'0' if season.seasonNumber < 10 else ''}{season.seasonNumber}.*") + matches = util.glob_filter(season_filter) if len(matches) > 0: season_poster = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title} Season {season.seasonNumber}'s ", is_url=False) self.upload_images(season, poster=season_poster) @@ -792,8 +791,8 @@ class Plex: if item_dir: episode_filter = os.path.join(item_dir, f"{episode.seasonEpisode.upper()}.*") else: - episode_filter = os.path.join(ad, f"{glob_name}_{episode.seasonEpisode.upper()}.*") - matches = glob.glob(episode_filter) + episode_filter = os.path.join(ad, f"{name}_{episode.seasonEpisode.upper()}.*") + matches = util.glob_filter(episode_filter) if len(matches) > 0: episode_poster = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title} {episode.seasonEpisode.upper()}'s ", is_url=False) self.upload_images(episode, poster=episode_poster) @@ -810,22 +809,21 @@ class Plex: def find_collection_assets(self, item, name=None, create=False): if name is None: name = item.title - glob_name = name.translate({ord("["): "[[]", ord("]"): "[]]"}) if "[" in name else name for ad in self.asset_directory: poster = None background = None if self.asset_folders: if not os.path.isdir(os.path.join(ad, name)): continue - poster_filter = os.path.join(ad, glob_name, "poster.*") - background_filter = os.path.join(ad, glob_name, "background.*") + poster_filter = os.path.join(ad, name, "poster.*") + background_filter = os.path.join(ad, name, "background.*") else: - poster_filter = os.path.join(ad, f"{glob_name}.*") - background_filter = os.path.join(ad, f"{glob_name}_background.*") - matches = glob.glob(poster_filter) + poster_filter = os.path.join(ad, f"{name}.*") + background_filter = os.path.join(ad, f"{name}_background.*") + matches = util.glob_filter(poster_filter) if len(matches) > 0: poster = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title}'s ", is_url=False) - matches = glob.glob(background_filter) + matches = util.glob_filter(background_filter) if len(matches) > 0: background = ImageData("asset_directory", os.path.abspath(matches[0]), prefix=f"{item.title}'s ", is_poster=False, is_url=False) if poster or background: diff --git a/modules/util.py b/modules/util.py index d5dc6d7e..af024ef6 100644 --- a/modules/util.py +++ b/modules/util.py @@ -1,5 +1,6 @@ -import logging, os, re, signal, sys, time, traceback +import glob, logging, os, re, signal, sys, time, traceback from datetime import datetime, timedelta +from logging.handlers import RotatingFileHandler from pathvalidate import is_valid_filename, sanitize_filename from plexapi.exceptions import BadRequest, NotFound, Unauthorized @@ -203,7 +204,7 @@ def separator(text=None, space=True, border=True, debug=False): def apply_formatter(handler, border=True): text = f"| %(message)-{screen_width - 2}s |" if border else f"%(message)-{screen_width - 2}s" - if isinstance(handler, logging.handlers.RotatingFileHandler): + if isinstance(handler, RotatingFileHandler): text = f"[%(asctime)s] %(filename)-27s %(levelname)-10s {text}" handler.setFormatter(logging.Formatter(text)) @@ -246,6 +247,10 @@ def is_locked(filepath): file_object.close() return locked +def glob_filter(filter_in): + filter_in = filter_in.translate({ord("["): "[[]", ord("]"): "[]]"}) if "[" in filter_in else filter_in + return glob.glob(filter_in) + def is_date_filter(value, modifier, data, final, current_time): if value is None: return True