使用Python實(shí)現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換
bt種子文件轉(zhuǎn)換為磁力鏈接
BT種子文件相對(duì)磁力鏈來說存儲(chǔ)不方便,而且在網(wǎng)站上存放BT文件容易引起版權(quán)糾紛,而磁力鏈相對(duì)來說則風(fēng)險(xiǎn)小一些。而且很多論壇或者網(wǎng)站限制了文件上傳的類型,分享一個(gè)BT種子還需要改文件后綴或者壓縮一次,其他人需要下載時(shí)候還要額外多一步下載種子的操作。
所以將BT種子轉(zhuǎn)換為占用空間更小,分享更方便的磁力鏈還是有挺大好處的。
首先一個(gè)方案是使用bencode這個(gè)插件,通過pip方式安裝或者自行下載源文件https://pypi.python.org/pypi/bencode/1.0通過python setup.py install方式安裝均可。
相應(yīng)的將BT種子轉(zhuǎn)換為磁力鏈代碼為:
import bencode, hashlib, base64, urllib torrent = open('ubuntu-12.04.2-server-amd64.iso.torrent', 'rb').read() metadata = bencode.bdecode(torrent) hashcontents = bencode.bencode(metadata['info']) digest = hashlib.sha1(hashcontents).digest() b32hash = base64.b32encode(digest) params = {'xt': 'urn:btih:%s' % b32hash, 'dn': metadata['info']['name'], 'tr': metadata['announce'], 'xl': metadata['info']['length']} paramstr = urllib.urlencode(params) magneturi = 'magnet:?%s' % paramstr print magneturi
還有另外一個(gè)效率相對(duì)較高,而且更方便的方案是安裝libtorrent,在ubuntu只需要apt-get install python-libtorrent即可對(duì)應(yīng)轉(zhuǎn)換磁力鏈的代碼為:
import libtorrent as bt info = bt.torrent_info('test.torrent') print "magnet:?xt=urn:btih:%s&dn=%s" % (info.info_hash(), info.name())
轉(zhuǎn)換磁力鏈接為bt種子文件
下面來看一個(gè)反過程,將磁力鏈轉(zhuǎn)化為種子文件。
1、需要先安裝python-libtorrent包 ,在ubuntu環(huán)境下,可以通過以下指令完成安裝:
# sudo apt-get install python-libtorrent
2、代碼如下:
#!/usr/bin/env python import shutil import tempfile import os.path as pt import sys import libtorrent as lt from time import sleep def magnet2torrent(magnet, output_name=None): if output_name and \ not pt.isdir(output_name) and \ not pt.isdir(pt.dirname(pt.abspath(output_name))): print("Invalid output folder: " + pt.dirname(pt.abspath(output_name))) print("") sys.exit(0) tempdir = tempfile.mkdtemp() ses = lt.session() params = { 'save_path': tempdir, 'duplicate_is_error': True, 'storage_mode': lt.storage_mode_t(2), 'paused': False, 'auto_managed': True, 'duplicate_is_error': True } handle = lt.add_magnet_uri(ses, magnet, params) print("Downloading Metadata (this may take a while)") while (not handle.has_metadata()): try: sleep(1) except KeyboardInterrupt: print("Aborting...") ses.pause() print("Cleanup dir " + tempdir) shutil.rmtree(tempdir) sys.exit(0) ses.pause() print("Done") torinfo = handle.get_torrent_info() torfile = lt.create_torrent(torinfo) output = pt.abspath(torinfo.name() + ".torrent") if output_name: if pt.isdir(output_name): output = pt.abspath(pt.join( output_name, torinfo.name() + ".torrent")) elif pt.isdir(pt.dirname(pt.abspath(output_name))): output = pt.abspath(output_name) print("Saving torrent file here : " + output + " ...") torcontent = lt.bencode(torfile.generate()) f = open(output, "wb") f.write(lt.bencode(torfile.generate())) f.close() print("Saved! Cleaning up dir: " + tempdir) ses.remove_torrent(handle) shutil.rmtree(tempdir) return output def showHelp(): print("") print("USAGE: " + pt.basename(sys.argv[0]) + " MAGNET [OUTPUT]") print(" MAGNET\t- the magnet url") print(" OUTPUT\t- the output torrent file name") print("") def main(): if len(sys.argv) < 2: showHelp() sys.exit(0) magnet = sys.argv[1] output_name = None if len(sys.argv) >= 3: output_name = sys.argv[2] magnet2torrent(magnet, output_name) if __name__ == "__main__": main()
3、用法如下
# python Magnet_To_Torrent2.py <magnet link> [torrent file]
相關(guān)文章
Python實(shí)現(xiàn)的一個(gè)自動(dòng)售飲料程序代碼分享
這篇文章主要介紹了Python實(shí)現(xiàn)的一個(gè)自動(dòng)售飲料程序代碼分享,就是用python實(shí)現(xiàn)的生活中一種投幣式自動(dòng)售飲料機(jī)的內(nèi)部程序判斷代碼,需要的朋友可以參考下2014-08-08使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法
今天小編就為大家分享一篇使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python命名空間(namespace)簡(jiǎn)單介紹
這篇文章主要介紹了python命名空間(namespace)簡(jiǎn)單介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python開發(fā)之thread實(shí)現(xiàn)布朗運(yùn)動(dòng)的方法
這篇文章主要介紹了python開發(fā)之thread實(shí)現(xiàn)布朗運(yùn)動(dòng)的方法,實(shí)例分析了Python基于多線程實(shí)現(xiàn)繪圖的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11對(duì)python同一個(gè)文件夾里面不同.py文件的交叉引用方法詳解
今天小編就為大家分享一篇對(duì)python同一個(gè)文件夾里面不同.py文件的交叉引用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12