Skip to content

Commit cc47302

Browse files
committed
Add hidden discover methods
1 parent c70dc7e commit cc47302

File tree

8 files changed

+205
-213
lines changed

8 files changed

+205
-213
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

tmdbapis/api3.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import logging
22
from json.decoder import JSONDecodeError
33
from typing import Dict, Optional, Union
4-
54
from requests import Session, Response
65
from requests.exceptions import RequestException
7-
86
from tmdbapis.api4 import API4
97
from tmdbapis.exceptions import TMDbException, NotFound, Unauthorized, WritePermission, PrivateResource, Authentication
108

@@ -821,6 +819,8 @@ def discover_movie_discover(self, **kwargs) -> Dict:
821819
with_runtime.gte (Optional[int]): Filter and only include movies that have a runtime that is greater or equal to a value.
822820
with_runtime.lte (Optional[int]): Filter and only include movies that have a runtime that is less than or equal to a value.
823821
with_original_language (Optional[str]): Specify an ISO 639-1 string to filter results by their original language value.
822+
with_title_translation (Optional[str]): Specify a Primary Translation string to filter results by their title translation value.
823+
with_overview_translation (Optional[str]): Specify a Primary Translation string to filter results by their overview translation value.
824824
with_watch_providers (Optional[str]): A comma or pipe separated list of watch provider ID's. Combine this filter with ``watch_region`` in order to filter your results by a specific watch provider in a specific region.
825825
watch_region (Optional[str]): An ISO 3166-1 code. Combine this filter with ``with_watch_providers`` in order to filter your results by a specific watch provider in a specific region.
826826
with_watch_monetization_types (Optional[str]): In combination with ``watch_region``, you can filter by monetization type. Allowed Values: ``flatrate``, ``free``, ``ads``, ``rent``, ``buy``
@@ -869,6 +869,8 @@ def discover_tv_discover(self, **kwargs) -> Dict:
869869
screened_theatrically (Optional[bool]): Filter results to include items that have been screened theatrically.
870870
with_companies (Optional[str]): A comma separated list of production company ID's. Only include movies that have one of the ID's added as a production company.
871871
with_keywords (Optional[str]): A comma separated list of keyword ID's. Only includes TV shows that have one of the ID's added as a keyword.
872+
with_name_translation (Optional[str]): Specify a Primary Translation string to filter results by their name translation value.
873+
with_overview_translation (Optional[str]): Specify a Primary Translation string to filter results by their overview translation value.
872874
with_watch_providers (Optional[str]): A comma or pipe separated list of watch provider ID's. Combine this filter with ``watch_region`` in order to filter your results by a specific watch provider in a specific region.
873875
watch_region (Optional[str]): An ISO 3166-1 code. Combine this filter with ``with_watch_providers`` in order to filter your results by a specific watch provider in a specific region.
874876
with_watch_monetization_types (Optional[str]): In combination with ``watch_region``, you can filter by monetization type. Allowed Values: ``flatrate``, ``free``, ``ads``, ``rent``, ``buy``

tmdbapis/api4.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import logging
22
from json.decoder import JSONDecodeError
33
from typing import Dict, Optional, Union, List
4-
54
from requests import Session, Response
65
from requests.exceptions import RequestException
7-
8-
from tmdbapis import util
9-
from tmdbapis.exceptions import TMDbException, NotFound, Unauthorized, WritePermission, PrivateResource, Authentication
6+
from tmdbapis.exceptions import TMDbException, NotFound, Unauthorized, WritePermission, PrivateResource, \
7+
Authentication, Invalid
108

119
logger = logging.getLogger(__name__)
1210

