Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring code in to a function, and also using torrent name instea…

…d of digest as the default name

  Refactored main code in to the magnet2torrent
    this should make it easier to reuse this code in the future
  Change the default name from the hash of the magnet link to name of the torrent
  • Loading branch information...
commit b19d7e310247b9a34f35373a25d1a548750e44a8 1 parent a4e4999
@scommab scommab authored
Showing with 62 additions and 82 deletions.
  1. +62 −82 Magnet_To_Torrent2.py
View
144 Magnet_To_Torrent2.py
@@ -24,95 +24,75 @@
import shutil, tempfile, os.path as pt, sys, libtorrent as lt, time, hashlib
-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 ""
-if len(sys.argv) < 2:
- showHelp();
- sys.exit(0)
-
-magnet = sys.argv[1]
-digest = hashlib.md5(sys.argv[1]).hexdigest()
-output = pt.abspath(digest + ".torrent" )
-
-if len(sys.argv) == 3:
- if pt.isdir(sys.argv[2]):
- output = pt.abspath(pt.join(sys.argv[2],digest + ".torrent"))
- elif pt.isdir(pt.dirname(pt.abspath(sys.argv[2]))) == True:
- output = pt.abspath(sys.argv[2])
- else:
- showHelp();
- print "Invalid output folder: " + pt.dirname(pt.abspath(sys.argv[2]))
+def magnet2torrent(magnet, output_name = None):
+ digest = hashlib.md5(magnet).hexdigest()
+ 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)
+ return
-tempdir = tempfile.mkdtemp()
-ses = lt.session()
-#ses.listen_on(6881, 6891)
-params = {
+ tempdir = tempfile.mkdtemp()
+ ses = lt.session()
+ params = {
'save_path': tempdir,
- 'duplicate_is_error': True}
-handle = lt.add_magnet_uri(ses, magnet, params)
-#ses.start_dht()
-print 'saving torrent file here : ' + output + " ..."
-while (not handle.has_metadata()):
+ '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:
- time.sleep(.1)
+ time.sleep(1)
except KeyboardInterrupt:
- print "Abrorting..."
- ses.pause()
- print "Cleanup dir " + tempdir
- shutil.rmtree(tempdir)
- sys.exit(0)
-
-torinfo = handle.get_torrent_info()
-
-fs = lt.file_storage()
-for file in torinfo.files():
+ print "Abrorting..."
+ ses.pause()
+ print "Cleanup dir " + tempdir
+ shutil.rmtree(tempdir)
+ return
+ print "done"
+
+ torinfo = handle.get_torrent_info()
+
+ 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))) == True:
+ output = pt.abspath(output_name)
+ print 'saving torrent file here : ' + output + " ..."
+
+ fs = lt.file_storage()
+ for file in torinfo.files():
fs.add_file(file)
-torfile = lt.create_torrent(fs)
-torfile.set_comment(torinfo.comment())
-torfile.set_creator(torinfo.creator())
-
-torcontent = lt.bencode(torfile.generate())
-f = open(output, "wb")
-f.write(lt.bencode(torfile.generate()))
-f.close()
-print 'Saved! Cleaning up dir: ' + tempdir
-shutil.rmtree(tempdir)
-
-#Uncomment to Download the Torrent:
-# print 'starting torrent download...'
+ torfile = lt.create_torrent(fs)
+ torfile.set_comment(torinfo.comment())
+ torfile.set_creator(torinfo.creator())
+
+ torcontent = lt.bencode(torfile.generate())
+ f = open(output, "wb")
+ f.write(lt.bencode(torfile.generate()))
+ f.close()
+ print 'Saved! Cleaning up dir: ' + tempdir
+ shutil.rmtree(tempdir)
+ return output
-# while (handle.status().state != lt.torrent_status.seeding):
-# s = handle.status()
-# time.sleep(55)
-# print 'downloading...'
-
-'''
-Created on Apr 19, 2012
-@author: dan, Faless
-
- GNU GENERAL PUBLIC LICENSE - Version 3
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- http://www.gnu.org/licenses/gpl-3.0.txt
+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 ""
-'''
+if __name__ == "__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)
Please sign in to comment.
Something went wrong with that request. Please try again.