From 78fc59db3bc32aa7c5bb8615efca29f5fd5fe622 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 30 Aug 2025 23:46:57 +0300 Subject: [PATCH] Feat: List content of a torrent metafile (full format with lengths) --- ls-torrent-full-len | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 ls-torrent-full-len diff --git a/ls-torrent-full-len b/ls-torrent-full-len new file mode 100755 index 0000000..3ff665d --- /dev/null +++ b/ls-torrent-full-len @@ -0,0 +1,43 @@ +#! /usr/bin/env python3 + +import sys +from eff_bdecode import eff_bdecode + +torrent_file = open(sys.argv[1], 'rb') +data = torrent_file.read() +torrent_file.close() + +data = eff_bdecode(data) + + +def get_value_len(value): + if isinstance(value, bytes): + return "(blen:%d)%s" % (len(value), value) + elif isinstance(value, dict): + res = "(keys:%d){" % len(value) + res += ', '.join( + ['%s: %s' % (k, get_value_len(v)) for k, v in value.items()] + ) + res += ']' + return res + elif isinstance(value, list): + res = "(values:%d)[" % len(value) + res += ', '.join([get_value_len(item) for item in value]) + res += ']' + return res + elif isinstance(value, str): + return "(strlen:%d)%s" % (len(value), value) + elif isinstance(value, int): + return str(value) + else: + raise TypeError('Unknown type %r %s' % (type(value), value)) + + +print('Meta:') +for key, value in data.items(): + if key != b'info': + print(key, ': ', get_value_len(value)) +if b'info' in data: + print('\nInfo:') + for key, value in data[b'info'].items(): + print(key, ': ', get_value_len(value)) -- 2.39.5