[英]Is there any way to convert a torrent file to magnet link?
Recently, I'm doing things related to magnet link.最近在做磁链相关的事情。 What I want to do is to convert a torrent file to magnet link.我想要做的是将 torrent 文件转换为磁力链接。
I've tried given-a-torrent-file-how-do-i-generate-a-magnet-link-in-python , but get an error when issuing command metadata = bencode.bdecode(torrent)
:我已经尝试过given-a-torrent-file-how-do-i-generate-a-magnet-link-in-python ,但是在发出命令metadata = bencode.bdecode(torrent)
时出现错误:
"bencode.BTL.BTFailure: not a valid bencoded string" “bencode.BTL.BTFailure:不是有效的编码字符串”
Then I tried installing python-libtorrent
, but failed to finish the installation.然后我尝试安装python-libtorrent
,但未能完成安装。
Is there a way to do it in Java?有没有办法在Java中做到这一点? If not, how can this be easily done in Python, Thanks a lot!如果没有,这如何在 Python 中轻松完成,非常感谢!
I didn't check if this works but it's a reference to start, follow this link for a example in python using the bencode library.我没有检查这是否有效,但它是开始的参考,请按照此链接查看使用 bencode 库的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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.