Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
meisnate12 committed Apr 25, 2023
1 parent 1cc7eee commit 4fb8371
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions tmdbapis/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _validate_date(self, data, date_format="%Y-%m-%d"):
try:
return datetime.strptime(str(data), date_format).strftime(date_format)
except ValueError:
raise Invalid(f"date: {data} must be a datetime or in the format YYYY-MM-DD")
raise Invalid(f"date: {data} must be a datetime or in the format YYYY-MM-DD (e.g. 2020-12-25)")

def _validate_discover(self, is_movie, **kwargs):
validated = {}
Expand All @@ -168,33 +168,29 @@ def _validate_discover(self, is_movie, **kwargs):
raise Invalid("certification must be used with certification_country")
validated[k] = str(v)
elif k in ["include_adult", "include_video", "include_null_first_air_dates", "screened_theatrically"]:
if not isinstance(v, bool):
data = self._parse(data=v, value_type="bool")
if data is None:
raise Invalid(f"{k} must be either True or False")
validated[k] = v
elif k in ["primary_release_date.gte", "primary_release_date.lte", "release_date.gte", "release_date.lte",
"air_date.gte", "air_date.lte", "first_air_date.gte", "first_air_date.lte"]:
if isinstance(v, datetime):
date_obj = v
else:
try:
date_obj = datetime.strptime(str(v), "%Y-%m-%d")
except ValueError:
raise Invalid(f"{k} must be a datetime object or match pattern YYYY-MM-DD (e.g. 2020-12-25)")
validated[k] = datetime.strftime(date_obj, "%Y-%m-%d")
data = self._validate_date(v)
if data:
validated[k] = data
elif k in ["primary_release_year", "first_air_date_year", "vote_count.gte", "vote_count.lte",
"with_runtime.gte", "with_runtime.lte"]:
if not isinstance(v, int) or v < 1:
if not isinstance(v, int) or v <= 0:
raise Invalid(f"{k} must be an integer greater then 0")
validated[k] = v
elif k in ["vote_average.gte", "vote_average.lte"]:
if not isinstance(v, float) or v < 1:
if not isinstance(v, (int, float)) or v <= 0:
raise Invalid(f"{k} must be a number greater then 0.0")
validated[k] = v
validated[k] = float(v)
elif k == "with_watch_monetization_types":
if "watch_region" not in kwargs:
raise Invalid("with_watch_monetization_types must be used with watch_region")
if v not in ["flatrate", "free", "ads", "rent", "buy"]:
raise Invalid(f"{v} is not a valid with_watch_monetization_types option. Options: [flatrate, free, ads, rent, or buy]")
if "watch_region" not in kwargs:
raise Invalid("with_watch_monetization_types must be used with watch_region")
validated[k] = v
else:
validated[k] = ",".join([str(x) for x in v]) if isinstance(v, list) else str(v)
Expand All @@ -215,10 +211,11 @@ def include_language(self, include_language):
final.append("null")
elif str(lang).lower() in self._iso_639_1:
final.append(str(lang).lower())
elif str(lang).lower() in self._translations:
final.append(self._translations[str(lang).lower()])
else:
raise Invalid(f"Language: {lang} is invalid see Configuration.languages and Configuration.primary_translations for the options.")
try:
final.append(self._validate_translation(lang))
except Invalid:
raise Invalid(f"Language: {lang} is invalid see Configuration.languages and Configuration.primary_translations for the options.")
self._include_language = ",".join(final)

@property
Expand All @@ -231,10 +228,11 @@ def language(self, lang):
self._language = None
elif str(lang).lower() in self._iso_639_1:
self._language = str(lang).lower()
elif str(lang).lower() in self._translations:
self._language = self._translations[str(lang).lower()]
else:
raise Invalid(f"Language: {lang} is invalid see Configuration.languages and Configuration.primary_translations for the options.")
try:
self._language = self._validate_translation(lang)
except Invalid:
raise Invalid(f"Language: {lang} is invalid see Configuration.languages and Configuration.primary_translations for the options.")

@property
def account_id(self):
Expand Down Expand Up @@ -586,10 +584,8 @@ def movie_certifications(self, reload: bool = False) -> Dict[str, CountryCertifi
Dict[str, :class:`~tmdbapis.objs.simple.CountryCertifications`]
"""
if reload or self._movie_certifications is None:
self._movie_certifications = {
k: CountryCertifications(self, v, k) for k, v in
self._api.certifications_get_movie_certifications()["certifications"].items()
}
self._movie_certifications = self._parse(data=self._api.certifications_get_movie_certifications(),
attrs="certifications", value_type="country_certification", is_dict=True)
return self._movie_certifications

def tv_certifications(self, reload: bool = False) -> Dict[str, CountryCertifications]:
Expand All @@ -602,10 +598,8 @@ def tv_certifications(self, reload: bool = False) -> Dict[str, CountryCertificat
Dict[str, :class:`~tmdbapis.objs.simple.CountryCertifications`]
"""
if reload or self._tv_certifications is None:
self._tv_certifications = {
k: CountryCertifications(self, v, k) for k, v in
self._api.certifications_get_tv_certifications()["certifications"].items()
}
self._tv_certifications = self._parse(data=self._api.certifications_get_tv_certifications(),
attrs="certifications", value_type="country_certification", is_dict=True)
return self._tv_certifications

def movie_change_list(self,
Expand Down Expand Up @@ -910,7 +904,8 @@ def movie_genres(self, reload: bool = False) -> List[Genre]:
List[:class:`~tmdbapis.objs.simple.Genre`]
"""
if reload or self._movie_genres is None:
self._movie_genres = [Genre(self, g) for g in self._api.genres_get_movie_list()["genres"]]
self._movie_genres = self._parse(data=self._api.genres_get_movie_list(),
attrs="genres", value_type="load_genre", is_list=True)
return self._movie_genres

def tv_genres(self, reload: bool = False) -> List[Genre]:
Expand All @@ -923,7 +918,8 @@ def tv_genres(self, reload: bool = False) -> List[Genre]:
List[:class:`~tmdbapis.objs.simple.Genre`]
"""
if reload or self._tv_genres is None:
self._tv_genres = [Genre(self, g) for g in self._api.genres_get_tv_list()["genres"]]
self._tv_genres = self._parse(data=self._api.genres_get_tv_list(),
attrs="genres", value_type="load_genre", is_list=True)
return self._tv_genres

def keyword(self, keyword_id: int, load: bool = True) -> Keyword:
Expand Down

0 comments on commit 4fb8371

Please sign in to comment.