-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_ch.py
182 lines (141 loc) · 5.75 KB
/
test_ch.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
#coding:utf-8
import os, re
import sqlite3,cPickle,gzip
import time
from time import sleep
from redis_inc import RedisQueueConnection
from worker_filter import Filter
def getip():
ip = ""
try:
ip = os.popen('ifconfig eth0 | grep "inet "').read().split()[1]
except:
pass
return ip
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:# 'ISO-8859-1' and so on
code = None
if code == None:
code = utils.get_encodings_from_content(content)
print "unknown code",seed,code
if code:
code = code[0]
if code.lower() == 'gbk' or code.lower() == 'gb2312':
code = 'gbk'
return code
class Daemon:
def __init__(self, done_que):
self.cnt = 0
self.showpercounts = 100
self.dbsize = 0
self.dbsizelimit = 536870912 # 512M 536870912
self.spend = 0
#queue for daemon recieve downloaded websites info
self.done_que = done_que
#urls queue to put filtered urls extracted from the webpage
self.urls_que = RedisQueueConnection('extracturls').conn
self.urlfilter = Filter()
self.ip = getip()
self.fname = self.getdbname()
self.conn = sqlite3.connect(self.fname)
self.conn.execute("create table if not exists mainpages (id integer primary key autoincrement, url TEXT,headers TEXT,content BLOB)")
#when recv the ctrl+c signal, run out the extractation jobs and then quit
self.quit = False
def getdbname(self, create=False):
path = "/work/db"
tf = "%Y%m%d-%H%M%S"
pre = "sitedata"
suf = ".db"
dbsize = 0
ip = getip()
findname = "%s%s" % (ip, suf)
if create == True:
date = time.strftime(tf, time.localtime())
lastname = "_".join([pre, date, ip]) + suf
self.dbsize = 0
print "Create db: ", lastname
return os.path.join(path, lastname)
fnames = os.listdir(path)
last = 0
lastname = ""
for fname in fnames:
if fname.endswith(findname):
fnow = fname.split('_')[1]
fnown = int(time.mktime(time.strptime(fnow, tf)))
if fnown > last:
last = fnown
lastname = fname
#can not found the newest db file, so create it
if not last:
date = time.strftime(tf, time.localtime())
lastname = "_".join([pre, date, ip]) + suf
print "Create db: ", lastname
self.dbsize = 0
else:
print "Reuse the last db: ", lastname
self.dbsize = os.stat(os.path.join(path, lastname)).st_size
return os.path.join(path, lastname)
def geturls(self, seed, content):
urls = []
returls = []
if not content or len(content) == 0:
return []
try:
urls = re.findall(self.urlfilter.urlpatern, content)
returls = self.urlfilter.filter_urls(seed,urls)
except:
pass
return returls
def run(self):
#backend job,
sleep(2)
while True:
try:
if self.done_que.empty():
if self.quit == True:
#the speed to extract urls is more higher than crawler
sleep(1)
if not self.done_que.empty():
continue
print "Daemon run done and quit successfuly"
exit(0)
#print "Downloaded queue empty, wait crawler ..."
sleep(10)
continue
data = cPickle.loads(self.done_que.get())
seed = data['seed']
content = data['content']
headers = str(data['headers'])
urls = self.geturls(seed, content)
#put the extracted urls to urls_que
for url in urls:
self.urls_que.put(url)
#use level 1 to compress data , we get enough compress ratio and speed
gziphtml = sqlite3.Binary(gzip.zlib.compress(content, 1))
self.dbsize += ( len(gziphtml) + len(seed) + len(headers) )
self.conn.execute("insert into mainpages (url,headers,content) values (?,?,?)", (seed, headers, gziphtml))
self.cnt += 1
if self.cnt % self.showpercounts == 0:
self.conn.commit()
print "\n%s\n\tExtract done:%d todo:%d size:%dM" % \
(self.ip, self.cnt, self.done_que.qsize(), self.dbsize/1024/1024)
if self.dbsize > self.dbsizelimit:
self.fname = self.getdbname(True)
self.conn.close()
self.conn = sqlite3.connect(self.fname)
self.conn.execute("create table if not exists mainpages (id integer primary key autoincrement, url TEXT,headers TEXT,content BLOB)")
except Exception as e:
print e
except KeyboardInterrupt:
print "Daemon recv quit singal, waiting for queue empty"
self.quit = True
if __name__ == '__main__':
print "Can not run alone, run worker_crawler indeed."
#daemon = Daemon()
#daemon.run()