import bencodepy
import hashlib
import base64
import os
list_all = []
temp = input(‘请输入您要导出的盘符与文件名:’) #torrnet文件名称不能有中文,只允许字母和数字
ii = str(temp)
torrentName = ii + ‘.torrent’
torrent = open(torrentName, ‘rb’).read()
metadata = bencodepy.bdecode(torrent)
metadata1 = {}
for key in metadata.keys():
key1 = key.decode(“utf-8”)
metadata1[key1] = metadata[key]
hashcontents = bencodepy.bencode(metadata1[‘info’])
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest).decode(“utf-8”)
输出链接
print(‘magnet:?xt=urn:btih:%s’ % b32hash)