首頁  >  文章  >  後端開發  >  實戰Python實現BT種子轉化為磁力鏈接

實戰Python實現BT種子轉化為磁力鏈接

高洛峰
高洛峰原創
2016-10-17 16:54:272330瀏覽

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

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

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

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

pip install bencode

   

,請移步《詳解python套件管理器pip安裝》

實戰程式碼

安裝完成後,我們來看看程式碼:

系統環境:Linux

Python環境:Python2.7

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

   


   


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn