Home >Backend Development >Python Tutorial >Practical Python implements conversion of BT seeds into magnet links

Practical Python implements conversion of BT seeds into magnet links

高洛峰
高洛峰Original
2016-10-17 16:54:272404browse

Friends who often watch movies must be familiar with BT seeds, but BT seed files are inconvenient to store compared to magnet links, and storing BT files on a website can easily cause copyright disputes, while magnet links are relatively less risky.

Converting BT seeds to magnet links that take up less space and are more convenient for sharing has great benefits.

Today we will take a look at how to convert seeds into magnet links. The solution is: use python’s bencode module, which is relatively simple to use.

First of all, you need to install this module. The installation command:

pip install bencode

If pip is not installed , please move to "Detailed explanation of python package manager pip installation"

Actual code

After the installation is completed, let's take a look at the code:

System environment: Linux

Python environment: Python2.7

Please pay attention to the python version

bt2url.py

#! /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



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn