Python-Bot

python爬虫,php web,微信公众号

python实现bt种子 torrent转magnet

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%BDwww.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种子的文件格式 文章
http://www.cnblogs.com/DxSoft/archive/2012/02/11/2346314.html
python2.7 的bt转磁链 代码
http://www.cnblogs.com/qytang/p/5534491.html
 
 
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
 
 
 

 

posted on 2017-09-21 07:06  尼莫船长  阅读(4487)  评论(0编辑  收藏  举报

导航