-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_movies.py
128 lines (106 loc) · 3.12 KB
/
get_movies.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import requests, time
import pandas as pd
api_key = 'API_KEY'
base_tmdb_url = 'http://api.themoviedb.org/3'
bechdel_data = pd.read_csv('bechdel_test_data.csv', dtype=object, names=['Title',
'IMDB_ID', 'Year', 'Bechdel_Rating'])
df = pd.DataFrame(columns=['Title',
'IMDB_ID',
'Year',
'Bechdel_Rating',
'TMDB_ID',
'Budget',
'Overview',
'Popularity',
'Revenue',
'Genres',
'Cast',
'Crew',
'Recommendations',
])
#Movie /find/ -> TMDB ID
def imdb_to_tmdb(imdb_id):
path = '/find/tt' + str(imdb_id)
r = requests.get(base_tmdb_url + path,
params = {'api_key': api_key,'external_source': 'imdb_id'})
if r.status_code is requests.codes.ok:
parsed = r.json()
if 'movie_results' in parsed and len(parsed['movie_results']) > 0:
movie = parsed['movie_results'][0]
if 'id' in movie:
return movie['id']
# GetDetails
# Budget
# Overview
# Popularity
# Revenue
# Genres: List [{id: , name: }]
def get_details(tmdb_id):
path = '/movie/{0}'.format(tmdb_id)
r = requests.get(base_tmdb_url + path, params = {'api_key': api_key})
if r.status_code is requests.codes.ok:
parsed = r.json()
return (parsed['title'],
parsed['budget'],
parsed['overview'],
parsed['popularity'],
parsed['revenue'],
parsed['genres'])
# GetCredits
# Cast: List[{id: , order: }]
# Crew: List[{id: , job: , department: }]
def get_credits(tmdb_id):
cast = list()
crew = list()
path = '/movie/{0}/credits'.format(tmdb_id)
r = requests.get(base_tmdb_url + path, params = {'api_key': api_key})
if r.status_code is requests.codes.ok:
parsed = r.json()
if 'cast' in parsed:
cast_obj = parsed['cast']
cast = list(map(lambda person: {'id': person['id'],
'order': person['order'],
'character': person['character'],
}, cast_obj))
if 'crew' in parsed:
crew_obj = parsed['crew']
crew = list(map(lambda person: {'id': person['id'],
'department': person['department'],
'job': person['job'],
}, crew_obj))
return (cast, crew)
# GetRecommendations
# recommendations: List [id]
def get_recommendations(tmdb_id):
recommendations = list()
path = '/movie/{0}/recommendations'.format(tmdb_id)
r = requests.get(base_tmdb_url + path, params = {'api_key': api_key})
if r.status_code is requests.codes.ok:
parsed = r.json()
if 'results' in parsed:
recommendations = [rec['id'] for rec in parsed['results']][:5]
return recommendations
for index, movie in bechdel_data.iterrows():
tmdb_id = imdb_to_tmdb(movie['IMDB_ID'])
time.sleep(0.3)
if tmdb_id:
(title, budget, overview, popularity, revenue, genres) = get_details(tmdb_id)
time.sleep(0.3)
(cast, crew) = get_credits(tmdb_id)
time.sleep(0.3)
recommendations = get_recommendations(tmdb_id)
time.sleep(0.3)
df.loc[index] = [title,
movie['IMDB_ID'],
movie['Year'],
movie['Bechdel_Rating'],
tmdb_id,
budget,
overview,
popularity,
revenue,
genres,
cast,
crew,
recommendations]
df.to_csv('movies.csv')