Skip to content

Commit 026a185

Browse files
Update docstrings
1 parent 417e9ce commit 026a185

4 files changed

Lines changed: 159 additions & 5 deletions

File tree

src/simplejustwatchapi/graphql.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def graphql_search_query() -> str:
322322
"""
323323
Prepare GraphQL query used for searching for entries.
324324
325-
The query is GetSearchTitles query + details fragment + offers fragment.
325+
The query is GetSearchTitles query + details fragment + offers fragment + package fragment.
326326
327327
Returns:
328328
str: Full GraphQL "search" query.
@@ -337,6 +337,15 @@ def graphql_search_query() -> str:
337337

338338

339339
def graphql_popular_query() -> str:
340+
"""
341+
Prepare GraphQL query used for looking up currently popular titles.
342+
343+
The query is GetPopularTitles query + details fragment + offers fragment + package fragment.
344+
345+
Returns:
346+
str: Full GraphQL "popular" query.
347+
348+
"""
340349
return (
341350
_GRAPHQL_POPULAR_QUERY
342351
+ _GRAPHQL_DETAILS_FRAGMENT
@@ -346,14 +355,23 @@ def graphql_popular_query() -> str:
346355

347356

348357
def graphql_providers_query() -> str:
358+
"""
359+
Prepare GraphQL query used for looking up all providers for a given country.
360+
361+
The query is GetProviders query + package fragment.
362+
363+
Returns:
364+
str: Full GraphQL "providers" query.
365+
366+
"""
349367
return _GRAPHQL_PROVIDERS_QUERY + _GRAPHQL_PACKAGE_FRAGMENT
350368

351369

352370
def graphql_details_query() -> str:
353371
"""
354372
Prepare GraphQL query used for getting details regarding a single entry.
355373
356-
The full query is GetTitleNode query + details fragment + offers fragment.
374+
The full query is GetTitleNode query + details fragment + offers fragment + package fragment.
357375
It is meant for movies and shows, but can be used for seasons and episodes as well,
358376
it just won't return full season/episodes list.
359377
@@ -373,7 +391,8 @@ def graphql_seasons_query() -> str:
373391
"""
374392
Prepare GraphQL query used for getting a list of seasons for a single show.
375393
376-
The full query is GetTitleNode query (with seasons list) + details fragment + offers fragment.
394+
The full query is GetTitleNode query (with seasons list) + details fragment + offers fragment
395+
+ package fragment.
377396
It will only return data for shows with a list of all available seasons, ascending.
378397
Details query itself matches :func:`graphql_details_query`, its conditions will return all
379398
relevant data for seasons.
@@ -394,7 +413,8 @@ def graphql_episodes_query() -> str:
394413
"""
395414
Prepare GraphQL query used for getting a list of episodes for a single show season.
396415
397-
The full query is GetTitleNode query (with episodes list) + details fragment + offers fragment.
416+
The full query is GetTitleNode query (with episodes list) + details fragment + offers fragment
417+
+ package fragment.
398418
It will only return data for show seasons with a list of all available episodes, ascending.
399419
Details query itself matches :func:`graphql_details_query`, its conditions will return all
400420
relevant data for episodes.

