Skip to content

Commit 7011402

Browse files
Add filtering by release year and object type
1 parent 6236ce8 commit 7011402

2 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/simplejustwatchapi/justwatch.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def search(
7979
best_only: bool = True,
8080
offset: int = 0,
8181
providers: list[str] | str | None = None,
82+
min_year: int | None = None,
83+
max_year: int | None = None,
84+
object_types: list[str] | str | None = None,
8285
) -> list[MediaEntry]:
8386
"""
8487
Search JustWatch for the given title.
@@ -148,6 +151,18 @@ def search(
148151
You can look up values through [`providers`]
149152
[simplejustwatchapi.justwatch.providers] function.
150153
154+
min_year (int | None): Minimum release year of returned titles.
155+
If `None` (the default value), no filtering is done.
156+
157+
max_year (int | None): Maximum release year of returned titles.
158+
If `None` (the default value), no filtering is done.
159+
160+
object_types (list[str] | str | None): Types of objects to filter for, it seems
161+
that only `SHOW` and `MOVIE` are useful, but it's not strictly enforced.
162+
Types like `SHOW_EPISODE`, or `SHOW_SEASON` can be used but they seem to
163+
return shows, the same as `SHOW` type. If `None` (the default value), no
164+
filtering is done.
165+
151166
Returns:
152167
(list[MediaEntry]): List of tuples with details of search results.
153168
@@ -159,7 +174,16 @@ def search(
159174
160175
"""
161176
request = prepare_search_request(
162-
title, country, language, count, best_only, offset, providers
177+
title,
178+
country,
179+
language,
180+
count,
181+
best_only,
182+
offset,
183+
providers,
184+
min_year,
185+
max_year,
186+
object_types,
163187
)
164188
response = _post_to_jw_graphql_api(request)
165189
return parse_search_response(response)
@@ -172,6 +196,9 @@ def popular(
172196
best_only: bool = True,
173197
offset: int = 0,
174198
providers: list[str] | str | None = None,
199+
min_year: int | None = None,
200+
max_year: int | None = None,
201+
object_types: list[str] | str | None = None,
175202
) -> list[MediaEntry]:
176203
"""
177204
Look up all currently popular titles on JustWatch.
@@ -233,6 +260,18 @@ def popular(
233260
You can look up values through [`providers`]
234261
[simplejustwatchapi.justwatch.providers] function.
235262
263+
min_year (int | None): Minimum release year of returned titles.
264+
If `None` (the default value), no filtering is done.
265+
266+
max_year (int | None): Maximum release year of returned titles.
267+
If `None` (the default value), no filtering is done.
268+
269+
object_types (list[str] | str | None): Types of objects to filter for, it seems
270+
that only `SHOW` and `MOVIE` are useful, but it's not strictly enforced.
271+
Types like `SHOW_EPISODE`, or `SHOW_SEASON` can be used but they seem to
272+
return shows, the same as `SHOW` type. If `None` (the default value), no
273+
filtering is done.
274+
236275
Returns:
237276
(list[MediaEntry]): List of tuples with details of popular titles.
238277
@@ -244,7 +283,15 @@ def popular(
244283
245284
"""
246285
request = prepare_popular_request(
247-
country, language, count, best_only, offset, providers
286+
country,
287+
language,
288+
count,
289+
best_only,
290+
offset,
291+
providers,
292+
min_year,
293+
max_year,
294+
object_types,
248295
)
249296
response = _post_to_jw_graphql_api(request)
250297
return parse_popular_response(response)

src/simplejustwatchapi/query.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def prepare_search_request(
4444
best_only: bool,
4545
offset: int,
4646
providers: list[str] | str | None,
47+
min_year: int | None,
48+
max_year: int | None,
49+
object_types: list[str] | str | None,
4750
) -> dict[str, Any]:
4851
"""
4952
Prepare search request for JustWatch GraphQL API.
@@ -66,6 +69,10 @@ def prepare_search_request(
6669
offset (int): Search results offset.
6770
providers (list[str] | str | None): 3-letter service identifier(s),
6871
or `None` for all providers.
72+
min_year (int | None): Minimum release year of returned titles.
73+
max_year (int | None): Maximum release year of returned titles.
74+
object_types (list[str] | str | None): Types of objects to filter for, it seems
75+
that only "SHOW" and "MOVIE" make sense.
6976
7077
Returns:
7178
(dict[str, Any]): JSON with GraphQL POST body.
@@ -75,7 +82,11 @@ def prepare_search_request(
7582
"operationName": "GetSearchTitles",
7683
"variables": {
7784
"first": count,
78-
"searchTitlesFilter": {"searchQuery": title, "packages": providers},
85+
"searchTitlesFilter": {
86+
"searchQuery": title,
87+
"packages": providers,
88+
**_list_variables(min_year, max_year, object_types),
89+
},
7990
**_common_variables(best_only),
8091
**_locale_variables(country, language),
8192
"offset": offset or None,
@@ -118,6 +129,9 @@ def prepare_popular_request(
118129
best_only: bool,
119130
offset: int,
120131
providers: list[str] | str | None,
132+
min_year: int | None,
133+
max_year: int | None,
134+
object_types: list[str] | str | None,
121135
) -> dict[str, Any]:
122136
"""
123137
Prepare "get popular" request for JustWatch GraphQL API.
@@ -139,6 +153,10 @@ def prepare_popular_request(
139153
offset (int): Search results offset.
140154
providers (list[str] | str | None): 3-letter service identifier(s),
141155
or `None` for all providers.
156+
min_year (int | None): Minimum release year of returned titles.
157+
max_year (int | None): Maximum release year of returned titles.
158+
object_types (list[str] | str | None): Types of objects to filter for, it seems
159+
that only "SHOW" and "MOVIE" make sense.
142160
143161
Returns:
144162
(dict[str, Any]): JSON with GraphQL POST body.
@@ -148,7 +166,10 @@ def prepare_popular_request(
148166
"operationName": "GetPopularTitles",
149167
"variables": {
150168
"first": count,
151-
"popularTitlesFilter": {"packages": providers},
169+
"popularTitlesFilter": {
170+
"packages": providers,
171+
**_list_variables(min_year, max_year, object_types),
172+
},
152173
**_common_variables(best_only),
153174
**_locale_variables(country, language),
154175
"offset": offset or None,
@@ -507,6 +528,16 @@ def _locale_variables(country: str, language: str) -> dict[str, str]:
507528
return {"country": country.upper(), "language": language}
508529

509530

531+
def _list_variables(
532+
min_year: int | None, max_year: int | None, object_types: list[str] | str | None
533+
) -> dict[str, Any]:
534+
"""Return dict with variables related to looking up lists of titles."""
535+
return {
536+
"objectTypes": object_types,
537+
"releaseYear": {"min": min_year, "max": max_year},
538+
}
539+
540+
510541
def _raise_for_errors_in_response(json: dict[str, Any]) -> None:
511542
"""Raise JustWatchApiError if given JSON contains `errors` key."""
512543
if "errors" in json:

0 commit comments

Comments
 (0)