从torren读取Unicode元数据

2024-06-17 10:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要生成一个主列表文本文件,其中包含.torrent文件的Unicode/UTF-8格式的内容。可以搜索此文件以查找特定文件以及它来自哪个torrent。你知道吗

这里也回答了类似的问题: Reading the fileset from a torrent

但其中一条评论是,解决方案脚本存在Unicode问题。 “有一些unicode问题,但有效:)–xvan 9月27日3:36”

如何修改脚本以使其具有Unicode功能?你知道吗


Tags: 文件thefrom脚本内容列表格式unicode
2条回答

以下是使用libtorrent的代码:

import libtorrent

info = libtorrent.torrent_info('C:\\Torrents\\test.torrent')

n = 0
for f in info.files():
    #print (f.path, f.size)
    # print "%s - %s" % (f.path, f.size)
    n = n + 1
    filenamepath = str(f.path)
    filesize = str(f.size)
    print str(n) + "   " + filenamepath + "   " + str(filesize)
import re

def tokenize(text, match=re.compile("([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)]
        i = i + int(s)
    else:
        yield s

def decode_item(next, token):
if token == "i":
    # integer: "i" value "e"
    data = int(next())
    if next() != "e":
        raise ValueError
elif token == "s":
    # string: "s" value (virtual tokens)
    data = next()
elif token == "l" or token == "d":
    # container: "l" (or "d") values "e"
    data = []
    tok = next()
    while tok != "e":
        data.append(decode_item(next, tok))
        tok = next()
    if token == "d":
        data = dict(zip(data[0::2], data[1::2]))
else:
    raise ValueError
return data

def decode(text):
    try:
        src = tokenize(text)
        data = decode_item(src.next, src.next())
        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("C:\\Torrents\\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"])

相关问题 更多 >