-
Notifications
You must be signed in to change notification settings - Fork 17
/
crawler.py
executable file
·518 lines (477 loc) · 18.4 KB
/
crawler.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import psutil
import socket
import struct
import collections
import multiprocessing
from threading import Thread
from btdht import DHT, ID
from btdht.utils import enumerate_ids, Scheduler
import pymongo
import netaddr
from bson.binary import Binary
import config
import resource
import torrent
from utils import getdb
class SharedObject(object):
pass
class HashToIgnore(object):
hash_to_ignore = set()
hash_not_to_ignore = collections.defaultdict(int)
def __init__(self, db=None):
if db is None:
self.db = getdb("torrents")
else:
self.db = db
def add(self, item):
self.hash_to_ignore.add(item)
try:
del self.hash_not_to_ignore[item]
except KeyError:
pass
def __contains__(self, item, errno=0):
if item not in self.hash_to_ignore:
if time.time() - self.hash_not_to_ignore[item] > 600:
if os.path.isfile(
os.path.join(config.torrents_dir, "%s.torrent" % item.encode("hex"))
):
self.hash_to_ignore.add(item)
try:
del self.hash_not_to_ignore[item]
except KeyError:
pass
return True
try:
if self.db.find(
{"_id": Binary(item), "status": {"$nin": [0, None]}}
).count() > 0:
self.hash_to_ignore.add(item)
try:
del self.hash_not_to_ignore[item]
except KeyError:
pass
return True
else:
self.hash_not_to_ignore[item] = time.time()
return False
except pymongo.errors.AutoReconnect:
return self.__contains__(item, errno=errno+1)
else:
return False
else:
return True
class Crawler(DHT):
def __init__(self, *args, **kwargs):
self.master = kwargs.get("master", False)
if self.master:
del kwargs["master"]
self.share = kwargs["share"]
del kwargs["share"]
super(Crawler, self).__init__(*args, **kwargs)
if self.master:
self.share.client = torrent.Client(debug=(self.debuglvl > 0))
self.register_message("get_peers")
self.register_message("announce_peer")
def stop(self):
if self.master:
self.share.client.stoped = True
super(Crawler, self).stop()
def start(self):
# doing some initialisation
if self.master:
self.share.db = getdb("torrents")
self.share.hash_to_ignore = HashToIgnore(self.share.db)
self.share.bad_info_hash = {}
self.share.good_info_hash = {}
self.hash_to_fetch = collections.OrderedDict()
self.hash_to_fetch_tried = collections.defaultdict(set)
self.hash_to_fetch_totry = collections.defaultdict(set)
# calling parent method
super(Crawler, self).start()
# starting threads
for f, name in [(self._client_loop, 'client_loop')]:
t = Thread(target=f)
t.setName("%s:%s" % (self.prefix, name))
t.daemon = True
t.start()
self._threads.append(t)
self.threads.append(t)
if self.master:
# addings threads to parent threads list
self.share.client.start()
self._threads.extend(self.share.client.threads)
self.threads.extend(self.share.client.threads)
def _client_loop(self):
failed_count = collections.defaultdict(int)
last_fail = collections.defaultdict(int)
while True:
processed = False
for hash in self.hash_to_fetch.keys():
if self.stoped:
return
if (
hash in self.share.client.meta_data or
os.path.isfile(
os.path.join(config.torrents_dir, "%s.torrent" % hash.encode("hex"))
)
):
if (
hash in self.share.client.meta_data and
self.share.client.meta_data[hash] is not True
):
with open(
os.path.join(
config.torrents_dir,
"%s.torrent.new" % hash.encode("hex")
),
'wb'
) as f:
f.write("d4:info%se" % self.share.client.meta_data[hash])
os.rename(
os.path.join(
config.torrents_dir,
"%s.torrent.new" % hash.encode("hex")
),
os.path.join(config.torrents_dir, "%s.torrent" % hash.encode("hex"))
)
self.share.db.update(
{'_id': Binary(hash)},
{"$set": {'status': 1}},
upsert=True
)
self.debug(-2, "%s downloaded" % hash.encode("hex"))
self.share.client.meta_data[hash] = True
self.share.client.clean_hash(hash)
self.share.hash_to_ignore.add(hash)
try:
del self.hash_to_fetch[hash]
except:
pass
try:
del self.hash_to_fetch_tried[hash]
except:
pass
try:
del self.hash_to_fetch_totry[hash]
except:
pass
del self.share.client.meta_data[hash]
else:
self.get_peers(hash, block=False, limit=1000)
if time.time() - last_fail[hash] > 10:
try:
(ip, port) = self.hash_to_fetch_totry[hash].pop()
self.hash_to_fetch_tried[hash].add((ip, port))
self.share.client.add(ip, port, hash)
processed = True
except KeyError:
last_fail[hash] = time.time()
failed_count[hash] += 1
if failed_count[hash] >= 18:
self.share.client.meta_data[hash] = True
self.share.client.clean_hash(hash)
self.share.good_info_hash[hash] = time.time()
try:
del self.hash_to_fetch[hash]
except:
pass
try:
del self.hash_to_fetch_tried[hash]
except:
pass
try:
del self.hash_to_fetch_totry[hash]
except:
pass
self.debug(-1, "%s failed" % hash.encode("hex"))
del failed_count[hash]
del self.share.client.meta_data[hash]
if not processed:
self.sleep(10)
def clean(self):
pass
def clean_long(self):
if self.master:
now = time.time()
# cleanng old bad info_hash
for hash in self.share.bad_info_hash.keys():
try:
if now - self.share.bad_info_hash[hash] > 30 * 60:
del self.share.bad_info_hash[hash]
except KeyError:
pass
for hash in self.share.good_info_hash.keys():
try:
if now - self.share.good_info_hash[hash] > 30 * 60:
del self.share.good_info_hash[hash]
except KeyError:
pass
def on_get_peers_response(self, query, response):
if response.get("values"):
info_hash = query.get("info_hash")
if info_hash:
if (
info_hash not in self.hash_to_fetch and
info_hash not in self.share.hash_to_ignore
):
self.hash_to_fetch[info_hash] = time.time()
try:
del self.share.bad_info_hash[info_hash]
except KeyError:
pass
if info_hash in self.hash_to_fetch:
for ipport in response.get("values", []):
(ip, port) = struct.unpack("!4sH", ipport)
ip = socket.inet_ntoa(ip)
try:
if (ip, port) not in self.hash_to_fetch_tried[info_hash]:
if not netaddr.IPAddress(ip).is_private():
self.hash_to_fetch_totry[info_hash].add((ip, port))
else:
self.hash_to_fetch_tried[info_hash].add((ip, port))
except netaddr.AddrFormatError:
self.hash_to_fetch_tried[info_hash].add((ip, port))
def on_get_peers_query(self, query):
info_hash = query.get("info_hash")
if info_hash:
if (
info_hash not in self.share.bad_info_hash and
info_hash not in self.share.good_info_hash and
info_hash not in self.share.hash_to_ignore and
info_hash not in self.hash_to_fetch
):
self.determine_info_hash(info_hash)
def on_announce_peer_query(self, query):
info_hash = query.get("info_hash")
if info_hash:
if info_hash not in self.share.hash_to_ignore:
self.hash_to_fetch[info_hash] = time.time()
self.share.good_info_hash[info_hash] = time.time()
try:
del self.share.bad_info_hash[info_hash]
except KeyError:
pass
def get_hash_to_ignore(self, errornb=0):
try:
results = self.share.db.find(
{"status": {"$nin": [0, None]}},
{"_id": True, "status": False}
)
hashs = set(r["status"] for r in results)
self.debug(0, "Returning %s hash to ignore" % len(hashs))
return hashs
except pymongo.errors.AutoReconnect as e:
self.debug(0, "%r" % e)
if errornb > 10:
raise
time.sleep(0.1)
return self.get_hash_to_ignore(errornb=1+errornb)
def determine_info_hash(self, hash):
def callback(peers):
if peers:
self.share.good_info_hash[hash] = time.time()
else:
self.share.bad_info_hash[hash] = time.time()
self.get_peers(hash, delay=15, block=False, callback=callback, limit=1000)
def get_id(id_file, others=[]):
try:
with open(id_file) as f:
return ID(f.read(20))
except IOError:
print("generating new id")
id = ID()
while id[0] in others:
id = ID()
with open(id_file, "w+") as f:
f.write(id.value)
return id
def lauch(debug, index_nb, id_file="crawler1.id", lprefix="", worker_alive=None):
global stoped
print "%slauch %s" % (lprefix, id_file)
resource.setrlimit(resource.RLIMIT_NOFILE, (4096, 4096))
id_base = get_id(id_file)
pidfile = "%s.pid" % id_file
try:
with open(pidfile) as f:
pid = int(f.read().strip())
psutil.Process(pid)
print("pid %s is alive" % pid)
return
except (psutil.NoSuchProcess, IOError):
pass
pid = os.getpid()
with open(pidfile, 'w') as f:
f.write(str(pid))
if len(config.crawler_base_port) < config.crawler_worker:
ratio = max(config.crawler_worker // len(config.crawler_base_port), 1)
pool = index_nb // ratio
port_base = config.crawler_base_port[pool]
else:
port_base = config.crawler_base_port[index_nb]
prefix = 1
scheduler = Scheduler()
share = SharedObject()
print "%s%02d: using port %s" % (lprefix, prefix, port_base + ord(id_base[0]))
dht_base = Crawler(
bind_port=port_base + ord(id_base[0]),
id=id_base,
debuglvl=debug,
prefix="%s%02d:" % (lprefix, prefix),
master=True,
ignored_ip=config.ignored_ip,
ignored_net=config.ignored_net,
scheduler=scheduler,
share=share
)
liveness = [dht_base]
for id in enumerate_ids(config.crawler_instance, id_base):
if id == id_base:
continue
prefix += 1
bind_port = port_base + ord(id[0])
liveness.append(
Crawler(
bind_port=bind_port,
id=ID(id),
debuglvl=debug,
prefix="%s%02d:" % (lprefix, prefix),
ignored_ip=config.ignored_ip,
ignored_net=config.ignored_net,
scheduler=scheduler,
share=share
)
)
stoped = False
try:
for liv in liveness:
if stoped:
raise Exception("Stoped")
liv.start()
time.sleep(1.4142135623730951 * 0.1)
while True:
for liv in liveness:
if stoped:
raise Exception("Stoped")
if not liv.is_alive():
if liv.zombie:
raise Exception("Stoped Zombie")
raise Exception("Stoped")
print("thread stoped, restarting")
liv.start()
if worker_alive is not None and (time.time() - worker_alive.value) > 15:
raise Exception("Manager worker exited")
time.sleep(10)
except (KeyboardInterrupt, Exception) as e:
print("%r" % e)
stop(liveness)
print("exit")
def stop(liveness):
global stoped
stoped = True
print("start stopping")
s = []
for liv in liveness:
s.append(Thread(target=liv.stop))
for t in s:
t.daemon = True
t.start()
s = [t for t in s if t.is_alive()]
while s:
time.sleep(1)
s = [t for t in s if t.is_alive()]
def worker(debug):
jobs = {}
try:
worker_alive = multiprocessing.Value('i', int(time.time()))
others_ids = []
for i in range(1, config.crawler_worker + 1):
id_file = os.path.join(config.data_dir, "crawler%s.id" % i)
id = get_id(id_file, others_ids)
others_ids.append(id[0])
jobs[i] = multiprocessing.Process(
target=lauch,
args=(
debug,
(i - 1),
id_file,
"W%s:" % i,
worker_alive
)
)
jobs[i].daemon = True
jobs[i].start()
stats = {}
mem = {}
while True:
worker_alive.value = int(time.time())
for i, p in jobs.items():
# watch if the process has done some io
try:
pp = psutil.Process(p.pid)
mem[i] = pp.memory_info().rss
c = pp.io_counters()
if i in stats:
if c[0] != stats[i][0] or c[1] != stats[i][1]:
stats[i] = (c[0], c[1], time.time(), 0)
# if no io since more than 2 min, there is a problem
elif time.time() - stats[i][2] > 120:
print("crawler%s no activity since 2min, killing" % i)
if stats[i][3] < 5:
p.terminate()
stats[i] = stats[i][0:3] + (stats[i][3] + 1, )
else:
os.system("kill -9 %s" % p.pid)
else:
stats[i] = (c[0], c[1], time.time(), 0)
except(psutil.NoSuchProcess, psutil.AccessDenied):
try:
del stats[i]
except KeyError:
pass
if sum(mem.values()) > config.crawler_max_memory:
raise EnvironmentError("Reach memory limit, exiting")
# if a worker died then respan it
if not p.is_alive():
print("crawler%s died, respawning" % i)
jobs[i] = multiprocessing.Process(
target=lauch,
args=(debug, (i-1), os.path.join(config.data_dir, "crawler%s.id" % i), "W%s:" % i)
)
jobs[i].start()
try:
del stats[i]
except KeyError:
pass
time.sleep(10)
except (KeyboardInterrupt, EnvironmentError) as e:
print("%r" % e)
jobs = [j for j in jobs.values() if j.terminate() or j.is_alive()]
for i in range(40):
jobs = [j for j in jobs if j.is_alive()]
if not jobs:
break
time.sleep(0.1)
if jobs:
for i in range(10):
jobs = [j for j in jobs if j.terminate() or j.is_alive()]
if not jobs:
break
time.sleep(0.5)
if __name__ == '__main__':
debug = config.debug
for dir in [
config.torrents_dir, config.torrents_done, config.torrents_archive,
config.torrents_new, config.torrents_error, config.data_dir
]:
if not os.path.isdir(dir):
os.mkdir(dir)
for file in os.listdir(config.torrents_dir):
if file.endswith('.new'):
os.remove(os.path.join(config.torrents_dir, file))
if not isinstance(config.crawler_base_port, list):
config.crawler_base_port = [config.crawler_base_port]
worker(debug)