Parsing 从torrent中读取文件集
我想(快速)将一个程序/脚本放在一起,从.torrent文件中读取文件集。然后我想使用该集合删除特定目录中不属于torrent的任何文件 关于从.torrent文件读取此索引的便捷库,有什么建议吗?虽然我不反对它,但我不想为了这个简单的目的而深入挖掘bittorrent规范并从头开始加载代码Parsing 从torrent中读取文件集,parsing,bittorrent,Parsing,Bittorrent,我想(快速)将一个程序/脚本放在一起,从.torrent文件中读取文件集。然后我想使用该集合删除特定目录中不属于torrent的任何文件 关于从.torrent文件读取此索引的便捷库,有什么建议吗?虽然我不反对它,但我不想为了这个简单的目的而深入挖掘bittorrent规范并从头开始加载代码 我对语言没有偏好。原始主线BitTorrent 5.x客户端()中的bencode.py将为您提供Python中的参考实现 它对BTL包有一个导入依赖项,但很容易删除。然后你看BeNoC.BDCODE(Fi
我对语言没有偏好。原始主线BitTorrent 5.x客户端()中的bencode.py将为您提供Python中的参考实现 它对BTL包有一个导入依赖项,但很容易删除。然后你看BeNoC.BDCODE(FielCeCon)['Field]([Fix])[< /P>< p>我会使用RASTBAR的小而快的C++库。 要迭代文件,可以使用类(begin\u files(),end\u files()) libtorrent还有一个解决方案:
import libtorrent
info = libtorrent.torrent_info('test.torrent')
for f in info.files():
print "%s - %s" % (f.path, f.size)
。以下是从.torrent文件(Python 2.4+)读取文件列表的完整代码:
重新导入
def标记化(text,match=re.compile(([idel])|(\d+):|((\d+))).match):
i=0
而我
在上述思想的基础上,我做了以下工作:
~> cd ~/bin
~/bin> ls torrent*
torrent-parse.py torrent-parse.sh
~/bin> cat torrent-parse.py
# torrent-parse.py
import sys
import libtorrent
# get the input torrent file
if (len(sys.argv) > 1):
torrent = sys.argv[1]
else:
print "Missing param: torrent filename"
sys.exit()
# get names of files in the torrent file
info = libtorrent.torrent_info(torrent);
for f in info.files():
print "%s - %s" % (f.path, f.size)
~/bin> cat torrent-parse.sh
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Missing param: torrent filename"
exit 0
fi
python torrent-parse.py "$*"
您需要适当设置权限以使shell脚本可执行:
~/bin> chmod a+x torrent-parse.sh
希望这对某人有所帮助:)这是上面康斯坦丁答案中的代码,稍作修改以处理torrent文件名中的Unicode字符和torrent信息中的文件集文件名:
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"])
重新导入
def标记化(text,match=re.compile(([idel])|(\d+):|((\d+))).match):
i=0
而我