-
Notifications
You must be signed in to change notification settings - Fork 9
/
mag2tor.py
executable file
·76 lines (71 loc) · 2.15 KB
/
mag2tor.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
#!/usr/bin/python
#
# Inspired by the Magnet2Torrent script from the
# https://github.com/danfolkes/Magnet2Torrent.git
import libtorrent as lt
import time
import sys
import os.path
def mag2tor_v1(mag_link):
sess = lt.session()
prms = {
'save_path':os.path.abspath(os.path.curdir),
# 'storage_mode':lt.storage_mode_t(2),
'paused':False,
'auto_managed':False,
'upload_mode':True,
}
torr = lt.add_magnet_uri(sess, mag_link, prms)
dots = 0
sys.stdout.write('%s: ' % mag_link)
while not torr.has_metadata():
dots += 1
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
if (dots): sys.stdout.write(' ')
sess.pause()
tinf = torr.get_torrent_info()
sys.stdout.write('%s.torrent\n' % tinf.name())
f = open(tinf.name() + '.torrent', 'wb')
f.write(lt.bencode(lt.create_torrent(tinf).generate()))
f.close()
sess.remove_torrent(torr)
def mag2tor_v2(mag_link):
sess = lt.session()
atp = lt.parse_magnet_uri(mag_link)
atp.save_path = "."
atp.flags = lt.torrent_flags.upload_mode
torr = sess.add_torrent(atp)
dots = 0
sys.stdout.write('%s: ' % mag_link)
while not torr.status().has_metadata:
dots += 1
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
if (dots): sys.stdout.write(' ')
sess.pause()
tinf = torr.torrent_file()
# Workaround for empty torrent_info.trackers() in
# libtorrent-rasterbar-2.0.7:
trn = 0
for t in tinf.trackers(): trn += 1
if trn == 0:
for t in atp.trackers:
tinf.add_tracker(t)
sys.stdout.write('%s.torrent\n' % tinf.name())
f = open(tinf.name() + '.torrent', 'wb')
f.write(lt.bencode(lt.create_torrent(tinf).generate()))
f.close()
sess.remove_torrent(torr)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.stderr.write('USAGE: %s <magnet_link>\n' % sys.argv[0])
sys.exit(1)
for mag_link in sys.argv[1:]:
if not hasattr(lt, 'version_major') or lt.version_major < 2:
mag2tor_v1(mag_link)
else:
mag2tor_v2(mag_link)
# vi:set sw=4 et ts=8 tw=71: