Home >Backend Development >Python Tutorial >Practical Python implements conversion of BT seeds into magnet links
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 to use python’s bencode module, which is relatively simple to use.
First of all, you need to install this module. The installation command is:
pip install bencode
If you have not installed pip, please Move to "Detailed explanation of pip installation of python package manager"
Practical 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