Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. '''
  3. Created on Apr 19, 2012
  4. @author: dan, Faless
  5.  
  6.    GNU GENERAL PUBLIC LICENSE - Version 3
  7.  
  8.    This program is free software: you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation, either version 3 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21.    http://www.gnu.org/licenses/gpl-3.0.txt
  22.  
  23. '''
  24.  
  25. import shutil
  26. import tempfile
  27. import os.path as pt
  28. import sys
  29. import libtorrent as lt
  30. from time import sleep
  31.  
  32. def magnet2torrent(magnet, output_name=None):
  33.     if output_name and \
  34.             not pt.isdir(output_name) and \
  35.             not pt.isdir(pt.dirname(pt.abspath(output_name))):
  36.         print("Invalid output folder: " + pt.dirname(pt.abspath(output_name)))
  37.         print("")
  38.         sys.exit(0)
  39.  
  40.     tempdir = tempfile.mkdtemp()
  41.     ses = lt.session()
  42.     params = {
  43.         'save_path': tempdir,
  44.         'duplicate_is_error': True,
  45.         'storage_mode': lt.storage_mode_t(2),
  46.         'paused': False,
  47.         'auto_managed': True,
  48.         'duplicate_is_error': True
  49.     }
  50.     handle = lt.add_magnet_uri(ses, magnet, params)
  51.  
  52.     print("Downloading Metadata (this may take a while)")
  53.     while (not handle.has_metadata()):
  54.         try:
  55.             sleep(1)
  56.         except KeyboardInterrupt:
  57.             print("Aborting...")
  58.             ses.pause()
  59.             print("Cleanup dir " + tempdir)
  60.             shutil.rmtree(tempdir)
  61.             sys.exit(0)
  62.     ses.pause()
  63.     print("Done")
  64.  
  65.     torinfo = handle.get_torrent_info()
  66.     torfile = lt.create_torrent(torinfo)
  67.  
  68.     output = pt.abspath(torinfo.name() + ".torrent")
  69.  
  70.     if output_name:
  71.         if pt.isdir(output_name):
  72.             output = pt.abspath(pt.join(
  73.                 output_name, torinfo.name() + ".torrent"))
  74.         elif pt.isdir(pt.dirname(pt.abspath(output_name))):
  75.             output = pt.abspath(output_name)
  76.  
  77.     print("Saving torrent file here : " + output + " ...")
  78.     torcontent = lt.bencode(torfile.generate())
  79.     f = open(output, "wb")
  80.     f.write(lt.bencode(torfile.generate()))
  81.     f.close()
  82.     print("Saved! Cleaning up dir: " + tempdir)
  83.     ses.remove_torrent(handle)
  84.     shutil.rmtree(tempdir)
  85.  
  86.     return output
  87.  
  88.  
  89. def showHelp():
  90.     print("")
  91.     print("USAGE: " + pt.basename(sys.argv[0]) + " MAGNET [OUTPUT]")
  92.     print("  MAGNET\t- the magnet url")
  93.     print("  OUTPUT\t- the output torrent file name")
  94.     print("")
  95.  
  96.  
  97. def main():
  98.     if len(sys.argv) < 2:
  99.         showHelp()
  100.         sys.exit(0)
  101.  
  102.     magnet = sys.argv[1]
  103.     output_name = None
  104.  
  105.     if len(sys.argv) >= 3:
  106.         output_name = sys.argv[2]
  107.  
  108.     magnet2torrent(magnet, output_name)
  109.  
  110.  
  111. if __name__ == "__main__":
  112.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement