Skip to content

Commit

Permalink
Merge pull request #40 from dbatten5/search-movie-region
Browse files Browse the repository at this point in the history
Search movie region
  • Loading branch information
dbatten5 authored Nov 20, 2021
2 parents 5b0e4ad + 82f0eae commit c9a8e12
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 101 deletions.
19 changes: 18 additions & 1 deletion docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ through `search_tmdb_movies`. Note that this search performs a lot quicker than
'overview': 'Set in the 22nd century, The Matrix tells the story of a computer hacker...'
'popularity': 79.956,
'poster_path': '/f89U3ADr1oiB1s9GkdPOEpXUk5H.jpg',
'release_date': '1999-06-11',
'release_date': '1999-03-30',
'title': 'The Matrix',
'video': False,
'vote_average': 8.2,
Expand All @@ -68,6 +68,23 @@ through `search_tmdb_movies`. Note that this search performs a lot quicker than
}
```

By default the `release_date` will be the US release date. You can specify a different
region by providing a region argument:

```python
>>> from phylm.tools import search_tmdb_movies
>>> search_tmdb_movies("The Matrix", region="gb")
[{
'id': 603,
...
'release_date': '1999-06-11',
'title': 'The Matrix',
...
}, {
...
}
```

::: phylm.tools.search_tmdb_movies
rendering:
show_signature_annotations: true
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "phylm"
version = "4.3.0"
version = "4.3.1"
description = "Phylm"
authors = ["Dom Batten <[email protected]>"]
license = "MIT"
Expand Down
12 changes: 8 additions & 4 deletions src/phylm/clients/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ def __init__(self, api_key: str) -> None:
self.api_key = api_key
self._base = "https://api.themoviedb.org/3"

def search_movies(self, query: str) -> List[Dict[str, Any]]:
def search_movies(self, query: str, region: str = "us") -> List[Dict[str, Any]]:
"""Search for movies.
Args:
query: the search query
region: the region for the query, affects the release date value
Returns:
Any: the search results
List[Dict[str, Any]]: the search results
"""
payload = {
"api_key": self.api_key,
"language": "en-GB",
"language": "en-US",
"query": query,
"include_adult": "false",
"region": "GB",
"region": region.upper(),
}
res = self.session.get(f"{self._base}/search/movie", params=payload)

res.raise_for_status()

results: List[Dict[str, Any]] = res.json()["results"]
return results

Expand Down
7 changes: 5 additions & 2 deletions src/phylm/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,24 @@ def _initialize_tmdb_client(api_key: Optional[str] = None) -> TmdbClient:


def search_tmdb_movies(
query: str, api_key: Optional[str] = None
query: str, api_key: Optional[str] = None, region: str = "us"
) -> List[Dict[str, Any]]:
"""Search for movies on TMDb.
Args:
query: the query string
api_key: an api_key can either be provided here or through a TMDB_API_KEY env
var
region: an optional region to provide with the search request, affects the
release_date value returned, must be provided in ISO 3166-1 format (eg.
"us" or "gb")
Returns:
List[Dict[str, Any]]: the search results
"""
client = _initialize_tmdb_client(api_key=api_key)

return client.search_movies(query=query)
return client.search_movies(query=query, region=region)


def get_streaming_providers(
Expand Down
53 changes: 53 additions & 0 deletions tests/fixtures/vcr_cassettes/clients/tmdb/invalid_key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
interactions:
- request:
body: null
headers:
Accept:
- "*/*"
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.26.0
method: GET
uri: https://api.themoviedb.org/3/search/movie?include_adult=false&language=en-US&query=The+Matrix&region=US
response:
body:
string:
'{"status_code":7,"status_message":"Invalid API key: You must be granted
a valid key.","success":false}
'
headers:
Access-Control-Allow-Methods:
- GET, HEAD, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:
- "*"
Access-Control-Expose-Headers:
- ETag, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After,
Content-Length, Content-Range
Cache-Control:
- public, max-age=300
Connection:
- keep-alive
Content-Type:
- application/json; charset=utf-8
Date:
- Sat, 20 Nov 2021 10:19:07 GMT
Server:
- openresty
Transfer-Encoding:
- chunked
Via:
- 1.1 bdbd00aed84ab2a1b2623adb63c9331f.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- oeHPChCFqG9v3RhOYMdD7zFVgpREaR4uyeTjy26ALcug3h1GcYPHOg==
X-Amz-Cf-Pop:
- LHR61-P1
X-Cache:
- Error from cloudfront
status:
code: 401
message: Unauthorized
version: 1
137 changes: 137 additions & 0 deletions tests/fixtures/vcr_cassettes/clients/tmdb/invalid_region.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
interactions:
- request:
body: null
headers:
Accept:
- "*/*"
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.26.0
method: GET
uri: https://api.themoviedb.org/3/search/movie?include_adult=false&language=en-US&query=The+Matrix&region=XX
response:
body:
string: !!binary |
H4sIAAAAAAAAA61b23LbOrL9FZQeJmeqZG1Rd/k8pBzbsZ34bidOfHIqBZGQCIskaICULE9N1fzD
PJ3fmy85qwHqLiux965KJTYvIIBevXp1N/KPUsoHorTrlUtamDzKTGn3f/5R4gF+LO32eWREudTj
/jDQKv2Z8iws7Zb+6J8ftWX7+vHsUA/9x1CFX2u85X/1DsNJ5SEdlMqlgUi0+CkDGq7WKXfanf8t
l2RQ2m1V6+WS0nIgEx79jHgyyO0ESiIpLdzIZBbR1dtQsDOeaflEd0dCj6QY4/qNyJhMWIbbtVoS
MF8kWa4nZTZ/gWUiiox9xGRKT5jqM858Fad5JjQLsSj8Mw4Ve1AyMbg30CpP6bE8CYSm3zAybuUa
y8kM68tBmMlkYMcccZMxjgdSNRa6n0ezoY0dNFFjpvNI2IcF11lYwRJSleYR1zKblHY77Uq3RZcM
Xprvbaf7pb53oD0lP3imezQMLi8O029fhs3jYm+1iAQ34mfAM9oir9vt7lTrO/Uq7m3ct5EMhJpZ
c6Qy8ZNjK+2+dyq14pKP5cLmtWqt1vxn+ZcYqJ9Leb33OaudNntf8r3RzcVw1PqetPPW7WYMeLVF
GNQanVb1bUhg10Cq1sLPpErMMi72YIUENso53SRbzu0vTMZ7kTShC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HdSGpbAilSLsdI+9iTCRTDGOGj
zPIkxWbNCnjuFHiqMDmilzJLzYTqFkU1xX5c+6HIJil+jcBY0cy7qC+zdpwM0rS6XsJtj+4vH+rR
xxu/JT6Z02EzeR49cn+kj65fcnWvQ10XbzF1eFPXpbWaycPWNjr9pP9oAvPXp7/P/r9Jw/vn/wNt
3mR1hzIAAA==
headers:
Access-Control-Allow-Methods:
- GET, HEAD, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:
- "*"
Access-Control-Expose-Headers:
- ETag, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After,
Content-Length, Content-Range
Age:
- "65"
Cache-Control:
- public, max-age=600
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json;charset=utf-8
Date:
- Sat, 20 Nov 2021 10:16:24 GMT
Server:
- openresty
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
Via:
- 1.1 774a6d3757afff80abe764a68fbbe545.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- yeb9F8W_my08XUB0tj3_7DWMS65xo3oVnADXkSANNZSm6XSSVIJtRw==
X-Amz-Cf-Pop:
- LHR50-C1
X-Cache:
- Hit from cloudfront
status:
code: 200
message: OK
version: 1
10 changes: 5 additions & 5 deletions tests/fixtures/vcr_cassettes/clients/tmdb/no_results.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interactions:
User-Agent:
- python-requests/2.26.0
method: GET
uri: https://api.themoviedb.org/3/search/movie?include_adult=false&language=en-GB&query=aslkdjaskldjaslkdjaslkdjasd&region=GB
uri: https://api.themoviedb.org/3/search/movie?include_adult=false&language=en-US&query=aslkdjaskldjaslkdjaslkdjasd&region=US
response:
body:
string: !!binary |
Expand All @@ -34,19 +34,19 @@ interactions:
Content-Type:
- application/json;charset=utf-8
Date:
- Fri, 19 Nov 2021 14:23:36 GMT
- Sat, 20 Nov 2021 10:13:00 GMT
Server:
- openresty
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
Via:
- 1.1 68126347056de2d05be3dd362ccba987.cloudfront.net (CloudFront)
- 1.1 38625201ad7f896d72a7ab055328881f.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- MZgNnx0ZejBqMqIgXJ86OP5OtMBnbboSC5jTI8w_I5BKmv2vpMLJLA==
- 4PLVZIxaLwY7MEg16ZKq9lvl2_7L69aF_s3fDB7kmtMiKdyPxYC2IA==
X-Amz-Cf-Pop:
- LHR50-C1
- LHR52-C1
X-Cache:
- Miss from cloudfront
status:
Expand Down
Loading

0 comments on commit c9a8e12

Please sign in to comment.