Here is the code from Alix above, modified to run under python 3.x
import re
def tokenize(text, match=re.compile(b"([idel])|(\d+):|(-?\d+)").match):
i = 0
while i < len(text):
m = match(text, i)
s = m.group(m.lastindex)
i = m.end()
if m.lastindex == 2:
yield "s"
yield text[i:i+int(s)].decode("utf-8", "ignore")
i = i + int(s)
else:
yield s.decode("utf-8", "ignore")
def decode_item(torrent, token):
if token == "i":
# integer: "i" value "e"
data = int(next(torrent))
if next(torrent) != "e":
raise ValueError
elif token == "s":
data = next(torrent)
elif token == "l" or token == "d":
# container: "l" (or "d") values "e"
data = []
tok = next(torrent)
while tok != "e":
data.append(decode_item(torrent, tok))
tok = next(torrent)
if token == "d":
data = dict(zip(data[0::2], data[1::2]))
else:
raise ValueError
return data
def decode(text):
try:
src = tokenize(text)
token=next(src)
data = decode_item(src, token)
for token in src: # look for more tokens
raise SyntaxError("trailing junk")
except (AttributeError, ValueError, StopIteration):
raise SyntaxError("syntax error")
return data
n = 0
if __name__ == "__main__":
data = open("test.torrent", "rb").read()
torrent = decode(data)
for file in torrent["info"]["files"]:
n = n + 1
filenamepath = file["path"]
print(str(n) + " -- " + ', '.join(map(str, filenamepath)))
fname = ', '.join(map(str, filenamepath))
print(fname + " -- " + str(file["length"]))
print(f.path.replace("test/", ""))
– Najera