src/simplejustwatchapi/justwatch.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@ def search(
5050
in 4K, HD and SD, using ``best_only = True`` returns only 4K option, ``best_only = False``
5151
returns all three.
5252
53+
``providers`` is a list of (usually) 3-letter service identifiers (e.g, ``nfx`` for "Netflix).
54+
Only entries which are available for given providers will be returned.
55+
For single provider you can either pass a single string, or a list of one string.
56+
For ``None`` (also a default value) entries for all providers will be looked up.
57+
Invalid names will be ignored, however if all are invalid, then no filtering will be done.
58+
You can look up values through ``providers`` function.
59+
5360
Args:
5461
title (str): Title to search.
5562
country (str): Country to search for offers, ``US`` by default.
5663
language (str): Language of responses, ``en`` by default.
5764
count (int): How many responses should be returned.
5865
best_only (bool): Return only best offers if ``True``, return all offers if ``False``.
5966
offset (int): Search results offset.
67+
providers (list[str] | str | None): 3-letter service identifier(s),
68+
or ``None`` for all providers.
6069
6170
Returns:
6271
list[MediaEntry]: List of ``MediaEntry`` NamedTuples parsed from JustWatch response.
@@ -79,6 +88,47 @@ def popular(
7988
offset: int = 0,
8089
providers: list[str] | str | None = None,
8190
) -> list[MediaEntry]:
91+
"""
92+
Look up all currently popular titles on JustWatch.
93+
94+
Returns a list of entries up to ``count``.
95+
96+
``offset`` specifies how many first entries should be skipped. This is done on API side,
97+
not the library side; the returned list is still directly parsed from API response.
98+
I'm not sure if it guarantees stability of results - whether repeated calls to this function
99+
with increasing offset will guarantee that you won't get repeats.
100+
101+
JustWatch API won't allow for getting more than 2000 responses, either through ``count``, or
102+
when ``count + offset`` is equal or greater than 2000 - it will return an empty list instead
103+
(ALWAYS an empty list, if ``offset`` is lower than 2000 it won't include entries up to 2000).
104+
105+
``best_only`` allows filtering out redundant offers, e.g. when service provides offers
106+
in 4K, HD and SD, using ``best_only = True`` returns only 4K option, ``best_only = False``
107+
returns all three.
108+
109+
``providers`` is a list of (usually) 3-letter service identifiers (e.g, ``nfx`` for "Netflix).
110+
Only entries which are available for given providers will be returned.
111+
For single provider you can either pass a single string, or a list of one string.
112+
For ``None`` (also a default value) entries for all providers will be looked up.
113+
Invalid names will be ignored, however if all are invalid, then no filtering will be done.
114+
You can look up values through ``providers`` function.
115+
116+
Args:
117+
country (str): Country to search for offers, ``US`` by default.
118+
language (str): Language of responses, ``en`` by default.
119+
count (int): How many responses should be returned.
120+
best_only (bool): Return only best offers if ``True``, return all offers if ``False``.
121+
offset (int): Search results offset.
122+
providers (list[str] | str | None): 3-letter service identifier(s),
123+
or ``None`` for all providers.
124+
125+
Returns:
126+
list[MediaEntry]: List of ``MediaEntry`` NamedTuples parsed from JustWatch response.
127+
128+
Raises:
129+
httpx.HTTPStatusError: If JustWatch API doesn't respond with success code.
130+
131+
"""
82132
request = prepare_popular_request(country, language, count, best_only, offset, providers)
83133
response = post(_GRAPHQL_API_URL, json=request)
84134
response.raise_for_status()
@@ -238,6 +288,18 @@ def offers_for_countries(
238288

239289

240290
def providers(country: str) -> list[OfferPackage]:
291+
"""
292+
Look up all providers for the given country code.
293+
294+
Args:
295+
country (str): 2-letter country code.
296+
297+
Returns:
298+
list[OfferPackage]: List of all found providers. ``OfferPackage`` tuple matches
299+
values in ``Offer`` (and thus in ``MediaEntry``), but the data structure is the same,
300+
so the same tuple is reused.
301+
302+
"""
241303
request = prepare_providers_request(country)
242304
response = post(_GRAPHQL_API_URL, json=request)
243305
response.raise_for_status()

src/simplejustwatchapi/query.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def prepare_search_request(
5555
count (int): How many responses should be returned.
5656
best_only (bool): Return only best offers if ``True``, return all offers if ``False``.
5757
offset (int): Search results offset.
58+
providers (list[str] | str | None): 3-letter service identifier(s),
59+
or ``None`` for all providers.
5860
5961
Returns:
6062
dict: JSON/dict with GraphQL POST body.
@@ -107,6 +109,28 @@ def prepare_popular_request(
107109
offset: int,
108110
providers: list[str] | str | None,
109111
) -> dict:
112+
"""
113+
Prepare "get popular" request for JustWatch GraphQL API.
114+
115+
Creates a ``GetPopularTitles`` GraphQL query.
116+
117+
Country code should be two uppercase letters, however it will be auto-converted to uppercase.
118+
119+
Meant to be used together with :func:`parse_popular_response`.
120+
121+
Args:
122+
country (str): Country to search for offers.
123+
language (str): Language of responses.
124+
count (int): How many responses should be returned.
125+
best_only (bool): Return only best offers if ``True``, return all offers if ``False``.
126+
offset (int): Search results offset.
127+
providers (list[str] | str | None): 3-letter service identifier(s),
128+
or ``None`` for all providers.
129+
130+
Returns:
131+
dict: JSON/dict with GraphQL POST body.
132+
133+
"""
110134
_assert_country_code_is_valid(country)
111135
return {
112136
"operationName": "GetPopularTitles",
@@ -127,6 +151,22 @@ def prepare_popular_request(
127151

128152

129153
def parse_popular_response(json: dict) -> list[MediaEntry]:
154+
"""
155+
Parse response from the "get popular" query from JustWatch GraphQL API.
156+
157+
Parses response for ``GetPopularTitles`` query.
158+
159+
If API didn't return any data, then an empty list is returned.
160+
161+
Meant to be used together with :func:`prepare_popular_request`.
162+
163+
Args:
164+
json (dict): JSON returned by JustWatch GraphQL API.
165+
166+
Returns:
167+
list[MediaEntry]: Parsed received JSON as a list of ``MediaEntry`` NamedTuples.
168+
169+
"""
130170
return [_parse_entry(edge["node"]) for edge in json["data"]["popularTitles"]["edges"]]
131171

132172

@@ -390,19 +430,50 @@ def parse_offers_for_countries_response(json: dict, countries: set[str]) -> dict
390430

391431

392432
def prepare_providers_request(country: str) -> dict:
433+
"""
434+
Prepare "get all providers" request for JustWatch GraphQL API.
435+
436+
Creates a ``GetProviders`` GraphQL query.
437+
438+
Country code should be two uppercase letters, however it will be auto-converted to uppercase.
439+
440+
Meant to be used together with :func:`parse_providers_response`.
441+
442+
Args:
443+
country (str): 2-letter country code for which providers should be looked up.
444+
445+
Returns:
446+
dict: JSON/dict with GraphQL POST body.
447+
448+
"""
393449
_assert_country_code_is_valid(country)
394450
return {
395451
"operationName": "GetProviders",
396452
"variables": {
397453
"country": country.upper(),
398454
"formatOfferIcon": "PNG",
399-
"fullPath": "/us/tv-show/the-madison",
400455
},
401456
"query": graphql_providers_query(),
402457
}
403458

404459

405460
def parse_providers_response(json: dict) -> list[OfferPackage]:
461+
"""
462+
Parse response from "get all providers" query from JustWatch GraphQL API.
463+
464+
Parses response for ``GetProviders`` query.
465+
466+
If API didn't return any data, then an empty list is returned.
467+
468+
Meant to be used together with :func:`prepare_providers_request`.
469+
470+
Args:
471+
json (dict): JSON returned by JustWatch GraphQL API.
472+
473+
Returns:
474+
list[MediaEntry]: Parsed received JSON as a list of ``OfferPackage`` NamedTuples.
475+
476+
"""
406477
return [_parse_package(package) for package in json["data"]["packages"]]
407478

408479

src/simplejustwatchapi/tuples.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class OfferPackage(NamedTuple):
1616
name (str): Name of the platform in format suited to display for users.
1717
technical_name (str): Technical name of the platform,
1818
usually all lowercase with no whitespaces.
19+
short_name (str): 3-letter provider name.
1920
icon (str): Platform icon URL.
2021
2122
"""

0 commit comments

Comments
 (0)