-
Notifications
You must be signed in to change notification settings - Fork 34
/
processor.py
426 lines (354 loc) · 14.5 KB
/
processor.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import asyncio
import logging
import sys
import time
import peewee
import collectors_list
import proxy_utils
import proxy_validator
from checkers.base_checker import CheckerResult
from models import CollectorState, Proxy, db
from proxy_py import settings
class Processor:
"""
main class which collects proxies from collectors,
checks them and saves in database
"""
instance = None
@staticmethod
def get_instance():
if Processor.instance is None:
Processor.instance = Processor()
return Processor.instance
def __init__(self):
self.logger = logging.getLogger("proxy_py/processor")
self.logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
logger_handler = logging.StreamHandler(sys.stdout)
logger_handler.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
logger_handler.setFormatter(logging.Formatter(settings.LOG_FORMAT_STRING))
self.logger.addHandler(logger_handler)
self.collectors_logger = logging.getLogger("proxy_py/collectors")
self.collectors_logger.setLevel(
logging.DEBUG if settings.DEBUG else logging.INFO
)
collectors_logger_handler = logging.StreamHandler(sys.stdout)
collectors_logger_handler.setLevel(
logging.DEBUG if settings.DEBUG else logging.INFO
)
collectors_logger_handler.setFormatter(
logging.Formatter(settings.LOG_FORMAT_STRING)
)
self.collectors_logger.addHandler(collectors_logger_handler)
self.logger.debug("processor initialization...")
self.proxies_semaphore = asyncio.BoundedSemaphore(
settings.NUMBER_OF_CONCURRENT_TASKS
)
self.good_proxies_are_processed = False
async def init(self):
for checker in settings.PROXY_CHECKERS:
if hasattr(checker, "init"):
await checker.init()
async def worker(self):
await self.init()
await asyncio.gather(
*[
self.process_proxies(),
self.process_collectors(),
]
)
async def process_proxies(self):
while True:
await asyncio.sleep(0.01)
try:
# check good proxies
proxies = await db.execute(
Proxy.select()
.where(
Proxy.number_of_bad_checks == 0,
Proxy.next_check_time < time.time(),
)
.order_by(Proxy.next_check_time)
.limit(settings.NUMBER_OF_CONCURRENT_TASKS)
)
if proxies:
self.good_proxies_are_processed = False
await self.add_proxies_to_queue(proxies)
if proxies:
continue
self.good_proxies_are_processed = True
# check bad proxies
proxies = await db.execute(
Proxy.select()
.where(
Proxy.number_of_bad_checks > 0,
Proxy.number_of_bad_checks < settings.DEAD_PROXY_THRESHOLD,
Proxy.next_check_time < time.time(),
)
.order_by(Proxy.next_check_time)
.limit(settings.NUMBER_OF_CONCURRENT_TASKS)
)
await self.add_proxies_to_queue(proxies)
if proxies:
continue
# check dead proxies
proxies = await db.execute(
Proxy.select()
.where(
Proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD,
Proxy.number_of_bad_checks
< settings.DO_NOT_CHECK_ON_N_BAD_CHECKS,
Proxy.next_check_time < time.time(),
)
.order_by(Proxy.next_check_time)
.limit(settings.NUMBER_OF_CONCURRENT_TASKS)
)
await self.add_proxies_to_queue(proxies)
except KeyboardInterrupt as ex:
raise ex
except BaseException as ex:
self.logger.exception(ex)
if settings.DEBUG:
raise ex
await asyncio.sleep(settings.SLEEP_AFTER_ERROR_PERIOD)
async def process_collectors(self):
while True:
await asyncio.sleep(0.1)
try:
# check collectors
collector_states = await db.execute(
CollectorState.select()
.where(
CollectorState.last_processing_time
< time.time() - CollectorState.processing_period
)
.order_by(peewee.fn.Random())
.limit(settings.NUMBER_OF_CONCURRENT_COLLECTORS)
)
await asyncio.gather(
*[
self.process_collector_of_state(collector_state)
for collector_state in collector_states
]
)
except KeyboardInterrupt as ex:
raise ex
except BaseException as ex:
self.collectors_logger.exception(ex)
if settings.DEBUG:
raise ex
await asyncio.sleep(settings.SLEEP_AFTER_ERROR_PERIOD)
async def add_proxy_to_queue(self, proxy: Proxy, collector_id=None):
async with self.proxies_semaphore:
asyncio.ensure_future(
self.process_proxy(
proxy.get_raw_protocol(),
proxy.auth_data,
proxy.domain,
proxy.port,
collector_id,
)
)
async def add_proxies_to_queue(self, proxies: list):
for proxy in proxies:
await self.add_proxy_to_queue(proxy)
async def process_collector_of_state(self, collector_state):
collector = await collectors_list.load_collector(collector_state)
try:
self.logger.debug(
'start processing collector of type "{}"'.format(type(collector))
)
tasks = []
number_of_proxies = 0
async for proxy in collector._collect():
number_of_proxies += 1
tasks.append(self.process_raw_proxy(proxy, collector_state.id))
if len(tasks) > settings.NUMBER_OF_CONCURRENT_TASKS:
await asyncio.gather(*tasks)
tasks.clear()
if tasks:
await asyncio.gather(*tasks)
if number_of_proxies == 0:
self.collectors_logger.warning(
'got 0 proxies from collector of type "{}"'.format(type(collector))
)
else:
self.collectors_logger.info(
f'got {number_of_proxies} proxies from collector of type "{type(collector)}"'
)
except KeyboardInterrupt as ex:
raise ex
except BaseException as ex:
self.collectors_logger.error(
'Error in collector of type "{}"'.format(collector_state.identifier)
)
self.collectors_logger.exception(ex)
finally:
collector.last_processing_time = int(time.time())
# TODO: new proxies count
await collectors_list.save_collector(collector_state)
async def process_raw_proxy(self, proxy, collector_id):
self.logger.debug('processing raw proxy "{}"'.format(proxy))
try:
_, auth_data, domain, port = proxy_validator.retrieve(proxy)
except proxy_validator.ValidationError as ex:
self.collectors_logger.error(
'Collector with id "{}" returned bad raw proxy "{}". '
"Message: {}".format(collector_id, proxy, ex)
)
return
# don't care about protocol
try:
proxy = await db.get(
Proxy.select().where(
Proxy.auth_data == auth_data,
Proxy.domain == domain,
Proxy.port == port,
)
)
if (
proxy.last_check_time + settings.PROXY_NOT_CHECKING_PERIOD
>= time.time()
):
proxy_short_address = ""
if auth_data:
proxy_short_address += auth_data + "@"
proxy_short_address += "{}:{}".format(domain, port)
self.logger.debug(
'skipping proxy "{}" from collector "{}"'.format(
proxy_short_address, collector_id
)
)
return
except Proxy.DoesNotExist:
pass
for raw_protocol in range(len(Proxy.PROTOCOLS)):
while not self.good_proxies_are_processed:
# TODO: find a better way
await asyncio.sleep(0.1)
new_proxy = Proxy()
new_proxy.raw_protocol = raw_protocol
new_proxy.auth_data = auth_data
new_proxy.domain = domain
new_proxy.port = port
await self.add_proxy_to_queue(new_proxy, collector_id)
async def process_proxy(
self, raw_protocol: int, auth_data: str, domain: str, port: int, collector_id
):
async with self.proxies_semaphore:
self.logger.debug(
"start processing proxy {}://{}@{}:{} with collector id {}".format(
raw_protocol, auth_data, domain, port, collector_id
)
)
if auth_data is None:
auth_data = ""
proxy_url = "{}://".format(Proxy.PROTOCOLS[raw_protocol])
if auth_data:
proxy_url += auth_data + "@"
proxy_url += domain + ":" + str(port)
start_checking_time = time.time()
check_result, checker_additional_info = await proxy_utils.check_proxy(
proxy_url
)
end_checking_time = time.time()
if check_result:
self.logger.debug("proxy {0} works".format(proxy_url))
await self.create_or_update_proxy(
raw_protocol,
auth_data,
domain,
port,
start_checking_time,
end_checking_time,
checker_additional_info,
)
else:
self.logger.debug("proxy {0} doesn't work".format(proxy_url))
try:
proxy = await db.get(
Proxy.select().where(
Proxy.raw_protocol == raw_protocol,
Proxy.auth_data == auth_data,
Proxy.domain == domain,
Proxy.port == port,
)
)
proxy.last_check_time = int(time.time())
proxy.next_check_time = (
proxy.last_check_time + proxy.checking_period
)
proxy.number_of_bad_checks += 1
proxy.uptime = int(time.time())
if proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD:
proxy.bad_uptime = int(time.time())
if (
proxy.number_of_bad_checks
== settings.DO_NOT_CHECK_ON_N_BAD_CHECKS
):
self.logger.debug(
"proxy {} isn't checked anymore".format(proxy.to_url())
)
await db.update(proxy)
except Proxy.DoesNotExist:
pass
@staticmethod
async def create_or_update_proxy(
raw_protocol: Proxy.PROTOCOLS,
auth_data: str,
domain: str,
port: int,
start_checking_time: int,
end_checking_time: int,
additional_info: CheckerResult,
):
if (
raw_protocol is None
or domain is None
or port is None
or auth_data is None
or start_checking_time is None
or end_checking_time is None
):
raise ValueError("Processor.create_or_update_proxy: Bad arguments")
if raw_protocol < 0 or raw_protocol >= len(Proxy.PROTOCOLS):
raise ValueError("Processor.create_or_update_proxy: Bad protocol")
# to microseconds
response_time = int(round((end_checking_time - start_checking_time) * 1000000))
async with db.atomic():
# TODO: fix spontaneous issue with unique constraint:
# peewee.IntegrityError: duplicate key value violates unique constraint
# "proxies_raw_protocol_auth_data_domain_port"
proxy, was_created = await db.get_or_create(
Proxy,
raw_protocol=raw_protocol,
auth_data=auth_data,
domain=domain,
port=port,
)
if was_created:
proxy.number_of_bad_checks = 0
if proxy.bad_proxy or proxy.uptime is None or proxy.uptime == 0:
proxy.uptime = int(time.time())
if (
proxy.bad_uptime is None
or proxy.bad_uptime == 0
or proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD
):
proxy.bad_uptime = int(time.time())
proxy.response_time = response_time
proxy.number_of_bad_checks = 0
proxy.last_check_time = int(time.time())
if additional_info is not None:
if additional_info.ipv4 is not None:
proxy.white_ipv4 = additional_info.ipv4
checking_time = int(end_checking_time - start_checking_time)
if checking_time > settings.PROXY_CHECKING_TIMEOUT:
checking_time = settings.PROXY_CHECKING_TIMEOUT
proxy.checking_period = settings.MIN_PROXY_CHECKING_PERIOD + (
checking_time / settings.PROXY_CHECKING_TIMEOUT
) * (
settings.MAX_PROXY_CHECKING_PERIOD - settings.MIN_PROXY_CHECKING_PERIOD
)
# TODO: bad and not working proxies period
proxy.next_check_time = proxy.last_check_time + proxy.checking_period
await db.update(proxy)