-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrap_imdb.py
168 lines (142 loc) · 6.54 KB
/
scrap_imdb.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
import argparse
import json
import os
import time
from imdb_scraper import parse_main
from imdb_scraper import parse_credits
from imdb_scraper import yield_reviews
from imdb_scraper import parse_keywords
from imdb_scraper import parse_quotes
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--directory', type=str, default='./output', help='Output directory')
parser.add_argument('--begin_year', type=int, default=1930, help='yyyy form')
parser.add_argument('--end_year', type=int, default=2019, help='yyyy form')
parser.add_argument('--sleep', type=float, default=10.0, help='yyyy form')
parser.add_argument('--main', dest='scrap_main', action='store_true')
parser.add_argument('--credits', dest='scrap_credits', action='store_true')
parser.add_argument('--keywords', dest='scrap_keywords', action='store_true')
parser.add_argument('--quotes', dest='scrap_quotes', action='store_true')
parser.add_argument('--reviews', dest='scrap_reviews', action='store_true')
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--rescrap', dest='rescrap', action='store_true')
parser.add_argument('--start-movie-index', dest='skip', type=int, default=0,
help='Number of movies which are skiped scraping')
args = parser.parse_args()
directory = args.directory
begin_year = args.begin_year
end_year = args.end_year
sleep = args.sleep
scrap_main = args.scrap_main
scrap_credits = args.scrap_credits
scrap_keywords = args.scrap_keywords
scrap_quotes = args.scrap_quotes
scrap_reviews = args.scrap_reviews
debug = args.debug
rescrap = args.rescrap
skip = args.skip
# check output directory
directories = ['{}/main/', '{}/credits/', '{}/keywords/', '{}/quotes/', '{}/reviews/']
directories = [d.format(directory) for d in directories]
for d in directories:
if not os.path.exists(d):
os.makedirs(d)
print('Scrap main : {}'.format(scrap_main))
print('Scrap credits : {}'.format(scrap_credits))
print('Scrap keywords: {}'.format(scrap_keywords))
print('Scrap quotes : {}'.format(scrap_quotes))
print('Scrap reviews : {}'.format(scrap_reviews))
print('num skips : {}'.format(skip))
print('year : {} ~ {}'.format(begin_year, end_year))
for year in range(end_year, begin_year - 1, -1):
id_list_path = 'movie_lists/{}.txt'.format(year)
if not os.path.exists(id_list_path):
print('List of {} year does not exist'.format(year))
continue
title_idxs = load_movie_idx(id_list_path)
if debug:
title_idxs = title_idxs[:3]
n_movies = len(title_idxs)
for i_movie, (title, idx) in enumerate(title_idxs):
if i_movie < skip:
print('[{} / {}]: {} ({}) is skiped'.format(i_movie + 1, n_movies, title, year))
continue
print('[{} / {}]: {} ({})'.format(i_movie + 1, n_movies, title, year))
path = '{}/main/{}.json'.format(directory, idx)
if (scrap_main and not os.path.exists(path)) or (scrap_main and rescrap):
obj = parse_main(idx)
save_json(obj, path)
print('scrap {} main'.format(idx))
time.sleep(1)
path = '{}/credits/{}'.format(directory, idx)
if (scrap_credits and not os.path.exists(path)) or (scrap_credits and rescrap):
obj = parse_credits(idx)
save_list_of_json(obj, path)
print('scrap {} credits'.format(idx))
time.sleep(1)
path = '{}/keywords/{}'.format(directory, idx)
if (scrap_keywords and not os.path.exists(path)) or (scrap_keywords and rescrap):
obj = parse_keywords(idx)
save_list(obj, path)
print('scrap {} keywords'.format(idx))
time.sleep(1)
path = '{}/quotes/{}'.format(directory, idx)
if (scrap_quotes and not os.path.exists(path)) or (scrap_quotes and rescrap):
obj = parse_quotes(idx)
save_list_of_json(obj, path)
print('scrap {} quotes'.format(idx))
time.sleep(1)
path = '{}/reviews/{}'.format(directory, idx)
if scrap_reviews:
# set debug mode
if debug:
max_page = 3
else:
max_page = -1
# load scraped reviews and initialize scrapeds
if not os.path.exists(path) or rescrap:
reviews = []
else:
reviews = load_reviews(path)
scrapeds = {r['id'] for r in reviews}
# reset scraped file
if rescrap:
save_list_of_json([], path, op='w')
for i_reviews, reviews in enumerate(yield_reviews(idx, max_page, sleep, scrapeds, rescrap)):
save_list_of_json(reviews, path, op='a')
print('\rscrap reviews of movie={} from {} pages ..'.format(idx, i_reviews + 1), end='', flush=True)
if max_page > 0 and (i_reviews + 1 < max_page):
print('\rearly stop scraping review of movie={} from {} pages'.format(idx, i_reviews + 1))
else:
print('\rscrap reviews of movie={} from {} pages was done.'.format(idx, i_reviews + 1))
print('-'*40)
print('done year = {} ({} ~ {})'.format(year, begin_year, end_year))
def load_movie_idx(path):
try:
with open(path, encoding='utf-8') as f:
# skip head
next(f)
docs = [doc.split('\t') for doc in f]
docs = [(doc[0], int(doc[1].strip())) for doc in docs if len(doc) == 2]
return docs
except:
return []
def save_list(obj, path):
with open(path, 'w', encoding='utf-8') as f:
for row in obj:
f.write('{}\n'.format(row))
def save_json(json_obj, path):
with open(path, 'w', encoding='utf-8') as f:
json.dump(json_obj, f, ensure_ascii=False, indent=2)
def save_list_of_json(json_list, path, op='w'):
with open(path, op, encoding='utf-8') as f:
for obj in json_list:
obj_strf = json.dumps(obj, ensure_ascii=False)
f.write('{}\n'.format(obj_strf))
def load_reviews(path):
with open(path, encoding='utf-8') as f:
reviews = [line.strip() for line in f]
reviews = [json.loads(r) for r in reviews if r]
return reviews
if __name__ == '__main__':
main()