實戰Python實現BT種子轉化為磁力連結

2019-10-30     科技i關注

經常看電影的朋友肯定對BT種子並不陌生,但是BT種子文件相對磁力鏈來說存儲不方便,而且在網站上存放BT文件容易引起版權糾紛,而磁力鏈相對來說則風險小一些。

將BT種子轉換為占用空間更小,分享更方便的磁力鏈還是有挺大好處的。

今天咱們來看下如何將種子轉換成磁力連結,方案是:利用python的bencode模塊,用起來比較簡單

首先要安裝這個模塊,安裝命令:

pip install bencode

如果沒有安裝pip,請移步《詳解python包管理器pip安裝》

實戰代碼

安裝完成後,我們來看下代碼:

系統環境:Linux

Python環境:Python2.7

請注意python版本

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

如何使用?

命令:

python bt2url.py test.torrent

結果 :

magnet:?xt=urn:btih:MWXFHXOGE2UMR7WBFZYEJPM3LF2VIHNH

更多技巧請《轉發 + 關注》哦!

文章來源: https://twgreatdaily.com/zh-cn/WyDnGm4BMH2_cNUg4jNl.html