最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python實(shí)現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換

 更新時(shí)間:2015年11月09日 14:46:35   投稿:goldensun  
這篇文章主要介紹了使用Python實(shí)現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換的方法,有時(shí)比如迅雷無(wú)法加載磁力鏈接或者無(wú)法上傳附件分享時(shí)可以用到,需要的朋友可以參考下

bt種子文件轉(zhuǎn)換為磁力鏈接

BT種子文件相對(duì)磁力鏈來(lái)說(shuō)存儲(chǔ)不方便,而且在網(wǎng)站上存放BT文件容易引起版權(quán)糾紛,而磁力鏈相對(duì)來(lái)說(shuō)則風(fēng)險(xiǎn)小一些。而且很多論壇或者網(wǎng)站限制了文件上傳的類(lèi)型,分享一個(gè)BT種子還需要改文件后綴或者壓縮一次,其他人需要下載時(shí)候還要額外多一步下載種子的操作。

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

首先一個(gè)方案是使用bencode這個(gè)插件,通過(guò)pip方式安裝或者自行下載源文件https://pypi.python.org/pypi/bencode/1.0通過(guò)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種子文件

下面來(lái)看一個(gè)反過(guò)程,將磁力鏈轉(zhuǎn)化為種子文件。
1、需要先安裝python-libtorrent包 ,在ubuntu環(huán)境下,可以通過(guò)以下指令完成安裝:

# 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)文章

  • pycharm 快速解決python代碼沖突的問(wèn)題

    pycharm 快速解決python代碼沖突的問(wèn)題

    這篇文章主要介紹了pycharm 快速解決python代碼沖突的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • 如何在Python項(xiàng)目中做多環(huán)境配置(環(huán)境變量使用.env文件)

    如何在Python項(xiàng)目中做多環(huán)境配置(環(huán)境變量使用.env文件)

    實(shí)際工程開(kāi)發(fā)中常常會(huì)對(duì)開(kāi)發(fā)、測(cè)試和生產(chǎn)等不同環(huán)境配置不同的數(shù)據(jù)庫(kù)環(huán)境,傳統(tǒng)方式可以通過(guò)添加不同環(huán)境的配置文件達(dá)到部署時(shí)的動(dòng)態(tài)切換的效果,這篇文章主要給大家介紹了關(guān)于如何在Python項(xiàng)目中做多環(huán)境配置的相關(guān)資料,環(huán)境變量使用.env文件,需要的朋友可以參考下
    2024-06-06
  • Python中Socket編程底層原理解析與應(yīng)用實(shí)戰(zhàn)

    Python中Socket編程底層原理解析與應(yīng)用實(shí)戰(zhàn)

    Socket編程是網(wǎng)絡(luò)通信的基礎(chǔ),Python通過(guò)內(nèi)置的socket模塊提供了強(qiáng)大的網(wǎng)絡(luò)編程接口,本文將結(jié)合實(shí)際案例,詳細(xì)介紹Python中Socket編程的基本概念、常用方法和實(shí)際應(yīng)用,需要的朋友可以參考下
    2024-08-08
  • Python圖片處理之圖片采樣處理詳解

    Python圖片處理之圖片采樣處理詳解

    這篇文章將詳細(xì)為大家講解圖像采樣處理,包括原理知識(shí)、代碼實(shí)現(xiàn)和局部馬賽克處理。文中的示例代碼講解詳細(xì),感興趣的可以跟隨小編一起動(dòng)手試一試
    2022-02-02
  • Python while 循環(huán)使用的簡(jiǎn)單實(shí)例

    Python while 循環(huán)使用的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇Python while 循環(huán)使用的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • 淺談python中真正關(guān)閉socket的方法

    淺談python中真正關(guān)閉socket的方法

    今天小編就為大家分享一篇淺談python中真正關(guān)閉socket的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python selenium 實(shí)例之通過(guò) selenium 查詢(xún)禪道是否有任務(wù)或者BUG

    Python selenium 實(shí)例之通過(guò) selenium 查詢(xún)禪道是否有任務(wù)或者BUG

    這篇文章主要介紹了Python selenium 實(shí)例之通過(guò) selenium 查詢(xún)禪道是否有任務(wù)或者BUG的相關(guān)資料,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 將Django使用的數(shù)據(jù)庫(kù)從MySQL遷移到PostgreSQL的教程

    將Django使用的數(shù)據(jù)庫(kù)從MySQL遷移到PostgreSQL的教程

    這篇文章主要介紹了將Django使用的數(shù)據(jù)庫(kù)從MySQL遷移到PostgreSQL的教程,同時(shí)提到了一些注意事項(xiàng),需要的朋友可以參考下
    2015-04-04
  • Python中的錯(cuò)誤和異常處理簡(jiǎn)單操作示例【try-except用法】

    Python中的錯(cuò)誤和異常處理簡(jiǎn)單操作示例【try-except用法】

    這篇文章主要介紹了Python中的錯(cuò)誤和異常處理簡(jiǎn)單操作,結(jié)合實(shí)例形式分析了Python中try except在錯(cuò)誤與異常處理中的用法,需要的朋友可以參考下
    2017-07-07
  • 使用Python實(shí)現(xiàn)一鍵隱藏屏幕并鎖定輸入

    使用Python實(shí)現(xiàn)一鍵隱藏屏幕并鎖定輸入

    本文主要介紹了使用 Python 編寫(xiě)一個(gè)一鍵隱藏屏幕并鎖定輸入的黑科技程序,能夠在指定熱鍵觸發(fā)后立即遮擋屏幕,并禁止一切鍵盤(pán)鼠標(biāo)輸入,這樣就再也不用擔(dān)心自己的屏幕被人偷看啦
    2025-04-04

最新評(píng)論

来安县| 阿荣旗| 永宁县| 墨玉县| 封开县| 罗江县| 龙井市| 邢台县| 荆门市| 马边| 荣成市| 漾濞| 田阳县| 乌兰察布市| 辽宁省| 安陆市| 洛扎县| 河南省| 台湾省| 马边| 饶河县| 德化县| 南雄市| 察雅县| 淮南市| 禹城市| 呈贡县| 琼中| 行唐县| 平顺县| 丰台区| 互助| 吴忠市| 汉阴县| 丰原市| 洛隆县| 双柏县| 石阡县| 大渡口区| 哈巴河县| 宁河县|