-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimdb_scrape.py
432 lines (383 loc) · 13.7 KB
/
imdb_scrape.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import imdb
import re
import csv
import json
import numbers_scrape
import boxofficemojo_scrape
import random
import time
import string
"""
Data Acquisition Project: Movie Success Predictor
Team: Jeff Baker, Patrick Howell, David Reilly
Code Author: David Reilly
Code Focus: IMDb API and Data Aggregation
Code Summary: This code accesses the IMDb API in order to obtain information about a movie
given the name and year of release. After populating a dictionary, the movie
information is written to a csv file for review.
"""
def imdb_grab(movie_list):
"""
This function accesses the IMDb API and obtains relevant information
for each movie in the movie_list
inputs: list of movie names
outputs: dictionary populated with title, genre, rating, runtime, country,
language, producers, writers, directors, and costume designers of each movie
"""
dictionary = {}
incorrectMatches = {}
# Iterate through each movie
for i in range(0, len(movie_list)):
# Repeat IMDb initialization to avoid timing out
while True:
try:
ia = imdb.IMDb()
break
except:
pass
movie = movie_list[i]
# Repeat search_movie call to avoid timing out
while True:
try:
movieMatches = ia.search_movie(movie)
break
except:
pass
# Iterate through list of search results
# Check for case-insensitive title match
# Resort to first element of list if no matches found
try:
for k in range(len(movieMatches)):
if movie[5:].lower() == str(movieMatches[k]['title']).lower():
first_match = movieMatches[k]
break
else:
if k == len(movieMatches) - 1:
incorrectMatches[movie] = 1.0
first_match = movieMatches[0]
else:
pass
except:
continue
# Repeat update call to avoid timing out
while True:
try:
ia.update(first_match)
break
except:
pass
# Grab rating
try:
rating = str(first_match['rating'])
except:
rating = 0.0
# Grab genre
genre = ''
try:
for a in range(len(first_match['genre'])):
if a > 0:
genre += ' and ' + str(first_match['genre'][a])
else:
genre += str(first_match['genre'][a])
except:
genre = 0.0
# Grab country
country = ''
try:
for b in range(len(first_match['country'])):
if b > 0:
country += ' and ' + str(first_match['country'][b])
else:
country += str(first_match['country'][b])
except:
country = 0.0
# Grab language
language = ''
try:
for c in range(len(first_match['language'])):
if c > 0:
language += ' and ' + str(first_match['language'][c])
else:
language += str(first_match['language'][c])
except:
language = 0.0
# Grab runtime
try:
runtime = str(first_match['runtimes'][0])
except:
runtime = 0.0
# Grab costume designers
costume_designers = ""
try:
for j in range(len(first_match['costume designer'])):
if j > 0:
costume_designers += ' and ' + str(first_match['costume designer'][j])
else:
costume_designers += str(first_match['costume designer'][j])
except:
costume_designers = 0.0
# Grab producers
producers = ""
try:
for k in range(len(first_match['producer'])):
if k > 0:
producers += ' and ' + str(first_match['producer'][k])
else:
producers += str(first_match['producer'][k])
except:
producers = 0.0
# Grab writers
writers = ""
try:
for l in range(len(first_match['writer'])):
if l > 0:
writers += ' and ' + str(first_match['writer'][l])
else:
writers += str(first_match['writer'][l])
except:
writers = 0.0
# Grab directors
directors = ""
try:
for m in range(len(first_match['director'])):
if m > 0:
directors += ' and ' + str(first_match['director'][m])
else:
directors += str(first_match['director'][m])
except:
directors = 0.0
# Populate dictionary
dictionary[movie_list[i]] = {"Genre" : str(genre),
"Rating" : str(rating), "Runtime" : str(runtime),
"Country" : str(country), "Language" : str(language),
"Producers" : str(producers), "Writers" : str(writers),
"Directors" : str(directors), "Costume designers" : str(costume_designers)}
wait_time = round(max(0, 1 + random.gauss(0, 0.5)), 2)
time.sleep(wait_time)
return dictionary
def make_json(dictionary, fileName):
with open(fileName, 'wb') as out_file:
m = re.search(r'(?:\.)(\w+)',fileName)
if m is not None:
ext = m.groups()[0]
if re.match(ext,'JSON',re.IGNORECASE) is not None:
json.dump(dictionary, out_file, encoding='latin-1')
def getNumbers():
return numbers_scrape.create_numbers_dict()
def getBom():
return boxofficemojo_scrape.create_bom_dict()
def getTotalList(dictionary):
list_of_movies = []
for letter in string.uppercase():
for key in dictionary[letter].keys():
list_of_movies.append(key)
def populateCSVA(list1, list2):
"""
Populate CSV file for all movies beginning with the letter 'A'
Input: Two lists of movie titles, one from The-Numbers.com and one from BoxOfficeMojo.com
Output: TotalMovie.csv file
"""
# Initialize IMDb, The-Numbers, and BoxOfficeMojo dictionaries
imdb_dict = imdb_grab(list1)
numbers_dict = getNumbers()
bom_dict = getBom()
# Update BoxOfficeMojo dictionary with movies from The-Numbers.com
for i in range(len(list1)):
try:
bom_dict['A'][list1[i]].update(imdb_dict[list1[i]])
except:
pass
try:
bom_dict['A'][list1[i]].update(numbers_dict['A'][list1[i]])
except:
pass
# Initialize IMDb dictionary with second list of movies
imdb_dict_2 = imdb_grab(list2)
imdb_dict_2.update(imdb_dict)
# Update BOM dictionary with IMDb information
for i in range(len(list1)):
try:
bom_dict['A'][list1[i]].update(imdb_dict_2[list1[i]])
except:
pass
try:
bom_dict['A'][list1[i]].update(numbers_dict['A'][list1[i]])
except:
pass
for j in range(len(list2)):
try:
bom_dict['A'][list2[j]].update(imdb_dict_2[list2[j]])
except:
pass
# Write each row of BOM dictionary to a CSV
with open('TotalMovie.csv', 'wb') as f:
writer = csv.writer(f)
header = ["Title", "TotalGross", "OpeningGross", "Rating", \
"Country", "TotalTheaters", "DomesticBO", "Directors", \
"Studio", "OpeningTheaters", "OpeningDate", "Genre", "Budget", "Runtime"]
writer.writerow(header)
# Populate each row
# If no information is available, append 0
for key in bom_dict['A'].keys():
row = []
row.append(bom_dict['A'][key]["Title"])
try:
row.append(bom_dict['A'][key]["TotalGross"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["OpeningGross"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Rating"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Country"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["TotalTheaters"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["DomesticBO"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Directors"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Studio"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["OpeningTheaters"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["OpeningDate"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Genre"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Budget"])
except:
row.append(0.0)
try:
row.append(bom_dict['A'][key]["Runtime"])
except:
row.append(0.0)
writer.writerow(row)
def populateCSVTotal(list1, list2):
"""
Populate CSV file for all movies
Input: Two lists of movie titles, one from The-Numbers.com and one from BoxOfficeMojo.com
Output: TotalMovie.csv file
"""
# Initialize IMDb, The-Numbers, and BoxOfficeMojo dictionaries for first list of movies
imdb_dict = imdb_grab(list1)
imdb_dict_2 = imdb_grab(list2)
imdb_dict_2.update(imdb_dict)
numbers_dict = getNumbers()
bom_dict = getBom()
# Update BoxOfficeMojo dictionary with movies from The-Numbers.com
for letter in string.uppercase():
for i in range(len(list1)):
try:
bom_dict[letter][list1[i]].update(imdb_dict[list1[i]])
except:
pass
try:
bom_dict[letter][list1[i]].update(numbers_dict[letter][list1[i]])
except:
pass
for i in range(len(list1)):
try:
bom_dict[letter][list1[i]].update(imdb_dict_2[list1[i]])
except:
pass
try:
bom_dict[letter][list1[i]].update(numbers_dict[letter][list1[i]])
except:
pass
for j in range(len(list2)):
try:
bom_dict[letter][list2[j]].update(imdb_dict_2[list2[j]])
except:
pass
# Write CSV file with information for every movie
with open('TotalMovie.csv', 'wb') as f:
writer = csv.writer(f)
header = ["Title", "TotalGross", "OpeningGross", "Rating", \
"Country", "TotalTheaters", "DomesticBO", "Directors", \
"Studio", "OpeningTheaters", "OpeningDate", "Genre", "Budget", "Runtime"]
writer.writerow(header)
for letter in string.uppercase():
# Populate each row
# If no information is available, append 0
for key in bom_dict[letter].keys():
row = []
row.append(bom_dict[letter][key]["Title"])
try:
row.append(bom_dict[letter][key]["TotalGross"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["OpeningGross"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Rating"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Country"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["TotalTheaters"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["DomesticBO"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Directors"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Studio"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["OpeningTheaters"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["OpeningDate"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Genre"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Budget"])
except:
row.append(0.0)
try:
row.append(bom_dict[letter][key]["Runtime"])
except:
row.append(0.0)
writer.writerow(row)
if __name__ == '__main__':
# populateCSVA(getNumbers()['A'].keys()[:10], getBom()['A'].keys()[:10])
# populateCSVTotal(getTotalList(getNumbers()), getTotalList(getBom()))
pass