-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·137 lines (109 loc) · 3.82 KB
/
server.py
File metadata and controls
executable file
·137 lines (109 loc) · 3.82 KB
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
#!/usr/bin/env python
import os
import sys
import libtorrent as lt
import time
import urllib
import pymongo
class HandleWrap:
def __init__(self, handle):
self.handle = handle
def peers(self):
return self.handle.get_peer_info()
class MagnetDaemon:
def __init__(self):
self.torrents = []
self.session = lt.session()
# self.session.listen_on(8888, 9999)
self.session.start_dht()
# self.session.stop_upnp()
# self.session.stop_natpmp()
self.mongconn = pymongo.Connection()
self.db = self.mongconn['magnet']
self.peers = self.db.peers
def create_handle(self, link=None, path=None):
params = {
'save_path': 'tmp',
# 'upload_mode': True,
'storage_mode': lt.storage_mode_t.storage_mode_compact,
'paused': False,
'auto_managed': True,
}
if link:
return lt.add_magnet_uri(self.session, link, params)
if path:
return lt.add_files(self.session, path, params)
def create_magnet_url(self, info_hash):
trackers = [
'http://tracker.openbittorent.org/announce',
'http://tracker.publicbt.org/announce',
]
s = 'magnet:?xt=urn:btih:%s' % info_hash
for t in trackers:
s += '&tr='
s += urllib.quote(t)
return s
def add_from_hash(self, info_hash):
link = self.create_magnet_url(info_hash)
handle = self.create_handle(link=link)
self.add_handle(handle)
def add_handle(self, handle):
torrent = HandleWrap(handle)
self.torrents.append(torrent)
return torrent
def add_hashes_from_dump(self, path):
print 'Reading', path
for no, line in enumerate(open(path)):
if no == 500:
break
line = line.strip()
info_hash = self.hash_from_line(line)
self.add_from_hash(info_hash)
def hash_from_line(self, line):
h = line.split('|')
return h[-1]
def monitor(self):
while 1:
for torrent in self.torrents:
# print torrent.handle.status().distributed_full_copies,
# print torrent.handle.status().current_tracker,
# print torrent.handle.status().num_seeds,
# print torrent.handle.status().list_peers,
# print torrent.handle.piece_availability(),
# print torrent.handle.has_metadata(),
# print
# if torrent.handle.is_paused():
# continue
if not torrent.handle.has_metadata():
continue
self.add_peers_to_db(torrent)
# Now remove this torrent so we can leave room for others
self.session.remove_torrent(torrent.handle)
self.torrents.remove(torrent)
print 80 * '-'
time.sleep(1)
def add_peers_to_db(self, torrent):
peers = torrent.peers()
print len(peers), torrent.handle.name()
for p in peers:
# print ' - %s %s' % (p.ip, p.client)
addr = p.ip[0]
peer = self.peers.find_one({'addr': addr})
if not peer:
peer = {
'addr': addr,
'torrents': []
}
peer['client'] = p.client
ih = str(torrent.handle.info_hash())
if ih not in peer['torrents']:
peer['torrents'].append(ih)
self.peers.save(peer)
def main():
md = MagnetDaemon()
# md.add_from_hash('2ded86af7c0cce8224262ba703191bf7d8537a5d')
# md.add_from_hash('368a8beaaced1572f44ad5aae0685ed6130d983c')
md.add_hashes_from_dump('complete')
md.monitor()
if __name__ == '__main__':
main()