-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic.py
101 lines (79 loc) · 3.03 KB
/
elastic.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
import os
import json
import traceback
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
#ElasticSearch Setting
es = Elasticsearch("http://192.168.0.19:9200")
index_name = "hackerone2"
#fix field name
int_list = ['report_id','reputation', 'rank', 'signal', 'signal_percent', 'impact', 'percent', 'bounty', 'severity_score']
#document file directory
file_directory = "C:/dev/python/hackerone/result/success/"
file_list = os.listdir(file_directory)
#Fix Specific Field Data
def fixData(json_data):
for val in json_data:
if val in int_list and json_data[val] is not '':
#Remove Specific String
if val == 'rank' or val == 'percent' or val == 'signal_percent':
if json_data[val] == '-':
json_data[val] = -1
else:
json_data[val] = json_data[val].replace("st", "")
json_data[val] = json_data[val].replace("nd", "")
json_data[val] = json_data[val].replace("rd", "")
json_data[val] = json_data[val].replace("th", "")
#Remove Specific String From Score
if val == 'severity_score':
if json_data[val] == '(---)':
json_data[val] = -1
else:
json_data[val] = json_data[val].replace("(", "")
json_data[val] = json_data[val].replace(")", "")
if type(json_data[val]) is str and json_data[val].find(' ~ ') is not -1:
split = json_data[val].split(" ~ ")
first = float(split[0])
last = float(split[1])
avr = (first+last) / 2
json_data[val] = avr
if json_data[val] == '-':
json_data[val] = -1
try:
json_data[val] = int(json_data[val])
except Exception as e:
try:
json_data[val] = float(json_data[val])
except Exception as e:
print(json_data['report_id'])
print(str(e)+"\n")
print(str(traceback.print_tb(e.__traceback__))+"\n")
return json_data
#Inser Document To ElasticSearch
def indexData(document):
try:
es.index(index=index_name, doc_type='_doc', body=document)
except Exception as e:
print(document)
print(str(e)+"\n")
print(str(traceback.print_tb(e.__traceback__))+"\n")
#float and Int Check
def isNumeric(var):
try:
if int(var) == float(var):
return True
except:
try:
float(var)
return True
except:
return False
#Fix Data & Index To ElasticSearch
for file_name in file_list:
json_data=open(file_directory+file_name).read()
json_data = json.loads(json_data)
json_data = fixData(json_data)
indexData(json_data)
#print(json_data)
es.indices.refresh(index=index_name)