-
Notifications
You must be signed in to change notification settings - Fork 13
/
imdb_actor_import.py
105 lines (83 loc) · 2.75 KB
/
imdb_actor_import.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
#!/usr/bin/env python
# encoding: utf-8
# Download the specified data from
# ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/
# ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/actresses.list.gz
import re
import json
from hashlib import sha1
import codecs
# import requests
# url = "http://33.33.33.33:9200/"
# def index_item_to_es(item):
# requests.put(url, data=item)
# pass
#listname = "ACTOR"
listname = "ACTRESSES"
def write_a_bulk_file(items):
with open('%s.json' % listname.lower(), 'w') as f:
lines = '\n'.join(items)
lines += '\n'
f.write(lines)
def main(buffer):
somelines = buffer.split('THE %s LIST' % listname)[1]
somelines = somelines[50:]
splittedlist = somelines.split('\n\n')
items = []
for item in splittedlist[:50000]:
item = [x.split('\t') for x in item.split('\n\t\t\t')]
item = item[0]
name = item[0]
indexit = True
try:
role = re.search(r"\[.*?\]", item[-1])
role = role.group()[1:-1]
except:
role = ""
indexit = False
try:
year = re.search(r"\(\d*\)", item[-1])
year = year.group()[1:-1]
except:
year = ""
indexit = False
title = item[-1].split(' (')[0]
if title and name:
indexit = True
else:
indexit = False
newitem = {
'name': name.decode('latin1'),
'role': role.decode('latin1'),
'year': year,
'title': title.decode('latin1')
}
if indexit is True:
items.append(json.dumps({"index": {"_type": "actors", "_id": sha1(name+title+year).hexdigest() }}))
items.append(json.dumps(newitem))
# get all movies by actor
for movie in item[1:]:
try:
role = re.search(r"\[.*?\]", movie)
role = role.group()[1:-1]
except:
role = ""
try:
year = re.search(r"\(\d*\)", item[-1])
year = year.group()[1:-1]
except:
year = ""
title = movie.split(' (')[0]
newitem = {
'name': name.decode('latin1'),
'role': role.decode('latin1'),
'year': year,
'title': title.decode('latin1')
}
items.append(json.dumps({"index": {"_type": "actors", "_id": sha1(name+title+year).hexdigest() }}))
items.append(json.dumps(newitem))
write_a_bulk_file(items)
if __name__ == '__main__':
with open('%s.list' % listname.lower()) as f:
buffer = f.read()
main(buffer)