]> git.phdru.name Git - bittorrent.git/commitdiff
Feat: List content of a torrent metafile (full format with lengths) master
authorOleg Broytman <phd@phdru.name>
Sat, 30 Aug 2025 20:46:57 +0000 (23:46 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 1 Sep 2025 16:28:24 +0000 (19:28 +0300)
ls-torrent-full-len [new file with mode: 0755]

diff --git a/ls-torrent-full-len b/ls-torrent-full-len
new file mode 100755 (executable)
index 0000000..3ff665d
--- /dev/null
@@ -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))