Magnet_To_Torrent2.py 3.0 KB

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