-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2pacmand.py
80 lines (65 loc) · 2.06 KB
/
p2pacmand.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
#!/usr/bin/python
import libtorrent as lt
import time
import logging
import threading
import os
#logging.basicConfig(level=DEBUG)
# rescanning package cache every X seconds
# nach einer zeit "stalled" das seeding
# nicht packete hinzufügen, wenn pacman db log aktiv
# systemd service file
ses = lt.session()
class torrent:
def __init__(self, path, item):
self.path = path
self.item = item
info = lt.torrent_info(path+"/"+item)
self.h = ses.add_torrent(info, path)
def print_stat(self):
h = self.h
s = h.status()
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating']
print ('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]))
class threadstart(threading.Thread):
def __init__(self, path, item):
threading.Thread.__init__(self)
self.path = path
self.item = item
def run(self):
#logging.debug('Loading torrent file in thread: '+self.path)
print("Loading torrent file in thread")
print(self.path)
print(self.item)
package = torrent(self.path, self.item)
def walklevel(some_dir, level):
some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir):
yield root, dirs, files
num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this:
del dirs[:]
def scandir(path, level):
if os.path.exists(path):
for (path, dirs, files) in walklevel(path,level):
for item in files:
if ".torrent" in item:
thread = threadstart(path,item)
thread.start()
time.sleep(1)
else:
print("File or directory does not exists")
state = None
ses.start_dht(state)
ses.add_dht_router("router.bittorrent.com", 6881)
ses.add_dht_router("router.utorrent.com", 6881)
ses.add_dht_router("router.bitcomet.com", 6881)
ses.listen_on(6881, 6891)
scandir("/var/cache/pacman/pkg",1)
while True:
time.sleep(5)