Skip to content

Instantly share code, notes, and snippets.

@codeskyblue
Created February 19, 2014 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeskyblue/9085646 to your computer and use it in GitHub Desktop.
Save codeskyblue/9085646 to your computer and use it in GitHub Desktop.
torrent信息查看工具 showmetainfo
#!/usr/bin/python
# Written by Henry 'Pi' James and Loring Holden
# modified for multitracker display by John Hoffman
# see LICENSE.txt for license information
# dependency
# $ pip install bencode
from sys import *
from os.path import *
try:
from hashlib import sha1 as sha
except ImportError:
from sha import sha
from bencode import *
NAME, EXT = splitext(basename(argv[0]))
VERSION = '20030621'
print '%s %s - decode BitTorrent metainfo files' % (NAME, VERSION)
print
if len(argv) == 1:
print '%s file1.torrent file2.torrent file3.torrent ...' % argv[0]
print
exit(2) # common exit code for syntax error
for metainfo_name in argv[1:]:
print 'metainfo file.: %s' % basename(metainfo_name)
metainfo_file = open(metainfo_name, 'rb')
metainfo = metainfo_file.read()
metainfo_file.close()
metainfo = bdecode(metainfo)
try:
info = metainfo['info']
info_hash = sha(bencode(info))
print 'info hash.....: %s' % info_hash.hexdigest()
piece_length = info['piece length']
if info.has_key('length'):
# let's assume we just have a file
print 'file name.....: %s' % info['name']
file_length = info['length']
name ='file size.....:'
else:
# let's assume we have a directory structure
print 'directory name: %s' % info['name']
print 'files.........: '
file_length = 0;
for file in info['files']:
path = ''
for item in file['path']:
if (path != ''):
path = path + "/"
path = path + item
print ' %s (%d)' % (path, file['length'])
file_length += file['length']
name ='archive size..:'
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 'announce url..: %s' % metainfo['announce']
else:
print '***WARNING*** - no announce key'
if metainfo.has_key('announce-list'):
list = []
for tier in metainfo['announce-list']:
for tracker in tier:
list+=[tracker,',']
del list[-1]
list+=['|']
del list[-1]
liststring = ''
for i in list:
liststring+=i
print 'announce-list.: %s' % liststring
if metainfo.has_key('httpseeds'):
list = []
for seed in metainfo['httpseeds']:
list += [seed,'|']
del list[-1]
liststring = ''
for i in list:
liststring+=i
print 'http seeds....: %s' % liststring
if metainfo.has_key('comment'):
print 'comment.......: %s' % metainfo['comment']
except:
print '***ERROR*** - metainfo out of spec'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment