最近,我正在做与磁铁相关的事情。我想要做的是将torrent文件转换为磁力链接。
我已尝试given-a-torrent-file-how-do-i-generate-a-magnet-link-in-python,但在发出命令metadata = bencode.bdecode(torrent)
时出错:
“bencode.BTL.BTFailure:不是有效的bencoded字符串”
然后我尝试安装python-libtorrent
,但未能完成安装。
有没有办法在Java中实现?如果没有,如何在Python中轻松完成,非常感谢!
答案 0 :(得分:0)
我没有检查这是否可行,但这是使用bencode库开始follow this link for a example in python的参考。
#!/usr/bin/python
import sys
import urllib
import bencode
import hashlib
import base64
if len(sys.argv) == 0:
print("Usage: file")
exit()
torrent = open(sys.argv[1], 'r').read()
metadata = bencode.bdecode(torrent)
hashcontents = bencode.bencode(metadata['info'])
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest)
params = {'xt': 'urn:btih:%s' % b32hash,
'dn': metadata['info']['name']}
announcestr = ''
for announce in metadata['announce-list']:
announcestr += '&' + urllib.urlencode({'tr':announce[0]})
paramstr = urllib.urlencode(params) + announcestr
magneturi = 'magnet:?%s' % paramstr
print(magneturi)