-
Notifications
You must be signed in to change notification settings - Fork 10
/
daemon.py
166 lines (120 loc) · 3.53 KB
/
daemon.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
#coding:utf-8
import time
import re,time
import sqlite3
import cPickle
import gzip
from requests import utils
#from RedisQueue import RedisQueue
from pybloomfilter import BloomFilter
done_sites_fname='done_sites.bin'
try:
bfdone = BloomFilter.open(done_sites_fname)
except:
bfdone = BloomFilter(2**23, 10**(-5), done_sites_fname) #8M
class Filter:
def __init__(self):
self.https_enable = 0
self.blacklist = set (( '.blog.','.taobao.com','.baidu.com','facebook','google','.edu','.gov','.mil','mail',
'weibo.com','t.cn','worldpress','youtube','wikipedia','facebook','twitter','dropbox' ))
self.allowdDomain = set(('com','net','org','cn','info','biz','me','name','cc','tv'))
if self.https_enable == 0:
self.urlpatern = re.compile(r'href=["\']http://([^/?#\"\']+)',re.I)
else:
self.urlpatern = re.compile(r'href=["\']http[s]?://([^/?#\"\'"]+)',re.I)
def debug_filter(self,urls):
#return filter(lambda url: ".58.com" in url , urls)
return urls
def filter_urls(self, seed, urls):
nurls = []
seeditem = seed.lower().split('.')
seedlen = len(seeditem)
maindomain = 1 if seeditem[0] == 'www' else 0
urls = {}.fromkeys(urls).keys()
for url in urls:
#url = url.split('/',1)[0].split('#',1)[0].split('?',1)[0].lower()
url = url.lower()
#filter Domain , only allowd for china
suf = 0
urlitem = url.split('.')
nlen = len(urlitem)
if nlen < 2:
continue
tld = urlitem[-1]
if tld in self.allowdDomain:
if urlitem[-2] in self.allowdDomain:
if nlen<=4:
suf = 2
else:
if nlen<=3:
suf = 1
if suf >= 1:
# blacklist verify
block = 0
for b in self.blacklist:
if url.find(b) >= 0:
block = 1
continue
if block == 0:
if nlen != seedlen:
nurls.append(url)
else:
if maindomain or urlitem[-(suf+1)] != seeditem[-(suf+1)]:
nurls.append(url)
#print seed, nurls
return {}.fromkeys(nurls).keys()
def geturls( seed, content):
filterset = Filter()
if not content or len(content) == 0:
return []
urls = re.findall(filterset.urlpatern, content)
st = time.time()
urls = filterset.filter_urls(seed,urls)
et = time.time()
return urls
def procdata_getencoding(seed,headers,content):
code = utils.get_encoding_from_headers(headers)
if code:
if code.lower() == 'gbk' or code.lower() == 'gb2312':
code = 'gbk'
elif code.lower() == 'utf-8':
code = 'utf-8'
else:
code = None
if code == None:
code = utils.get_encodings_from_content(content)
print "content",seed,code
if code:
code = code[0]
if code.lower() == 'gbk' or code.lower() == 'gb2312':
code = 'gbk'
return code
def main():
done_que = RedisQueue('seed')
run_que = RedisQueue('run')
run_que.flushdb()
conn = sqlite3.connect('site_data.db')
conn.execute("create table if not exists mainpages (id integer primary key autoincrement, url TEXT,headers TEXT,content BLOB)")
spend = 0
cnt = 0
size = 0
while True:
data = cPickle.loads(done_que.get())
st = time.time()
urls = geturls(data['url'],data['content'])
if len(urls) == 0:
continue
for url in urls:
if url not in bfdone:
run_que.put(url)
gziphtml = sqlite3.Binary(gzip.zlib.compress(data['content']))
size += len(gziphtml)
conn.execute("insert into mainpages (url,headers,content) values (?,?,?)",(data['url'],str(data['headers']),gziphtml))
et = time.time()
spend += (et-st)
cnt += 1
if cnt % 10 == 0:
print "cost:", spend/cnt, cnt, done_que.qsize(), size / 1024 / 1024
conn.commit()
if __name__ == '__main__':
main()