亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

使用Python實現BT種子和磁力鏈接的相互轉換

系統 2099 0

bt種子文件轉換為磁力鏈接

BT種子文件相對磁力鏈來說存儲不方便,而且在網站上存放BT文件容易引起版權糾紛,而磁力鏈相對來說則風險小一些。而且很多論壇或者網站限制了文件上傳的類型,分享一個BT種子還需要改文件后綴或者壓縮一次,其他人需要下載時候還要額外多一步下載種子的操作。

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

首先一個方案是使用bencode這個插件,通過pip方式安裝或者自行下載源文件https://pypi.python.org/pypi/bencode/1.0通過python setup.py install方式安裝均可。

相應的將BT種子轉換為磁力鏈代碼為:

            
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

          

還有另外一個效率相對較高,而且更方便的方案是安裝libtorrent,在ubuntu只需要apt-get install python-libtorrent即可對應轉換磁力鏈的代碼為:

            
import libtorrent as bt
info = bt.torrent_info('test.torrent')
print "magnet:?xt=urn:btih:%s&dn=%s" % (info.info_hash(), info.name())


          

轉換磁力鏈接為bt種子文件

下面來看一個反過程,將磁力鏈轉化為種子文件。
1、需要先安裝python-libtorrent包 ,在ubuntu環境下,可以通過以下指令完成安裝:

            
# 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 
            
               [torrent file]

            
          


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产线拍大陆久久尤物 | 中文字幕日韩欧美一区二区三区 | 国产精品久久久久久一区二区三区 | 久久精品九九 | 国产欧美在线观看不卡 | 手机在线国产精品 | 精品一区二区三区免费毛片爱 | 国产在线播放成人免费 | 麻豆91精品91久久久 | 最新午夜韩国理论片77网 | 色婷婷精品综合久久狠狠 | 91一区二区三区四区五区 | 欧美视频一区二区三区 | 精品日韩在线视频一区二区三区 | 亚洲国产欧洲精品路线久久 | 欧美成人午夜免费完成 | 国产福利免费观看 | 成人精品一区久久久久 | 韩日中文字幕 | 伊人俺去久久涩五月综合 | 性做久久久久久免费观看 | www.男人的天堂.com | 国产高清一区二区三区 | 亚洲一区二区福利视频 | 91啦丨国产丨 | 性色网站 | 亚洲日本人成网站在线观看 | 一级片aa| 国产午夜亚洲精品第一区 | 中文字幕不卡在线观看 | 九九99线视频在线观看 | 国产精久久一区二区三区 | 特级毛片免费视频播放 | 一级毛片免费观看不卡的 | 天天干夜夜怕 | 国产日比视频 | 日韩欧美国产卡通动漫 | 亚洲天天做日日做天天看2018 | 欧美日韩国产中文字幕 | 国内精品福利在线视频 | 亚洲欧美一区二区三区综合 |