1
0

btshowmetainfo.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. # Written by Henry 'Pi' James and Loring Holden
  3. # modified for multitracker display by John Hoffman
  4. # see LICENSE.txt for license information
  5. # $Id: btshowmetainfo.py 2054 2006-12-30 16:57:55Z b4rt $
  6. from sys import *
  7. from os.path import *
  8. from sha import *
  9. from BitTornado.bencode import *
  10. NAME, EXT = splitext(basename(argv[0]))
  11. VERSION = '20030621'
  12. print '%s %s - decode BitTorrent metainfo files' % (NAME, VERSION)
  13. print
  14. if len(argv) == 1:
  15. print '%s file1.torrent file2.torrent file3.torrent ...' % argv[0]
  16. print
  17. exit(2) # common exit code for syntax error
  18. for metainfo_name in argv[1:]:
  19. metainfo_file = open(metainfo_name, 'rb')
  20. metainfo = bdecode(metainfo_file.read())
  21. # print metainfo
  22. info = metainfo['info']
  23. info_hash = sha(bencode(info))
  24. print 'metainfo file.: %s' % basename(metainfo_name)
  25. print 'info hash.....: %s' % info_hash.hexdigest()
  26. piece_length = info['piece length']
  27. if info.has_key('length'):
  28. # let's assume we just have a file
  29. print 'file name.....: %s' % info['name']
  30. file_length = info['length']
  31. name ='file size.....:'
  32. else:
  33. # let's assume we have a directory structure
  34. print 'directory name: %s' % info['name']
  35. print 'files.........: '
  36. file_length = 0;
  37. for file in info['files']:
  38. path = ''
  39. for item in file['path']:
  40. if (path != ''):
  41. path = path + "/"
  42. path = path + item
  43. print ' %s (%d)' % (path, file['length'])
  44. file_length += file['length']
  45. name ='archive size..:'
  46. piece_number, last_piece_length = divmod(file_length, piece_length)
  47. print '%s %i (%i * %i + %i)' \
  48. % (name,file_length, piece_number, piece_length, last_piece_length)
  49. print 'announce url..: %s' % metainfo['announce']
  50. if metainfo.has_key('announce-list'):
  51. list = []
  52. for tier in metainfo['announce-list']:
  53. for tracker in tier:
  54. list+=[tracker,',']
  55. del list[-1]
  56. list+=['|']
  57. del list[-1]
  58. liststring = ''
  59. for i in list:
  60. liststring+=i
  61. print 'announce-list.: %s' % liststring
  62. if metainfo.has_key('httpseeds'):
  63. list = []
  64. for seed in metainfo['httpseeds']:
  65. list += [seed,'|']
  66. del list[-1]
  67. liststring = ''
  68. for i in list:
  69. liststring+=i
  70. print 'http seeds....: %s' % liststring
  71. if metainfo.has_key('comment'):
  72. print 'comment.......: %s' % metainfo['comment']