-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_flow.py
More file actions
168 lines (147 loc) · 6.32 KB
/
query_flow.py
File metadata and controls
168 lines (147 loc) · 6.32 KB
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
from abstract_interface import DataSource, FilterContainer
from AllegroQuery import AllegroQuery
from filters.margin import MarginFilter
from filters.price import PriceFilter
from filters.sell_rate import SellRateFilter
import csv
import time
import numpy as np
from record import Record
from suspect import Suspect
class QueryFlow:
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
self.construct_ds()
self.treshold = 20
self.filterContainer = FilterContainer()
filters = [PriceFilter(40), MarginFilter(80), SellRateFilter(0)]
for filter_specified in filters:
self.filterContainer.addFilter(filter_specified)
def construct_ds(self):
self.ds = self.DataSourceConstructor(self.filterContainer, self.threshold)
def run_worker(self, query):
return self.ds.analyze(query)
def save_worker(self, query, worker_id=0):
return self.ds.dump_records(query, worker_id)
def split_worker_jobs2(self, total_queries, query_params):
with open('pickle_data.txt', 'w') as f:
f.write(str((len(total_queries))))
f = open('final_results' + str(time.time()) + '.csv', 'w')
writer = csv.writer(f, delimiter=";", quotechar=' ')
writer.writerow( ('nick', 'nip', 'email', 'phone') )
nip_list = []
for record in Record.read_pickle_folder("pickle_dumps"):
weighted_average = np.zeros(record.length) #provide length filed
df = record.parseDataFrame()
for filter_specified in self.filterContainer.filter_array:
current_filter_average_vector = filter_specified.filter(df)
weighted_average = filter_specified.filter_weight*np.array(current_filter_average_vector)
weighted_average[weighted_average > self.threshold] = 1
weighted_average[weighted_average < self.threshold] = 0
i = 0
for w in weighted_average:
if w == 1:
if (record.companyNip[i] != None):
nip = record.companyNip[i]
nip = nip.replace('-', '')
if nip in nip_list:
continue
else:
nip_list.append(nip)
s = Suspect(nip)
try:
writer.writerow( (record.userLogin[i], nip, s.getUserEmail(), s.getUserNumber()) )
f.flush()
except:
pass
i += 1
f.close()
def split_worker_jobs(self, total_queries, query_params):
with open('pickle_data.txt', 'w') as f:
f.write(str(len(total_queries)))
f = open('final_results' + str(time.time()) + '.csv', 'w')
writer = csv.writer(f, delimiter=";", quotechar=' ')
writer.writerow( ('nick', 'nip', 'email', 'phone') )
nip_list = []
for query in total_queries: #only query tab is variable
try:
built_query = self.DataSourceConstructor.Query(query, **query_params)
record = self.save_worker(built_query)
except:
continue
weighted_average = np.zeros(record.length) #provide length filed
df = record.parseDataFrame()
for filter_specified in self.filterContainer.filter_array:
current_filter_average_vector = filter_specified.filter(df)
weighted_average = filter_specified.filter_weight*np.array(current_filter_average_vector)
weighted_average[weighted_average > self.threshold] = 1
weighted_average[weighted_average < self.threshold] = 0
i = 0
for w in weighted_average:
if w == 1:
if (record.companyNip[i] != None):
nip = record.companyNip[i]
nip = nip.replace('-', '')
if nip in nip_list:
continue
else:
nip_list.append(nip)
s = Suspect(nip)
try:
writer.writerow( (record.userLogin[i], nip, s.getUserEmail(), s.getUserNumber()) )
f.flush()
except:
pass
i += 1
f.close()
def parse_categories(self, cat_file):
categories_list = []
with open(cat_file, "r") as f:
line = f.readline()
while line:
if "catId" in line:
line = line.replace("catId = ", "").strip()
categories_list.append(int(line))
line = f.readline()
return categories_list
if __name__ == "__main__":
sort = {'price' : "desc"}
query_tab = {
'search': 'faktura vat marża',
'category': 0,
'offerType': 'buyNow',
'offerOptions': 'vatInvoice',
'description': 'true',
'condition': 'used'
}
query_params = {
'maxPrice': 100000,
'minPrice': 1000,
'numberOfItems': 100,
'sortOptions': sort
}
cat_file = "data_source/test.txt"
test_filterContainer = FilterContainer()
filters = [PriceFilter(0.6), PriceFilter(0.6), MarginFilter(0.8),
SellRateFilter(0.3)]
for filter_specified in filters:
test_filterContainer.addFilter(filter_specified)
param_dict = {
'workers' : 3,
'threshold': 0.8,
'DataSourceConstructor': AllegroQuery,
'filterContainer': test_filterContainer,
}
qf = QueryFlow(**param_dict)
conditions = ['new', 'used']
categories_list = qf.parse_categories(cat_file)
print(categories_list)
total_queries = []
for condition in conditions:
query_tab['condition'] = condition
for cat in categories_list:
query_tab['category'] = cat
total_queries.append(dict(query_tab))
print("TOTAL QUERIES {}".format(len(total_queries)))
qf.split_worker_jobs(total_queries, query_params)