-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_proxy.py
More file actions
51 lines (43 loc) · 1.47 KB
/
get_proxy.py
File metadata and controls
51 lines (43 loc) · 1.47 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
# -*- coding: utf-8 -*-
# @Author: LogicJake
# @Date: 2019-01-16 16:18:05
# @Last Modified time: 2019-01-22 19:03:44
from lib.database import db_object
from lib.proxy import all_source
from config import logger
import time
class GetProxy():
def __init__(self, db_type):
db_obj = db_object.new_db(db_type)
self.db = db_obj
def save(self, proxies):
self.db.connect()
try:
for proxy in proxies:
proxy['UPDATE_TIME'] = 'unix_timestamp()'
self.db.insert('origin', proxy)
logger.info('Successfully get {} proxies'.
format(len(proxies)))
except Exception as e:
logger.error(str(e))
logger.error('Failed to save proxy data')
self.db.close()
def more_proxy(self):
proxies = []
proxy_objects = all_source.get_all_proxy()
for ob in proxy_objects:
proxies = proxies + ob.get_proxy()
self.save(proxies)
def cycle_get(self):
interval = 1
logger.info('Start thread to get proxy from network')
while True:
self.db.connect()
num = self.db.select('origin', ['COUNT(*)'])
num = num[0][0]
self.db.close()
if num < 100:
logger.info('Begin to get proxy from network')
self.more_proxy()
logger.info("thread-get-proxy is sleeping")
time.sleep(interval * 60)