Python infrastructure projects combat: to achieve BT seeds into a magnetic link

Friends often watch movies certainly no stranger to BT seeds, but BT seed file relative to magnetic storage chain, inconvenient, and BT files stored on the site easily lead to copyright disputes, and the magnetic chain risk is relatively smaller.

BT seeds will be converted into a space smaller, more convenient sharing of magnetic chain still has big benefits.

Today we look at how to convert the seeds into a magnetic link, the program is: the use of bencode python module, with a relatively simple

You must first install the module installation command:

pip install bencode

If you do not pip (installed you can skip), see here:

pip installation method.

Method One: Script Installation

$ wget https://bootstrap.pypa.io/get-pip.py
$ [sudo] python get-pip.py

Method two: source installation:

$ 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

But the installation process error may occur:

An error occurred while trying to run get-pip.py. Make sure you have setuptools or distribute installed.

This error, stating that we must first install setuptools

setuptools installation:

wget -q http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py

After installing setuptools, again source installation just fine.

After installation is complete, we look at the code:

System Environment: Linux

Python environment: Python2.7

Please note that the python version

bt2url.py

Combat Code:

#! /usr/local/bin/python
# @desc python通过BT种子生成磁力链接 
# @date 2015/11/10
# @author pythontab.com
import bencode
import sys
import hashlib
import base64
import urllib
#获取参数
torrentName = sys.argv[1]
#读取种子文件
torrent = open(torrentName, 'rb').read()
#计算meta数据
metadata = bencode.bdecode(torrent)
hashcontents = bencode.bencode(metadata['info'])
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest)
#打印
print 'magnet:?xt=urn:btih:%s' % b32hash

how to use?

command:

python bt2url.py test.torrent

result:

magnet:?xt=urn:btih:MWXFHXOGE2UMR7WBFZYEJPM3LF2VIHNH

As a someone who has, I have to tell you to ask the older cattle is really important, so that you can take a lot less detours, do not be afraid shame, no face, the face value of some money? Learn a real skill is most important. No skill called really no face. python technology sharing , so that your future is no longer confused.

Recommended reading: zero-based learning Python, sooner understand this, the sooner you find a good job!

Guess you like

Origin blog.csdn.net/weichen090909/article/details/90647717
Recommended