-
Notifications
You must be signed in to change notification settings - Fork 71
/
Fetcher.py
63 lines (56 loc) · 1.66 KB
/
Fetcher.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
# encoding: utf-8
import datetime
class Fetcher(object):
"""
爬取器的状态储存在数据库中,包括是否启用爬取器,爬取到的代理数量等
"""
ddls = ["""
CREATE TABLE IF NOT EXISTS fetchers
(
name VARCHAR(255) NOT NULL,
enable BOOLEAN NOT NULL,
sum_proxies_cnt INTEGER NOT NULL,
last_proxies_cnt INTEGER NOT NULL,
last_fetch_date TIMESTAMP,
PRIMARY KEY (name)
)
"""]
def __init__(self):
self.name = None
self.enable = True
self.sum_proxies_cnt = 0
self.last_proxies_cnt = 0
self.last_fetch_date = None
def params(self):
"""
返回一个元组,包含自身的全部属性
"""
return (
self.name, self.enable,
self.sum_proxies_cnt, self.last_proxies_cnt, self.last_fetch_date
)
def to_dict(self):
"""
返回一个dict,包含自身的全部属性
"""
return {
'name': self.name,
'enable': self.enable,
'sum_proxies_cnt': self.sum_proxies_cnt,
'last_proxies_cnt': self.last_proxies_cnt,
'last_fetch_date': str(self.last_fetch_date) if self.last_fetch_date is not None else None
}
@staticmethod
def decode(row):
"""
将sqlite返回的一行解析为Fetcher
row : sqlite返回的一行
"""
assert len(row) == 5
f = Fetcher()
f.name = row[0]
f.enable = bool(row[1])
f.sum_proxies_cnt = row[2]
f.last_proxies_cnt = row[3]
f.last_fetch_date = row[4]
return f