Python实现BT种子转化为磁力链接

欢迎点击右上角关注小编,除了分享技术文章之外还有很多福利,私信学习资料可以领取包括不限于Python实战演练、大航海计划、BAT内推、学习资料等 。
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些 。

Python实现BT种子转化为磁力链接

文章插图
将BT种子转换为占用空间更小,分享更方便的磁力链还是有挺大好处的 。
今天咱们来看下如何将种子转换成磁力链接,方案是:利用python的bencode模块 , 用起来比较简单
首先要安装这个模块 , 安装命令:
pip install bencode 如果没有安装pip(安装了可以略过),看这里:
pip的安装方法 。
方法一:脚本安装
$ wget https://bootstrap.pypa.io/get-pip.py$ [sudo] python get-pip.py 方法二:源码安装:
$ curl -O https://pypi.python.org/packages/source/p/pip/pip-X.X.tar.gz$ tar xvfz pip-X.X.tar.gz$ cd pip-X.X$ python setup.py install 但是安装过程可能会出现错误:
An error occurred while trying to run get-pip.py. Make sure you have setuptools or distribute installed.
出现这个错误,说明首先要安装setuptools
setuptools 安装:
wget -q ***munity.com/dist/ez_setup.pypython ez_setup.py 安装完setuptools后,再次源码安装就好了 。
安装完成后,我们来看下代码:
系统环境:Linux
Python环境:Python2.7
【Python实现BT种子转化为磁力链接】 请注意python版本
bt2url.py
实战代码:
#! /usr/local/bin/python# @desc python通过BT种子生成磁力链接 # @date 2015/11/10# @author pythontab.comimport bencodeimport sysimport hashlibimport base64import urllib#获取参数torrentName = sys.argv[1]#读取种子文件torrent = open(torrentName, 'rb').read()#计算meta数据metadata = http://www.chy56.com/show/bencode.bdecode(torrent)hashcOntents= bencode.bencode(metadata['info'])digest = hashlib.sha1(hashcontents).digest()b32hash = base64.b32encode(digest)#打印print 'magnet:?xt=urn:btih:%s' % b32hash 如何使用?
命令:
python bt2url.py test.torrent 结果:
magnet:?xt=urn:btih:MWXFHXOGE2UMR7WBFZYEJPM3LF2VIHNH
Python实现BT种子转化为磁力链接

文章插图