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

首頁 > 編程 > Python > 正文

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

2020-01-04 17:56:23
字體:
來源:轉載
供稿:網(wǎng)友

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

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

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

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

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

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

 

  1. import bencode, hashlib, base64, urllib 
  2. torrent = open('ubuntu-12.04.2-server-amd64.iso.torrent''rb').read() 
  3. metadata = bencode.bdecode(torrent) 
  4. hashcontents = bencode.bencode(metadata['info']) 
  5. digest = hashlib.sha1(hashcontents).digest() 
  6. b32hash = base64.b32encode(digest) 
  7. params = {'xt''urn:btih:%s' % b32hash, 
  8. 'dn': metadata['info']['name'], 
  9. 'tr': metadata['announce'], 
  10. 'xl': metadata['info']['length']} 
  11. paramstr = urllib.urlencode(params) 
  12. magneturi = 'magnet:?%s' % paramstr 
  13. print magneturi 

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

 

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

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

下面來看一個反過程,將磁力鏈轉化為種子文件。

1、需要先安裝python-libtorrent包 ,在ubuntu環(huán)境下,可以通過以下指令完成安裝:

 

 
  1. # sudo apt-get install python-libtorrent 

2、代碼如下:

 

 
  1. #!/usr/bin/env python 
  2. import shutil 
  3. import tempfile 
  4. import os.path as pt 
  5. import sys 
  6. import libtorrent as lt 
  7. from time import sleep 
  8. def magnet2torrent(magnet, output_name=None): 
  9. if output_name and / 
  10. not pt.isdir(output_name) and / 
  11. not pt.isdir(pt.dirname(pt.abspath(output_name))): 
  12. print("Invalid output folder: " + pt.dirname(pt.abspath(output_name))) 
  13. print(""
  14. sys.exit(0) 
  15. tempdir = tempfile.mkdtemp() 
  16. ses = lt.session() 
  17. params = { 
  18. 'save_path': tempdir, 
  19. 'duplicate_is_error': True, 
  20. 'storage_mode': lt.storage_mode_t(2), 
  21. 'paused': False, 
  22. 'auto_managed': True, 
  23. 'duplicate_is_error': True 
  24. handle = lt.add_magnet_uri(ses, magnet, params) 
  25. print("Downloading Metadata (this may take a while)"
  26. while (not handle.has_metadata()): 
  27. try
  28. sleep(1) 
  29. except KeyboardInterrupt: 
  30. print("Aborting..."
  31. ses.pause() 
  32. print("Cleanup dir " + tempdir) 
  33. shutil.rmtree(tempdir) 
  34. sys.exit(0) 
  35. ses.pause() 
  36. print("Done"
  37. torinfo = handle.get_torrent_info() 
  38. torfile = lt.create_torrent(torinfo) 
  39. output = pt.abspath(torinfo.name() + ".torrent"
  40. if output_name: 
  41. if pt.isdir(output_name): 
  42. output = pt.abspath(pt.join( 
  43. output_name, torinfo.name() + ".torrent")) 
  44. elif pt.isdir(pt.dirname(pt.abspath(output_name))): 
  45. output = pt.abspath(output_name) 
  46. print("Saving torrent file here : " + output + " ..."
  47. torcontent = lt.bencode(torfile.generate()) 
  48. f = open(output, "wb"
  49. f.write(lt.bencode(torfile.generate())) 
  50. f.close() 
  51. print("Saved! Cleaning up dir: " + tempdir) 
  52. ses.remove_torrent(handle) 
  53. shutil.rmtree(tempdir) 
  54. return output 
  55. def showHelp(): 
  56. print(""
  57. print("USAGE: " + pt.basename(sys.argv[0]) + " MAGNET [OUTPUT]"
  58. print(" MAGNET/t- the magnet url"
  59. print(" OUTPUT/t- the output torrent file name"
  60. print(""
  61. def main(): 
  62. if len(sys.argv) < 2: 
  63. showHelp() 
  64. sys.exit(0) 
  65. magnet = sys.argv[1] 
  66. output_name = None 
  67. if len(sys.argv) >= 3: 
  68. output_name = sys.argv[2] 
  69. magnet2torrent(magnet, output_name) 
  70. if __name__ == "__main__"
  71. main() 

3、用法如下

  1. # python Magnet_To_Torrent2.py <magnet link> [torrent file] 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永年县| 施秉县| 鹰潭市| 梅河口市| 闸北区| 南京市| 平乡县| 桑植县| 铜鼓县| 长宁县| 武义县| 衢州市| 富平县| 冕宁县| 鲁山县| 固始县| 延吉市| 通州区| 屯昌县| 汶上县| 塘沽区| 祁门县| 麻栗坡县| 偃师市| 曲松县| 自贡市| 稷山县| 余姚市| 云霄县| 苏州市| 四子王旗| 乳山市| 疏附县| 海原县| 鹤山市| 南雄市| 中江县| 乐山市| 浦城县| 宁波市| 邯郸县|