-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
334 lines (258 loc) · 11.7 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import datetime
import json
import os
from imdb import IMDb
import pandas as pd
def get_ten_previous_movies(person, instance):
person_id = person.getID()
person_data = instance.get_person_filmography(person_id)
try:
filmography = person_data['data']['filmography']['actor']
except:
filmography = person_data['data']['filmography']['writer']
ten_previous_movies = []
for movie in filmography:
if '()' not in movie['title']:
ten_previous_movies.append(movie)
if len(ten_previous_movies) == 10:
break
return ten_previous_movies
# weekend=True for weekend Box Office revenues
# =False for worldwide
def get_10_prev_revenues_for(person, instance, weekend):
ten_previous_movies = get_ten_previous_movies(person, instance)
data_BOs = {}
ratios = {}
suma = 0
counter = 0
counter, suma = get_data_BOs(counter, data_BOs, instance, ratios, suma, ten_previous_movies, weekend)
average = suma/counter
if weekend:
return average, ratios, data_BOs
else:
return average, data_BOs
def get_data_BOs(counter, data_BOs, instance, ratios, suma, ten_previous_movies, weekend):
for movie in ten_previous_movies:
movie_id = movie.getID()
movie = instance.get_movie(movie_id)
revenue = movie.get('box office')
if revenue:
ratio = get_ratio(revenue)
ratios[str(movie)] = ratio
if weekend:
rev_bo = revenue.get('Opening Weekend United States')
else:
rev_bo = revenue.get('Cumulative Worldwide Gross')
else:
rev_bo = None
if rev_bo:
suma = suma + get_number(rev_bo)
counter += 1
data_BOs[str(movie)] = rev_bo
return counter, suma
def create_movies_by_genre():
print("Reading TSVs...")
basics_tsv = pd.read_csv('input_data/data_basics.tsv', sep='\t')
ratings_tsv = pd.read_csv('input_data/data_ratings.tsv', sep='\t')
print("Done.")
print("Merging and sorting the tables...")
df = pd.merge(ratings_tsv, basics_tsv, on='tconst', how='left')
sorted_df = df.sort_values(by=['numVotes'], ascending=False).reset_index().drop(axis=1, columns=['index'])
print("Done.")
print("Creating the dictionary...")
movies_by_genre = {}
for row in sorted_df['genres']:
try:
genres = row.split(',')
except:
continue
for genre in genres:
if genre not in movies_by_genre:
movies_by_genre[genre] = {}
# some movies do not have a genre
movies_by_genre.pop('\\N', None)
for movie_genre in movies_by_genre:
genre_df = sorted_df[sorted_df['genres'].str.contains(movie_genre, na=False)]
genre_dict = dict(zip(genre_df.tconst, genre_df.numVotes))
movies_by_genre[movie_genre] = genre_dict
print('Done.')
print('Writing it in "movies_by_genre.json"...')
with open('movies_by_genre.json', 'w') as file:
json.dump(movies_by_genre, file)
print('Done.')
return movies_by_genre
def get_top_100_by_genre(genre):
try:
# to read if the file exists
print("Trying to read 'movies_by_genre.json'...")
with open('movies_by_genre.json', 'r') as file:
movies_by_genre = json.load(file)
print('Done.')
except:
print("File not found. Starting to create the file...")
# create the file if not
movies_by_genre = create_movies_by_genre()
print("Done.")
genre_top_100 = {key: movies_by_genre[genre][key] for key in list(movies_by_genre[genre].keys())[:1]}
return genre_top_100
def get_number(string):
return int(string.split(' ')[0].replace(',', '')[1:])
def get_ratio(revenue):
opening = revenue.get('Opening Weekend United States')
worldwide = revenue.get('Cumulative Worldwide Gross')
if opening and worldwide:
ratio = get_number(opening)/get_number(worldwide)
else:
ratio = None
return ratio
def get_top_100_revenues_for(genre, instance, weekend):
top_100 = get_top_100_by_genre(genre)
ratios = {}
suma = 0
counter = 0
data_BOs = {}
for movie_id in top_100:
movie = instance.get_movie(movie_id[2:])
revenue = movie.get('box office')
if revenue:
ratio = get_ratio(revenue)
ratios[str(movie)] = ratio
if weekend:
rev_bo = revenue.get('Opening Weekend United States')
else:
rev_bo = revenue.get('Cumulative Worldwide Gross')
else:
rev_bo = None
if rev_bo:
suma = suma + get_number(rev_bo)
counter += 1
data_BOs[str(movie)] = rev_bo
average = suma/counter
if weekend:
return average, ratios, data_BOs
else:
return average, data_BOs
def get_data(instance, lead_actor_1, lead_actor_2, director, producer, top_genre, secondary_genre):
movie_data = {'lead_actor_1': {}, 'lead_actor_2': {}, 'director': {}, 'producer': {}, 'top_genre': {},
'secondary_genre': {}}
categorized_ratios = {}
averages = {'lead_actor_1': {}, 'lead_actor_2': {}, 'director': {}, 'producer': {}, 'top_genre': {},
'secondary_genre': {}}
# print('Getting data for LEAD ACTOR 1...')
# print('---opening weekend revenues ...')
# averages['lead_actor_1']['opening_weekend_revenues'], categorized_ratios['lead_actor_1'], movie_data['lead_actor_1']['opening_weekend_revenues'] = get_10_prev_revenues_for(lead_actor_1, instance, True)
# print('---worldwide revenues ...')
# averages['lead_actor_1']['worldwide_revenues'], movie_data['lead_actor_1']['worldwide_revenues'] = get_10_prev_revenues_for(lead_actor_1, instance, False)
# print('Done.\n')
#
# print('Getting data for LEAD ACTOR 2...')
# print('---opening weekend revenues ...')
# averages['lead_actor_2']['opening_weekend_revenues'], categorized_ratios['lead_actor_2'], movie_data['lead_actor_2']['opening_weekend_revenues'] = get_10_prev_revenues_for(lead_actor_2, instance, True)
# print('---worldwide revenues ...')
# averages['lead_actor_2']['worldwide_revenues'], movie_data['lead_actor_2']['worldwide_revenues'] = get_10_prev_revenues_for(lead_actor_2, instance, False)
# print('Done.\n')
#
# print('Getting data for DIRECTOR...')
# print('---opening weekend revenues ...')
# averages['director']['opening_weekend_revenues'], categorized_ratios['director'], movie_data['director']['opening_weekend_revenues'] = get_10_prev_revenues_for(director, instance, True)
# print('---worldwide revenues ...')
# averages['director']['worldwide_revenues'], movie_data['director']['worldwide_revenues'] = get_10_prev_revenues_for(director, instance, False)
# print('Done.\n')
#
# print('Getting data for PRODUCER...')
# print('---opening weekend revenues ...')
# averages['producer']['opening_weekend_revenues'], categorized_ratios['producer'], movie_data['producer']['opening_weekend_revenues'] = get_10_prev_revenues_for(producer, instance, True)
# print('---worldwide revenues ...')
# averages['producer']['worldwide_revenues'], movie_data['producer']['worldwide_revenues'] = get_10_prev_revenues_for(producer, instance, False)
# print('Done.\n')
#
# print('Getting data for TOP GENRE...')
# print('---opening weekend revenues ...')
# averages['top_genre']['opening_weekend_revenues'], categorized_ratios['top_genre'], movie_data['top_genre']['opening_weekend_revenues'] = get_top_100_revenues_for(top_genre, instance, True)
# print('---worldwide revenues ...')
# averages['top_genre']['worldwide_revenues'], movie_data['top_genre']['worldwide_revenues'] = get_top_100_revenues_for(top_genre, instance, False)
# print('Done.\n')
#
# print('Getting data for SECONDARY GENRE...')
# print('---opening weekend revenues ...')
# averages['secondary_genre']['opening_weekend_revenues'], categorized_ratios['secondary_genre'], movie_data['secondary_genre']['opening_weekend_revenues'] = get_top_100_revenues_for(secondary_genre, instance, True)
# print('---worldwide revenues ...')
# averages['secondary_genre']['worldwide_revenues'], movie_data['secondary_genre']['worldwide_revenues'] = get_top_100_revenues_for(secondary_genre, instance, False)
# print('Done.\n')
return averages, categorized_ratios, movie_data
def create_CSVs(data):
columns = list(data.keys())
weekend_dict = {}
worldwide_dict = {}
for column in columns:
weekend_dict[column] = []
worldwide_dict[column] = []
for column in columns:
weekend_data = data[column].get('opening_weekend_revenues')
worldwide_data = data[column].get('worldwide_revenues')
if weekend_data:
for key in weekend_data:
pair = key + ':' + str(weekend_data.get(key))
weekend_dict[column].append(pair)
if worldwide_data:
for key in worldwide_data:
pair = key + ':' + str(worldwide_data.get(key))
worldwide_dict[column].append(pair)
for column in columns:
week_col = weekend_dict[column]
world_col = worldwide_dict[column]
if len(week_col) < 100:
while len(week_col) < 100:
week_col.append('')
if len(world_col) < 100:
while len(world_col) < 100:
world_col.append('')
weekend_df = pd.DataFrame(weekend_dict)
worldwide_df = pd.DataFrame(worldwide_dict)
return weekend_df, worldwide_df
def get_movie_info(id):
ia = IMDb()
movie = ia.get_movie(id)
lead_actor_1 = movie['cast'][0]
lead_actor_2 = movie['cast'][1]
director = movie['director'][0]
producer = movie['producers'][0]
top_genre = movie['genres'][0]
secondary_genre = movie['genres'][1]
# studio = movie['production companies'][0]
averages, ratios, movie_data = get_data(ia, lead_actor_1, lead_actor_2, director, producer, top_genre, secondary_genre)
date = datetime.datetime.now().date()
filename = str(id) + "_" + str(date)
try:
os.makedirs('output_data')
except:
# it already exists
pass
print("Writing the data in JSON format...")
with open('output_data/' + filename + '.json', 'w') as f:
json.dump(movie_data, f)
print("Creating CSVs...")
weekend_csv, worldwide_csv = create_CSVs(movie_data)
print("Writing the data to 2 .CSV files...")
with open('output_data/' + filename + '_weekend.csv', 'w', newline='\n') as f:
weekend_csv.to_csv(f, index=False)
with open('output_data/' + filename + '_worldwide.csv', 'w', newline='\n') as f:
worldwide_csv.to_csv(f, index=False)
print("Writing the RATIOS to a JSON file...")
with open('output_data/' + filename + "_ratios.json", 'w') as f:
json.dump(ratios, f)
print("Writing the AVERAGES to a JSON file... ")
with open('output_data/' + filename + "_averages.json", 'w') as f:
json.dump(averages, f)
print('Done.')
import sys
try:
id = sys.argv[1]
except:
raise Exception("Argument Error: argument missing.")
if not id.isdecimal():
raise Exception("Argument Error: it should only contain digits.")
# example id: '0372784'
begin = datetime.datetime.now()
get_movie_info(id)
print("\n\nThe execution time is: {time}".format(time=(datetime.datetime.now() - begin)))