forked from xrgtn/mag2tor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mag2tor.py
executable file
·43 lines (39 loc) · 1.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
#!/usr/bin/env python
#
# Inspired by the Magnet2Torrent script from the
# https://github.com/danfolkes/Magnet2Torrent.git
import libtorrent
import time
import sys
import os.path
def mag2tor(mag_link):
sess = libtorrent.session()
prms = {
'save_path':os.path.abspath(os.path.curdir),
# 'storage_mode':libtorrent.storage_mode_t(2),
'paused':False,
'auto_managed':False,
'upload_mode':True,
}
torr = libtorrent.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('\n')
sess.pause()
tinf = torr.get_torrent_info()
f = open(tinf.name() + '.torrent', 'wb')
f.write(libtorrent.bencode(
libtorrent.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:]: mag2tor(mag_link)
# vi:set sw=4 et ts=8 tw=71: