国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:02:47
字體:
來源:轉載
供稿:網友

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

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

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

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

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

import bencode, hashlib, base64, urllibtorrent = 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' % paramstrprint magneturi

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

import libtorrent as btinfo = 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 pythonimport shutilimport tempfileimport os.path as ptimport sysimport libtorrent as ltfrom time import sleepdef 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 outputdef 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]

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇文区| 宁晋县| 定结县| 贞丰县| 洛阳市| 闽清县| 龙江县| 兴宁市| 连山| 新昌县| 康乐县| 彩票| 板桥市| 屯门区| 平塘县| 贵溪市| 丹东市| 鹤峰县| 蛟河市| 新平| 黄平县| 怀安县| 厦门市| 保康县| 陇西县| 抚顺县| 通山县| 石泉县| 高陵县| 元阳县| 贺州市| 蕉岭县| 新巴尔虎右旗| 和顺县| 财经| 东方市| 鹤庆县| 吉木萨尔县| 蛟河市| 本溪| 荃湾区|