2017-03-01 16 views
0

我需要生成包含.torrent文件的Unicode/UTF-8內容的主列表文本文件。可以搜索此文件以查找特定文件以及它來自哪個torrent。從Unicode中讀取Unicode中的元數據

類似的問題在這裏回答: Reading the fileset from a torrent

但意見之一是,該解決方案腳本有統一的問題。 「具有一定的unicode的問題,但工程:) - xvan年09月27 '16 3:36」

如何修改腳本,使它能夠統一?

+1

我沒有看到這裏的腳本,或者您有任何具體問題。一個鏈接,即使是SO,也不構成實際發佈您遇到問題並描述您的問題的代碼的有效替代。 –

+0

我自己已經解決了這個問題。以下是在Unicode中處理洪流內的torrent文件名/路徑和文件名的工作代碼。 – Alix

回答

0
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"]) 
0

下面的代碼使用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)