@@ -389,6 +387,16 @@ def list_delete_list(self, list_id: int) -> Dict:
389387
"""
390388
return self._delete(f"/list/{list_id}")
391389

390+
def _items(self, items, comment=False):
391+
json = {"items": []}
392+
for item in items:
393+
if "media_type" not in item or "media_id" not in item or item["media_type"] not in ["movie", "tv"] or \
394+
(comment and "comment" not in item):
395+
error = "'media_type', 'media_id', and 'comment'" if comment else "'media_type' and 'media_id'"
396+
raise Invalid(f"Each object must have {error}. 'media_type' must be either 'movie' or 'tv'")
397+
json["items"].append({"media_type": item["media_type"], "media_id": int(item["media_id"])})
398+
return json
399+
392400
def list_add_items(self, list_id: int, items: List[Dict[str, Union[str, int]]]) -> Dict:
393401
""" `List Add Items <https://developers.themoviedb.org/4/list/add-items>`__
394402
@@ -406,7 +414,7 @@ def list_add_items(self, list_id: int, items: List[Dict[str, Union[str, int]]])
406414
list_id (int): List ID
407415
items (List[Dict[str, Union[str, int]]]): List of items to add. Each item is a dictionary with the format {"media_type": str, "media_id": int}. ``media_type`` can either be ``movie`` or ``tv``.
408416
"""
409-
return self._post(f"/list/{list_id}/items", json=util.validate_items(items))
417+
return self._post(f"/list/{list_id}/items", json=self._items(items))
410418

411419
def list_update_items(self, list_id: int, items: List[Dict[str, Union[str, int]]]) -> Dict:
412420
""" `List Update Items <https://developers.themoviedb.org/4/list/update-items>`__
@@ -419,7 +427,7 @@ def list_update_items(self, list_id: int, items: List[Dict[str, Union[str, int]]
419427
list_id (int): List ID
420428
items (List[Dict[str, Union[str, int]]]): List of items to update. Each item is a dictionary with the format {"media_type": str, "media_id": int, "comment": str}. ``media_type`` can either be ``movie`` or ``tv``.
421429
"""
422-
return self._put(f"/list/{list_id}/items", json=util.validate_items(items, comment=True))
430+
return self._put(f"/list/{list_id}/items", json=self._items(items, comment=True))
423431

424432
def list_remove_items(self, list_id: int, items: List[Dict[str, Union[str, int]]]) -> Dict:
425433
""" `List Remove Items <https://developers.themoviedb.org/4/list/remove-items>`__
@@ -432,7 +440,7 @@ def list_remove_items(self, list_id: int, items: List[Dict[str, Union[str, int]]
432440
list_id (int): List ID
433441
items (List[Dict[str, Union[str, int]]]): List of items to remove. Each item is a dictionary with the format {"media_type": str, "media_id": int}. ``media_type`` can either be ``movie`` or ``tv``.
434442
"""
435-
return self._delete(f"/list/{list_id}/items", json=util.validate_items(items))
443+
return self._delete(f"/list/{list_id}/items", json=self._items(items))
436444

437445
def list_check_item_status(self, list_id: int, media_id: int, media_type: str) -> Dict:
438446
""" `List Check Item Status <https://developers.themoviedb.org/4/list/check-item-status>`__

tmdbapis/objs/mixin.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import ABC, abstractmethod
2-
32
from tmdbapis.exceptions import Invalid
43
from tmdbapis.objs.base import TMDbObj
54

tmdbapis/objs/pagination.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
from tmdbapis.objs.base import TMDbObj
66
from tmdbapis.objs.reload import Movie, TVShow
7-
from tmdbapis import util
87
from tmdbapis.exceptions import NotFound, Invalid
98

9+
v3_sorts = ["created_at.asc", "created_at.desc"]
10+
v4_movie_sorts = ["created_at.asc", "created_at.desc", "release_date.asc", "release_date.desc", "title.asc", "title.desc", "vote_average.asc", "vote_average.desc"]
11+
v4_show_sorts = ["created_at.asc", "created_at.desc", "first_air_date.asc", "first_air_date.desc", "name.asc", "name.desc", "vote_average.asc", "vote_average.desc"]
12+
v4_list_sorts = ["original_order.asc", "original_order.desc", "vote_average.asc", "vote_average.desc", "primary_release_date.asc", "primary_release_date.desc", "title.asc", "title.desc"]
1013

1114
class TMDbPagination(TMDbObj):
1215
""" Represents a Pagination Object. The standard iterator only loops through the current page.
@@ -118,6 +121,18 @@ def get_results(self, amount: int):
118121
results.extend(self.results[:amount-len(results)])
119122
return results
120123

124+
def _sort(self, sort_by, v3, is_movie):
125+
if not sort_by:
126+
return None
127+
elif v3 and sort_by not in v3_sorts:
128+
raise Invalid(f"sort_by not in {v3_sorts}")
129+
elif not v3 and is_movie and sort_by not in v4_movie_sorts:
130+
raise Invalid(f"sort_by not in {v4_movie_sorts}")
131+
elif not v3 and not is_movie and sort_by not in v4_show_sorts:
132+
raise Invalid(f"sort_by not in {v4_show_sorts}")
133+
else:
134+
return sort_by
135+
121136

122137
class CreatedLists(TMDbPagination):
123138
""" Paginated Object of the lists created by an account. Will include private lists if you are the owner. """
@@ -165,7 +180,7 @@ class FavoriteMovies(TMDbPagination):
165180
def __init__(self, tmdb, sort_by=None, v3=False):
166181
self._v3 = v3 or tmdb._api4 is None
167182
self._loading = True
168-
self.sort_by = util.validate_sort(sort_by, self._v3, True)
183+
self.sort_by = self._sort(sort_by, self._v3, True)
169184
self._loading = False
170185
super().__init__(tmdb, None, "movie", "FavoriteMovies")
171186

@@ -185,7 +200,7 @@ class FavoriteTVShows(TMDbPagination):
185200
def __init__(self, tmdb, sort_by=None, v3=False):
186201
self._v3 = v3 or tmdb._api4 is None
187202
self._loading = True
188-
self.sort_by = util.validate_sort(sort_by, self._v3, False)
203+
self.sort_by = self._sort(sort_by, self._v3, False)
189204
self._loading = False
190205
super().__init__(tmdb, None, "tv", "FavoriteTVShows")
191206

@@ -204,7 +219,7 @@ class RatedEpisodes(TMDbPagination):
204219
"""
205220
def __init__(self, tmdb, sort_by=None):
206221
self._loading = True
207-
self.sort_by = util.validate_sort(sort_by, True, False)
222+
self.sort_by = self._sort(sort_by, True, False)
208223
self._loading = False
209224
super().__init__(tmdb, None, "episode", "RatedEpisodes")
210225

@@ -221,7 +236,7 @@ class RatedMovies(TMDbPagination):
221236
def __init__(self, tmdb, sort_by=None, v3=False):
222237
self._v3 = v3 or tmdb._api4 is None
223238
self._loading = True
224-
self.sort_by = util.validate_sort(sort_by, self._v3, True)
239+
self.sort_by = self._sort(sort_by, self._v3, True)
225240
self._loading = False
226241
super().__init__(tmdb, None, "movie", "RatedMovies")
227242

@@ -241,7 +256,7 @@ class RatedTVShows(TMDbPagination):
241256
def __init__(self, tmdb, sort_by=None, v3=False):
242257
self._v3 = v3 or tmdb._api4 is None
243258
self._loading = True
244-
self.sort_by = util.validate_sort(sort_by, self._v3, False)
259+
self.sort_by = self._sort(sort_by, self._v3, False)
245260
self._loading = False
246261
super().__init__(tmdb, None, "tv", "RatedTVShows")
247262

@@ -276,7 +291,7 @@ class MovieRecommendations(TMDbPagination):
276291
"""
277292
def __init__(self, tmdb, sort_by=None):
278293
self._loading = True
279-
self.sort_by = util.validate_sort(sort_by, False, True)
294+
self.sort_by = self._sort(sort_by, False, True)
280295
self._loading = False
281296
super().__init__(tmdb, None, "movie", "MovieRecommendations")
282297

@@ -309,7 +324,7 @@ class MovieWatchlist(TMDbPagination):
309324
def __init__(self, tmdb, sort_by=None, v3=False):
310325
self._v3 = v3 or tmdb._api4 is None
311326
self._loading = True
312-
self.sort_by = util.validate_sort(sort_by, self._v3, True)
327+
self.sort_by = self._sort(sort_by, self._v3, True)
313328
self._loading = False
314329
super().__init__(tmdb, None, "movie", "MovieWatchlist")
315330

@@ -739,8 +754,8 @@ def update(self, name: Optional[str] = None, description: Optional[str] = None,
739754
"""
740755
if name is None and description is None and public is None and sort_by is None:
741756
raise Invalid("Must have at least one parameter to update (name, description, public, or sort_by)")
742-
if sort_by and sort_by not in util.v4_list_sorts:
743-
raise Invalid(f"sort_by not in {util.v4_list_sorts}")
757+
if sort_by and sort_by not in v4_list_sorts:
758+
raise Invalid(f"sort_by not in {v4_list_sorts}")
744759
self._tmdb._v4_check().list_update_list(
745760
self.id,
746761
name=name,
@@ -920,7 +935,7 @@ class TVShowRecommendations(TMDbPagination):
920935
"""
921936
def __init__(self, tmdb, sort_by=None):
922937
self._loading = True
923-
self.sort_by = util.validate_sort(sort_by, False, False)
938+
self.sort_by = self._sort(sort_by, False, False)
924939
self._loading = False
925940
super().__init__(tmdb, None, "tv", "TVShowRecommendations")
926941

@@ -955,7 +970,7 @@ class TVShowWatchlist(TMDbPagination):
955970
def __init__(self, tmdb, sort_by=None, v3=False):
956971
self._v3 = v3 or tmdb._api4 is None
957972
self._loading = True
958-
self.sort_by = util.validate_sort(sort_by, self._v3, False)
973+
self.sort_by = self._sort(sort_by, self._v3, False)
959974
self._loading = False
960975
super().__init__(tmdb, None, "tv", "TVShowWatchlist")
961976

tmdbapis/objs/reload.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from abc import abstractmethod
22
from typing import Optional, Union
3-
43
from tmdbapis.objs.base import TMDbObj
54
from tmdbapis.objs.mixin import Favorite, Rate, Watchlist
65

0 commit comments

Comments
 (0)