-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
50 lines (39 loc) · 1.07 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import dotenv
import requests
dotenv.load_dotenv()
IMDB_API_KEY = os.getenv("IMDB_API_KEY")
def search_movie(query: str) -> list:
response = requests.get(
f"https://imdb-api.com/en/API/SearchMovie/{IMDB_API_KEY}/{query}"
)
if response:
result = response.json()
return result.get("results") or []
else:
return []
def title(imdb_id: str) -> dict:
# which data to extract from API result
keyFilter = [
"id",
"title",
"year",
"releaseDate",
"runtimeMins",
"image",
"languages",
"genres",
"plot",
]
response = requests.get(
f"https://imdb-api.com/en/API/Title/{IMDB_API_KEY}/{imdb_id}"
)
if response:
result = response.json()
return {key: result[key] for key in keyFilter}
else:
return {}
def get_top10() -> list:
response = requests.get(f"https://imdb-api.com/en/API/Top250Movies/{IMDB_API_KEY}")
result = response.json()
return [title(item["id"]) for item in result["items"][:10]]