激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 使用Python實現BT種子和磁力鏈接的相互轉換

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

2020-08-02 10:19腳本之家 Python

這篇文章主要介紹了使用Python實現BT種子和磁力鏈接的相互轉換的方法,有時比如迅雷無法加載磁力鏈接或者無法上傳附件分享時可以用到,需要的朋友可以參考下

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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即可對應轉換磁力鏈的代碼為:

?
1
2
3
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環境下,可以通過以下指令完成安裝:

?
1
# sudo apt-get install python-libtorrent

2、代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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、用法如下

?
1
# python Magnet_To_Torrent2.py <magnet link> [torrent file]

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久欧美亚洲另类专区91大神 | 九九热在线视频观看 | 激情综合视频 | 免费国产精品视频 | julieann艳星激情办公室 | 国产剧情在线观看一区二区 | 久久国产在线观看 | 国产一区二区在线免费 | 免费黄色小网站 | 黑人日比| 亚洲一区二区三区四区精品 | 欧美a级毛片 | 成人资源在线观看 | 精品久久www| 精品亚洲视频在线 | 91精品国 | 九九综合视频 | www.99re14.com | 午夜精品久久久久久久爽 | 欧美a视频 | 国产一区二区高清在线 | 国产成人综合在线观看 | 日本韩国欧美一级片 | 亚洲视频精选 | 深夜激情视频 | 亚洲视屏在线 | 免费观看一级黄色片 | 91九色福利| 全黄性色大片 | 精品久久久久久久久亚洲 | 欧美精品免费一区二区三区 | 一级免费看片 | www国产成人免费观看视频 | 亚洲成人第一区 | 久久精品久久久久 | 性生活视频软件 | 欧美一级一区二区三区 | 国内精品久久久久久久影视红豆 | 空姐一级毛片 | 视频www | 成人精品视频在线 |