diff --git a/modules/builder.py b/modules/builder.py index 55a08603..24403caf 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -844,7 +844,7 @@ class CollectionBuilder: if discover_data is True: new_dictionary[discover_final] = discover_data elif discover_final in tmdb.discover_dates: - new_dictionary[discover_final] = util.check_date(discover_data, f"{method_name} attribute {discover_final}", return_string=True) + new_dictionary[discover_final] = util.validate_date(discover_data, f"{method_name} attribute {discover_final}", return_as="%m/%d/%Y") elif discover_final in ["primary_release_year", "year", "first_air_date_year"]: new_dictionary[discover_final] = util.check_number(discover_data, f"{method_name} attribute {discover_final}", minimum=1800, maximum=self.current_year + 1) elif discover_final in ["vote_count.gte", "vote_count.lte", "vote_average.gte", "vote_average.lte", "with_runtime.gte", "with_runtime.lte"]: @@ -1379,7 +1379,7 @@ class CollectionBuilder: elif attribute in ["year", "episode_year"] and modifier in [".gt", ".gte", ".lt", ".lte"]: return util.check_year(data, self.current_year, final) elif attribute in plex.date_attributes and modifier in [".before", ".after"]: - return util.check_date(data, final, return_string=True, plex_date=True) + return util.validate_date(data, final, return_as="%Y-%m-%d") elif attribute in plex.number_attributes and modifier in ["", ".not", ".gt", ".gte", ".lt", ".lte"]: return util.check_number(data, final, minimum=1) elif attribute in plex.float_attributes and modifier in [".gt", ".gte", ".lt", ".lte"]: @@ -1458,8 +1458,8 @@ class CollectionBuilder: elif modifier in [".before", ".after"]: if current_data is None: return False - filter_date = datetime.strptime(str(filter_data), "%m/%d/%Y") - if (modifier == ".before" and current_data >= filter_data) or (modifier == ".after" and current_data <= filter_data): + filter_date = util.validate_date(filter_data) + if (modifier == ".before" and current_data >= filter_date) or (modifier == ".after" and current_data <= filter_date): return False elif filter_attr in ["release", "added", "last_played"] and modifier == ".regex": jailbreak = False @@ -1618,7 +1618,7 @@ class CollectionBuilder: or (filter_method == "tmdb_vote_count.lte" and movie.vote_count > filter_data): match = False break - current_title = f"{movie.title} ({util.check_date(movie.release_date, 'test', plex_date=True).year})" if movie.release_date else movie.title + current_title = f"{movie.title} ({util.validate_date(movie.release_date, 'test').year})" if movie.release_date else movie.title if match: missing_movies_with_names.append((current_title, missing_id)) if self.details["show_missing"] is True: @@ -1958,7 +1958,7 @@ class CollectionBuilder: logger.error(e) continue if self.details["show_missing"] is True: - current_title = f"{movie.title} ({util.check_date(movie.release_date, 'test', plex_date=True).year})" if movie.release_date else movie.title + current_title = f"{movie.title} ({util.validate_date(movie.release_date, 'test').year})" if movie.release_date else movie.title logger.info(f"{name} Collection | ? | {current_title} (TMDb: {missing_id})") logger.info("") logger.info(f"{len(self.run_again_movies)} Movie{'s' if len(self.run_again_movies) > 1 else ''} Missing") diff --git a/modules/meta.py b/modules/meta.py index 309f6e74..f4805e56 100644 --- a/modules/meta.py +++ b/modules/meta.py @@ -90,7 +90,7 @@ class Metadata: try: current = str(getattr(current_item, key, "")) if var_type == "date": - final_value = util.check_date(value, name, return_string=True, plex_date=True) + final_value = util.validate_date(value, name, return_as="%Y-%m-%d") current = current[:-9] elif var_type == "float": final_value = util.check_number(value, name, number_type="float", minimum=0, maximum=10) diff --git a/modules/tmdb.py b/modules/tmdb.py index 3881a58e..ea0278c3 100644 --- a/modules/tmdb.py +++ b/modules/tmdb.py @@ -1,5 +1,4 @@ import logging, tmdbv3api -from datetime import datetime from modules import util from modules.util import Failed from retrying import retry @@ -254,7 +253,7 @@ class TMDb: count = 0 for date_attr in discover_dates: if date_attr in attrs: - attrs[date_attr] = datetime.strftime(datetime.strptime(attrs[date_attr], "%m/%d/%Y"), "%Y-%m-%d") + attrs[date_attr] = util.validate_date(attrs[date_attr], return_as="%Y-%m-%d") self.Discover.discover_movies(attrs) if is_movie else self.Discover.discover_tv_shows(attrs) total_pages = int(self.TMDb.total_pages) total_results = int(self.TMDb.total_results) diff --git a/modules/util.py b/modules/util.py index 76f74bee..984638a1 100644 --- a/modules/util.py +++ b/modules/util.py @@ -270,10 +270,10 @@ def check_number(value, method, number_type="int", minimum=None, maximum=None): else: return num_value -def check_date(date_text, method, return_string=False, plex_date=False): - try: date_obg = datetime.strptime(str(date_text), "%Y-%m-%d" if plex_date else "%m/%d/%Y") - except ValueError: raise Failed(f"Collection Error: {method}: {date_text} must match pattern {'YYYY-MM-DD e.g. 2020-12-25' if plex_date else 'MM/DD/YYYY e.g. 12/25/2020'}") - return str(date_text) if return_string else date_obg +def validate_date(date_text, method, return_as=None): + try: date_obg = datetime.strptime(str(date_text), "%Y-%m-%d" if "-" in str(date_text) else "%m/%d/%Y") + except ValueError: raise Failed(f"Collection Error: {method}: {date_text} must match pattern YYYY-MM-DD (e.g. 2020-12-25) or MM/DD/YYYY (e.g. 12/25/2020)") + return datetime.strftime(date_obg, return_as) if return_as else date_obg def logger_input(prompt, timeout=60): if windows: return windows_input(prompt, timeout)