Skip to content

Commit 0416014

Browse files
Initial implementation of "popular" function
Docstrings and unit tests are still missing.
1 parent 63ad2b2 commit 0416014

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/simplejustwatchapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from simplejustwatchapi.justwatch import details as details
44
from simplejustwatchapi.justwatch import episodes as episodes
55
from simplejustwatchapi.justwatch import offers_for_countries as offers_for_countries
6+
from simplejustwatchapi.justwatch import popular as popular
67
from simplejustwatchapi.justwatch import search as search
78
from simplejustwatchapi.justwatch import seasons as seasons
89
from simplejustwatchapi.tuples import Episode as Episode
@@ -24,6 +25,7 @@
2425
"details",
2526
"episodes",
2627
"offers_for_countries",
28+
"popular",
2729
"search",
2830
"seasons",
2931
]

src/simplejustwatchapi/graphql.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@
4444
}
4545
"""
4646

47+
_GRAPHQL_POPULAR_QUERY = """
48+
query GetPopularTitles(
49+
$popularTitlesFilter: TitleFilter
50+
$country: Country!
51+
$language: Language!
52+
$first: Int! = 70
53+
$formatPoster: ImageFormat,
54+
$formatOfferIcon: ImageFormat,
55+
$profile: PosterProfile
56+
$backdropProfile: BackdropProfile,
57+
$filter: OfferFilter!,
58+
$offset: Int = 0
59+
) {
60+
popularTitles(
61+
country: $country
62+
filter: $popularTitlesFilter
63+
first: $first
64+
sortBy: POPULAR
65+
sortRandomSeed: 0
66+
offset: $offset
67+
) {
68+
__typename
69+
edges {
70+
node {
71+
...TitleDetails
72+
__typename
73+
}
74+
__typename
75+
}
76+
}
77+
}
78+
"""
79+
4780
_GRAPHQL_DETAILS_QUERY = """
4881
query GetTitleNode(
4982
$nodeId: ID!,
@@ -274,6 +307,10 @@ def graphql_search_query() -> str:
274307
return _GRAPHQL_SEARCH_QUERY + _GRAPHQL_DETAILS_FRAGMENT + _GRAPHQL_OFFER_FRAGMENT
275308

276309

310+
def graphql_popular_query() -> str:
311+
return _GRAPHQL_POPULAR_QUERY + _GRAPHQL_DETAILS_FRAGMENT + _GRAPHQL_OFFER_FRAGMENT
312+
313+
277314
def graphql_details_query() -> str:
278315
"""
279316
Prepare GraphQL query used for getting details regarding a single entry.

src/simplejustwatchapi/justwatch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
parse_details_response,
77
parse_episodes_response,
88
parse_offers_for_countries_response,
9+
parse_popular_response,
910
parse_search_response,
1011
parse_seasons_response,
1112
prepare_details_request,
1213
prepare_episodes_request,
1314
prepare_offers_for_countries_request,
15+
prepare_popular_request,
1416
prepare_search_request,
1517
prepare_seasons_request,
1618
)
@@ -66,6 +68,20 @@ def search(
6668
return parse_search_response(response.json())
6769

6870

71+
def popular(
72+
country: str = "US",
73+
language: str = "en",
74+
count: int = 4,
75+
best_only: bool = True,
76+
offset: int = 0,
77+
providers: list[str] | str | None = None,
78+
) -> list[MediaEntry]:
79+
request = prepare_popular_request(country, language, count, best_only, offset, providers)
80+
response = post(_GRAPHQL_API_URL, json=request)
81+
response.raise_for_status()
82+
return parse_popular_response(response.json())
83+
84+
6985
def details(
7086
node_id: str,
7187
country: str = "US",

src/simplejustwatchapi/query.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
graphql_details_query,
1111
graphql_episodes_query,
1212
graphql_offers_for_countries_query,
13+
graphql_popular_query,
1314
graphql_search_query,
1415
graphql_seasons_query,
1516
)
@@ -96,6 +97,37 @@ def parse_search_response(json: dict) -> list[MediaEntry]:
9697
return [_parse_entry(edge["node"]) for edge in json["data"]["popularTitles"]["edges"]]
9798

9899

100+
def prepare_popular_request(
101+
country: str,
102+
language: str,
103+
count: int,
104+
best_only: bool,
105+
offset: int,
106+
providers: list[str] | str | None,
107+
) -> dict:
108+
_assert_country_code_is_valid(country)
109+
return {
110+
"operationName": "GetPopularTitles",
111+
"variables": {
112+
"first": count,
113+
"popularTitlesFilter": {"packages": providers},
114+
"language": language,
115+
"country": country.upper(),
116+
"formatPoster": "JPG",
117+
"formatOfferIcon": "PNG",
118+
"profile": "S718",
119+
"backdropProfile": "S1920",
120+
"filter": {"bestOnly": best_only},
121+
"offset": offset or None,
122+
},
123+
"query": graphql_popular_query(),
124+
}
125+
126+
127+
def parse_popular_response(json: dict) -> list[MediaEntry]:
128+
return [_parse_entry(edge["node"]) for edge in json["data"]["popularTitles"]["edges"]]
129+
130+
99131
def prepare_details_request(node_id: str, country: str, language: str, best_only: bool) -> dict:
100132
"""
101133
Prepare a details request for specified node ID to JustWatch GraphQL API.

0 commit comments

Comments
 (0)