#!/usr/bin/env python2.2 # By Nelson Minar 2003-05-26 # http://www.nelson.monkey.org/~nelson/weblog/tech/bittorrent/ # This code is placed in the public domain # Reference: http://bitconjurer.org/BitTorrent/protocol.html """btdump - dump info from a BitTorrent .torrent file Usage: btdump """ import sys, urllib, time from BitTorrent.bencode import bdecode def dumpItem(item, depth=0): "Recursively print out a BitTorrent data structure with indentation." if isinstance(item, dict): for k in item: if isinstance(item[k], list) and k == 'path': # Path gets special treatment output(k, "/".join(item[k]), depth*2) elif isinstance(item[k], dict) or isinstance(item[k], list): output(k, "", depth * 2) dumpItem(item[k], depth + 1) else: output(k, item[k], depth * 2) elif isinstance(item, list): for i in item: dumpItem(i, depth + 1) else: output("", item, depth) def output(label, data, indent=0): if label == 'pieces': # special case pieces data data = "[%d SHA-1 values]" % (len(data) / 20) elif label == 'creation date': # special case creationd ate try: data = "[%s]" % time.ctime(float(data)) except: pass out = " " * indent out += "%-19s %s" % (label, data) print out def main(argv): for a in argv[1:]: try: fp = open(a) except IOError: try: fp = urllib.urlopen(a) except: print "Cannot open" + a sys.exit(1) if len(argv) > 2: print a t = fp.read() fp.close() dumpItem(bdecode(t)) if len(argv) > 2: print if __name__ == '__main__': main(sys.argv)