Python實現bt轉磁鏈
參考前人資料主要兩種方式
1,利用python的bencode模塊
2,安裝libtorrent模塊
嘗試過兩種方法特記錄
環境:Windows系統 python 3
bencode模塊有很多,最早的一個不支持 python3,網上的實例代碼就是使用2.7 所以 結果是不行。

選了一個bencoder.pyx 1.2.0 不知道為啥選這個
作者的 github:
https://github.com/whtsky/bencoder.pyx
可以參考一下他的示例
pip 安裝,上代碼
from bencoder import bencode, bdecode import hashlib import base64 with open("加勒比海盜5:死無對證.2017.BD720P.X264.AAC.English&Mandarin.CHS-ENG.laziku.mp4.torrent", mode='rb',) as f : torrent = bdecode(f.read()) # print(bencode(torrent[b'info'])) # print(torrent) # torrent = f.read() digest = hashlib.sha1(bencode(torrent[b'info'])).digest() # torrent[b'info'] 里面包含了種子文件的關鍵信息 對這部分進行哈希處理 b32hash = base64.b32encode(digest) print('magnet:?xt=urn:btih:%s' % b32hash.decode()) #結果 magnet:?xt=urn:btih:7GI3YCSXAKTGIYBJYKVUVIKELGDBXLDF
我使用 bt轉磁鏈網站 得到結果
magnet:?xt=urn:btih:F991BC0A5702A6646029C2AB4AA14459861BAC65&dn=%5B%E7%94%B5%E5%BD%B1%E4%B8%8B%E8%BD%BD
www.laziku.com%5D%E5%8A%A0%E5%8B%92%E6%AF%94%E6%B5%B7%E7%9B%975%EF%BC%9A%E6%AD%BB%E6%97%A0%E5%AF%B9%E8%AF%81.BD%E9%AB%98%E6%B8%851280%E5%9B%BD%E8%8B%B1%E5%8F%8C%E8%AF%AD.%E4%B8%AD%E8%8B%B1%E5%8F%8C%E5%AD%97.mp4
用迅雷試一下 都能抓到種子
參考一下 關於bt種子的文件格式 文章
python2.7 的bt轉磁鏈 代碼
libtorrent 的處理代碼更加簡單,里面封裝好了方法 直接調用就可以
不 過我在win環境下 這個模塊安裝出錯,
摘記一下代碼

參考文章:http://www.au92.com/archives/P-y-t-h-o-n-jiang-B-T-zhong-zi-wen-jian-zhuan-huan-wei-ci-li-lian-de-liang-zhong-fang-fa.html