-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconnect.py
86 lines (74 loc) · 2.13 KB
/
connect.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
import pprint
import pandas as pd
api_key = "<your v3 key>"
api_key_v4 = "<your v4 key>"
# HTTP request METHODS
"""
GET -> grab data
POST -> add/update data
PATCH
PUT
DELETE
"""
# what's our endpoint (or a url)?
# what is the HTTP method that we need?
"""
Endpoint
/movie/{movie_id}
https://api.themoviedb.org/3/movie/550?api_key=075937068a3817fa489588e754320646
"""
movie_id = 500
api_version = 3
api_base_url = f"https://api.themoviedb.org/{api_version}"
endpoint_path = f"/movie/{movie_id}"
endpoint = f"{api_base_url}{endpoint_path}?api_key={api_key}&page=1"
print(endpoint)
# r = requests.get(endpoint) # json={"api_key": api_key})
# print(r.status_code)
# print(r.text)
# Using v4
movie_id = 501
api_version = 4
api_base_url = f"https://api.themoviedb.org/{api_version}"
endpoint_path = f"/movie/{movie_id}"
endpoint = f"{api_base_url}{endpoint_path}"
headers = {
'Authorization': f'Bearer {api_key_v4}',
'Content-Type': 'application/json;charset=utf-8'
}
# r = requests.get(endpoint, headers=headers) # json={"api_key": api_key})
# print(r.status_code)
# print(r.text)
api_base_url = f"https://api.themoviedb.org/{api_version}"
endpoint_path = f"/search/movie"
searh_query = "The Matrix"
endpoint = f"{api_base_url}{endpoint_path}?api_key={api_key}&query={searh_query}"
# print(endpoint)
r = requests.get(endpoint)
# pprint.pprint(r.json())
if r.status_code in range(200, 299):
data = r.json()
results = data['results']
if len(results) > 0:
# print(results[0].keys())
movie_ids = set()
for result in results:
_id = result['id']
# print(result['title'], _id)
movie_ids.add(_id)
# print(list(movie_ids))
output = 'movies.csv'
movie_data = []
for movie_id in movie_ids:
api_version = 3
api_base_url = f"https://api.themoviedb.org/{api_version}"
endpoint_path = f"/movie/{movie_id}"
endpoint = f"{api_base_url}{endpoint_path}?api_key={api_key}"
r = requests.get(endpoint)
if r.status_code in range(200, 299):
data = r.json()
movie_data.append(data)
df = pd.DataFrame(movie_data)
print(df.head())
df.to_csv(output, index=False)