Skip to content

Commit 54cf7a8

Browse files
Tweak requests module
Convert country code to uppercase. JustWatch returns an error otherwise. Assert that country code is 2 characters long, JustWatch returns an error otherwise. Fix typos.
1 parent 455679a commit 54cf7a8

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/simplejustwatchpythonapi/requests.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GRAPHQL_SEARCH_QUERY = """
1+
_GRAPHQL_SEARCH_QUERY = """
22
query GetSearchTitles(
33
$searchTitlesFilter: TitleFilter!,
44
$country: Country!,
@@ -74,30 +74,35 @@
7474
"""
7575

7676

77-
def prepare_search_request(title: str, county: str, lang: str, count: int, best_only: bool) -> dict:
77+
def prepare_search_request(
78+
title: str, country: str, language: str, count: int, best_only: bool
79+
) -> dict:
7880
"""Prepare search request for JustWatch GraphQL API.
81+
Country code should be two uppercase characters,
82+
however lowercase code will be converted to uppercase.
7983
8084
Args:
8185
title: title to search
82-
county: country to search for offers
83-
lang: language of responses
86+
country: country to search for offers
87+
language: language of responses
8488
count: how many responses should be returned
8589
best_only: return only best offers if True, return all offers if False
8690
8791
Returns:
8892
JSON/dict with GraphQL POST body
8993
"""
94+
assert len(country) == 2, f"Invalid country code: {country}, code must be 2 characters long"
9095
return {
9196
"operationName": "GetSearchTitles",
9297
"variables": {
9398
"first": count,
9499
"searchTitlesFilter": {"searchQuery": title},
95-
"language": lang,
96-
"country": county,
100+
"language": language,
101+
"country": country.upper(),
97102
"format": "JPG",
98103
"profile": "S718",
99104
"backdropProfile": "S1920",
100105
"filter": {"bestOnly": best_only},
101106
},
102-
"query": GRAPHQL_SEARCH_QUERY,
107+
"query": _GRAPHQL_SEARCH_QUERY,
103108
}

0 commit comments

Comments
 (0)