-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtorrentinfo-console.py
executable file
·132 lines (115 loc) · 4.83 KB
/
torrentinfo-console.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
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
#!/usr/bin/env python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Written by Henry 'Pi' James, Loring Holden and Matt Chisholm
app_name = "BitTorrent"
from BTL.translation import _
import time
from sys import *
from os.path import *
from sha import *
from BTL.bencode import *
from BitTorrent import version
NAME, EXT = splitext(basename(argv[0]))
print _("%s %s - decode %s metainfo files") % (NAME, version, app_name)
print
if len(argv) == 1:
print _("Usage: %s [TORRENTFILE [TORRENTFILE ... ] ]") % basename(argv[0])
print
exit(2) # common exit code for syntax error
labels = {'metafile' : _("metainfo file: %s" ),
'infohash' : _("info hash: %s" ),
'filename' : _("file name: %s" ),
'filesize' : _("file size:" ),
'files' : _("files:" ),
'title' : _("title: %s" ),
'dirname' : _("directory name: %s" ),
'creation date' : _("creation date: %s" ),
'archive' : _("archive size:" ),
'announce' : _("tracker announce url: %s"),
'announce-list' : _("tracker announce list: %s"),
'nodes' : _("trackerless nodes:" ),
'comment' : _("comment:" ),
'content_type' : _("content_type: %s" ),
'url-list' : _("url sources: %s" ),
}
maxlength = max( [len(v[:v.find(':')]) for v in labels.values()] )
# run through l10n-ed labels and make them all the same length
for k,v in labels.items():
if ':' in v:
index = v.index(':')
newlabel = v.replace(':', '.'*(maxlength-index) + ':')
labels[k] = newlabel
for metainfo_name in argv[1:]:
metainfo_file = open(metainfo_name, 'rb')
metainfo = bdecode(metainfo_file.read())
metainfo_file.close()
info = metainfo['info']
info_hash = sha(bencode(info))
if metainfo.has_key('title'):
print labels['title'] % metainfo['title']
print labels['metafile'] % basename(metainfo_name)
print labels['infohash'] % info_hash.hexdigest()
piece_length = info['piece length']
if info.has_key('length'):
# let's assume we just have a file
print labels['filename'] % info['name']
file_length = info['length']
name = labels['filesize']
if info.has_key('content_type'):
print labels['content_type'] % info['content_type']
else:
# let's assume we have a directory structure
print labels['dirname'] % info['name']
print labels['files']
file_length = 0;
for file in info['files']:
path = ''
for item in file['path']:
if (path != ''):
path = path + "/"
path = path + item
if file.has_key('content_type'):
print ' %s (%d,%s)' % (path, file['length'],
file['content_type'])
else:
print ' %s (%d)' % (path, file['length'])
file_length += file['length']
name = labels['archive']
piece_number, last_piece_length = divmod(file_length, piece_length)
print '%s %i (%i * %i + %i)' \
% (name,file_length, piece_number, piece_length, last_piece_length)
if metainfo.has_key('announce'):
print labels['announce'] % metainfo['announce']
if 'announce-list' in metainfo:
print labels['announce-list'] % metainfo['announce-list']
if metainfo.has_key('nodes'):
print labels['nodes']
for n in metainfo['nodes']:
print '\t%s\t:%d' % (n[0], n[1])
if metainfo.has_key('comment'):
print labels['comment'], metainfo['comment']
else:
print labels['comment']
if metainfo.has_key('url-list'):
print labels['url-list'] % '\n'.join(metainfo['url-list'])
if metainfo.has_key('creation date'):
fmt = "%a, %d %b %Y %H:%M:%S"
gm = time.gmtime(metainfo['creation date'])
s = time.strftime(fmt, gm)
print labels['creation date'] % s
# DANGER: modifies torrent file
if False:
metainfo_file = open(metainfo_name, 'wb')
metainfo_file.write(bencode(metainfo))
metainfo_file.close()