欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用Python 3从种子文件torrent中提取磁力链接的方法

最编程 2024-01-23 19:15:33
...


依赖

pip install bencode.py

代码

import bencode
import hashlib
from urllib.parse import quote


def torrent_file_to_magnet(torrent_file):
    data = open(torrent_file, 'rb').read()
    metadata = bencode.bdecode(data)
    name = metadata['info']['name']
    dn = quote(name)
    info_bts = bencode.bencode(metadata['info'])
    info_hash = hashlib.sha1(info_bts).hexdigest()
    return f'magnet:?xt=urn:btih:{info_hash}&dn={dn}'


if __name__ == '__main__':
    print(torrent_file_to_magnet('1.torrent'))

推荐阅读