1
0

torrentinfo-console.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. # The contents of this file are subject to the BitTorrent Open Source License
  3. # Version 1.1 (the License). You may not copy or use this file, in either
  4. # source code or executable form, except in compliance with the License. You
  5. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  6. #
  7. # Software distributed under the License is distributed on an AS IS basis,
  8. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  9. # for the specific language governing rights and limitations under the
  10. # License.
  11. # Written by Henry 'Pi' James, Loring Holden and Matt Chisholm
  12. app_name = "BitTorrent"
  13. from BTL.translation import _
  14. import time
  15. from sys import *
  16. from os.path import *
  17. from sha import *
  18. from BTL.bencode import *
  19. from BitTorrent import version
  20. NAME, EXT = splitext(basename(argv[0]))
  21. print _("%s %s - decode %s metainfo files") % (NAME, version, app_name)
  22. print
  23. if len(argv) == 1:
  24. print _("Usage: %s [TORRENTFILE [TORRENTFILE ... ] ]") % basename(argv[0])
  25. print
  26. exit(2) # common exit code for syntax error
  27. labels = {'metafile' : _("metainfo file: %s" ),
  28. 'infohash' : _("info hash: %s" ),
  29. 'filename' : _("file name: %s" ),
  30. 'filesize' : _("file size:" ),
  31. 'files' : _("files:" ),
  32. 'title' : _("title: %s" ),
  33. 'dirname' : _("directory name: %s" ),
  34. 'creation date' : _("creation date: %s" ),
  35. 'archive' : _("archive size:" ),
  36. 'announce' : _("tracker announce url: %s"),
  37. 'announce-list' : _("tracker announce list: %s"),
  38. 'nodes' : _("trackerless nodes:" ),
  39. 'comment' : _("comment:" ),
  40. 'content_type' : _("content_type: %s" ),
  41. 'url-list' : _("url sources: %s" ),
  42. }
  43. maxlength = max( [len(v[:v.find(':')]) for v in labels.values()] )
  44. # run through l10n-ed labels and make them all the same length
  45. for k,v in labels.items():
  46. if ':' in v:
  47. index = v.index(':')
  48. newlabel = v.replace(':', '.'*(maxlength-index) + ':')
  49. labels[k] = newlabel
  50. for metainfo_name in argv[1:]:
  51. metainfo_file = open(metainfo_name, 'rb')
  52. metainfo = bdecode(metainfo_file.read())
  53. metainfo_file.close()
  54. info = metainfo['info']
  55. info_hash = sha(bencode(info))
  56. if metainfo.has_key('title'):
  57. print labels['title'] % metainfo['title']
  58. print labels['metafile'] % basename(metainfo_name)
  59. print labels['infohash'] % info_hash.hexdigest()
  60. piece_length = info['piece length']
  61. if info.has_key('length'):
  62. # let's assume we just have a file
  63. print labels['filename'] % info['name']
  64. file_length = info['length']
  65. name = labels['filesize']
  66. if info.has_key('content_type'):
  67. print labels['content_type'] % info['content_type']
  68. else:
  69. # let's assume we have a directory structure
  70. print labels['dirname'] % info['name']
  71. print labels['files']
  72. file_length = 0;
  73. for file in info['files']:
  74. path = ''
  75. for item in file['path']:
  76. if (path != ''):
  77. path = path + "/"
  78. path = path + item
  79. if file.has_key('content_type'):
  80. print ' %s (%d,%s)' % (path, file['length'],
  81. file['content_type'])
  82. else:
  83. print ' %s (%d)' % (path, file['length'])
  84. file_length += file['length']
  85. name = labels['archive']
  86. piece_number, last_piece_length = divmod(file_length, piece_length)
  87. print '%s %i (%i * %i + %i)' \
  88. % (name,file_length, piece_number, piece_length, last_piece_length)
  89. if metainfo.has_key('announce'):
  90. print labels['announce'] % metainfo['announce']
  91. if 'announce-list' in metainfo:
  92. print labels['announce-list'] % metainfo['announce-list']
  93. if metainfo.has_key('nodes'):
  94. print labels['nodes']
  95. for n in metainfo['nodes']:
  96. print '\t%s\t:%d' % (n[0], n[1])
  97. if metainfo.has_key('comment'):
  98. print labels['comment'], metainfo['comment']
  99. else:
  100. print labels['comment']
  101. if metainfo.has_key('url-list'):
  102. print labels['url-list'] % '\n'.join(metainfo['url-list'])
  103. if metainfo.has_key('creation date'):
  104. fmt = "%a, %d %b %Y %H:%M:%S"
  105. gm = time.gmtime(metainfo['creation date'])
  106. s = time.strftime(fmt, gm)
  107. print labels['creation date'] % s
  108. # DANGER: modifies torrent file
  109. if False:
  110. metainfo_file = open(metainfo_name, 'wb')
  111. metainfo_file.write(bencode(metainfo))
  112. metainfo_file.close()