/
torrent.py
executable file
·47 lines (41 loc) · 1.08 KB
/
torrent.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
#!/usr/bin/env python
import sys
import os
from types import StringType
# get bencode package from http://github.com/fishy/scripts/downloads
from bencode.bencode import bencode, bdecode, BTFailure
try :
torrent = sys.argv[1]
except IndexError :
print "Usage: \"%s <torrent_file> [tracker_url]\" to show torrent info (without tracker_url), or to add tracker(s)" % sys.argv[0]
sys.exit()
size = os.stat(torrent).st_size
file = open(torrent, "rb")
data = file.read(size)
file.close()
info = bdecode(data)
if len(sys.argv) == 2 :
print info
sys.exit()
if 'announce-list' not in info :
list = [info['announce']]
for i in range(len(sys.argv)-2) :
tracker = sys.argv[i+2]
if tracker not in list :
list.append(tracker)
print list
info['announce-list'] = [list]
else :
list = info['announce-list'][0]
if type(list) == StringType :
list = [list]
for i in range(len(sys.argv)-2) :
tracker = sys.argv[i+2]
if tracker not in list :
list.append(tracker)
print list
info['announce-list'][0] = list
writedata = bencode(info)
file = open(torrent, "wb")
file.write(writedata)
file.close()