-
Notifications
You must be signed in to change notification settings - Fork 2
/
magnet2torrent.py
58 lines (54 loc) · 1.64 KB
/
magnet2torrent.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
import libtorrent
import time
import tempfile
import binascii
import sys
def magnet2torrent(link, torrent_file):
sess = libtorrent.session()
params = {
"save_path": tempfile.gettempdir(),
"storage_mode":libtorrent.storage_mode_t.storage_mode_sparse,
"paused": True,
"auto_managed": True,
"duplicate_is_error": True
}
handle = libtorrent.add_magnet_uri(sess, link, params)
print "Loading magnet file"
while True:
s = handle.status()
print "waiting..."
if s.state != 2:
t = handle.get_torrent_info()
print "Saving torrent -=%s=-" % t.name()
fs = libtorrent.file_storage()
for i in t.files():
fs.add_file(i)
print "\tFile: %s" % i.path
ct = libtorrent.create_torrent(fs)
for i in t.trackers():
print "\tTracker: %s, %s " % (i.url, i.tier)
ct.add_tracker(i.url, i.tier)
ct.set_creator(t.creator())
ct.set_comment(t.comment())
ct.set_priv(t.priv())
f = open(torrent_file, "wb")
g = ct.generate()
g["info"]["pieces"] = "".join([binascii.unhexlify("%s" % t.hash_for_piece(i)) for i in range(t.num_pieces())])
g["info"]["piece length"] = t.piece_length()
g["info"]["length"] = t.total_size()
f.write(libtorrent.bencode(g))
f.close()
return
time.sleep(1) # sleep for a second
if __name__ == "__main__":
if len(sys.argv) < 2:
print """
Usage:
magnet2torrent.py "magnet:?..." [output file]
"""
sys.exit()
link = sys.argv[1]
file_name = "torrent.torrent"
if len(sys.argv) >= 3:
file_name = sys.argv[2]
magnet2torrent(link, file_name)