-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic_import-oneletters.py
executable file
·213 lines (204 loc) · 6.59 KB
/
elastic_import-oneletters.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import time
import string
import itertools
from elasticsearch import Elasticsearch, helpers
INDEX = 'passwd'
INDEX_PREFIX = 'pwd_'
DOC_TYPE = 'account'
INDEX_CFG = {
"settings": {
"index": {
#"number_of_shards": 8,
"refresh_interval": -1,
"number_of_replicas": 0
},
"analysis": {
"filter": {
"tld_filter": {
"type": "pattern_capture",
"preserve_original": False,
"patterns": ["\\.([^\\.]+?)$"]
}
},
"analyzer": {
"lc_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": ["lowercase"]
},
"user_analyzer": {
"type": "custom",
"tokenizer": "user_tokenizer",
"filter": ["lowercase"]
},
"domain_analyzer": {
"type": "custom",
"tokenizer": "domain_tokenizer",
"filter": ["lowercase"]
},
"domain_notld_analyzer": {
"type": "custom",
"tokenizer": "domain_notld_tokenizer",
"filter": ["lowercase"]
},
"tld_analyzer": {
"type": "custom",
"tokenizer": "tld_tokenizer",
"filter": ["lowercase"]
}
},
"tokenizer": {
"user_tokenizer": {
"type": "pattern",
"pattern": "(.+?)@",
"group": 1
},
"domain_tokenizer": {
"type": "pattern",
"pattern": "@(.+)",
"group": 1
},
"domain_notld_tokenizer": {
"type": "pattern",
"pattern": "@(.+)\\.",
"group": 1
},
"tld_tokenizer": {
"type": "pattern",
"pattern": "\\.([^\\.]+?)$",
"group": 1
}
},
"normalizer": {
"lc_normalizer": {
"type": "custom",
"char_filter": [],
"filter": ["lowercase"]
}
}
}
},
"mappings": {
DOC_TYPE: {
"properties": {
"email": {
"type": "text",
"analyzer": "simple",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "lc_normalizer"
}
}
},
"username": {
"type": "text",
"analyzer": "simple",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "lc_normalizer"
}
}
},
"domain": {
"type": "keyword",
"normalizer": "lc_normalizer"
},
"domain_notld": {
"type": "keyword",
"normalizer": "lc_normalizer"
},
"tld": {
"type": "keyword",
"normalizer": "lc_normalizer"
},
"password": {
"type": "text",
"analyzer": "simple",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"password_length": {
"type": "short"
},
"source": {
"type": "short"
}
}
}
}
}
def parseCSV(csv_file):
# schema: "email","username","domain","domain_notld","tld","password","source"
count = 0
with open(csv_file, 'r') as fd:
for line in fd:
count += 1
if count % 100000 == 0:
print('%s: %s' % (time.time(), count))
sys.stdout.flush()
if len(line) > 127:
continue
l_split = line.rstrip().split('","')
if len(l_split) != 7:
continue
try:
email = l_split[0][1:]
username = l_split[1]
domain = l_split[2]
domain_notld = l_split[3]
tld = l_split[4]
password = l_split[5]
password_length = len(password)
except:
continue
try:
source = int(l_split[6][:-1])
except:
source = None
try:
yield {
'email': email,
'username': username,
'domain': domain,
'domain_notld': domain_notld,
'tld': tld,
'password': password,
'password_length': password_length,
'source': source
}
except:
continue
def run(csv_file):
es = Elasticsearch()
all_chars = string.ascii_lowercase + string.digits
extras = '._-'
indexes = set(itertools.chain(['misc'], all_chars))
for x in list(indexes):
if x.startswith(tuple(extras)):
indexes.discard(x)
for index in indexes:
es.indices.delete(index=INDEX_PREFIX + index, ignore=[400, 404])
es.indices.create(index=INDEX_PREFIX + index, body=INDEX_CFG, ignore=[400, 404])
print('created index', INDEX_PREFIX + index)
sys.stdout.flush()
actions = (
{'_index': '%s%s' % (INDEX_PREFIX, x['email'][:1].lower() if x['email'][:1].lower() in indexes else 'misc'),
'_id': '%s%s' % (x['email'], x['password']),
'_type': DOC_TYPE,
'_source': x,
'routing': x['email'].lower()
}
for x in parseCSV(csv_file))
for ret in helpers.parallel_bulk(es, actions, chunk_size=60000, thread_count=6, request_timeout=480,
raise_on_error=False, raise_on_exception=False):
pass
if __name__ == '__main__':
run(sys.argv[1])