【问题标题】:Reading metadata in Unicode from a torrent从 torrent 中读取 Unicode 元数据
【发布时间】:2017-03-01 18:39:47
【问题描述】:

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

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

但其中一个问题是解决方案脚本存在 Unicode 问题。 “有一些 unicode 问题,但可以工作 :) – xvan 2016 年 9 月 27 日 3:36”

如何修改该脚本以使其支持 Unicode?​​p>

【问题讨论】:

  • 我在这里看不到脚本,也看不到您遇到的任何具体问题。即使是指向 SO 的链接,也不能有效替代实际发布您遇到问题的代码和描述您的问题。
  • 已经自己解决了问题。这是处理 Unicode 中 torrent 文件名/路径和文件名的工作代码。

标签: python unicode torrent


【解决方案1】:
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"])

【讨论】:

    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-30
      • 2010-12-22
      • 1970-01-01
      • 2011-07-03
      • 2017-04-29
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多