-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_plot.py
37 lines (26 loc) · 926 Bytes
/
get_plot.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
from imdb import Cinemagoer
import csv
import multiprocessing
import pandas as pd
import pathlib
cinemagoer = Cinemagoer()
def get_plot(tid):
try:
plot = cinemagoer.get_movie(tid[2:], info=["plot"]).get("plot", " ")[0].strip().strip('"')
except Exception:
plot = ""
return tid, plot
if __name__ == "__main__":
plots = {}
num_votes_threshold = 2000 # rougly 50k titles
filepath = pathlib.PurePath("data", "imdb", "title.ratings.tsv")
df = pd.read_csv(filepath, delimiter='\t')
tids = df[df.numVotes > num_votes_threshold].tconst.tolist()
with open('plots.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(["tconst", "plot"])
for i in range(0, len(tids), 400):
with multiprocessing.Pool() as pool:
plots = pool.map(get_plot, tids[i: i + 400])
writer.writerows(plots)
print(i + 400)