convert bt seed file to magnetic link
The BT seed file is not easy to store relative to the magnetic chain, and storing BT files on the website can easily cause copyright disputes, while the magnetic chain is relatively less risky. and many forums or websites restrict the type of file upload, share a BT seed also need to change the file suffix or compression once, others need to download time to download the operation of the seed.
So it is good to convert BT seeds to a smaller footprint and to share a more convenient magnetic chain.
The first option is to use the Bencode plug-in, either by PIP installation or by downloading the source files https://pypi.python.org/pypi/bencode/1.0 the Python setup.py install mode.
The corresponding conversion of the BT seed to the magnetic chain code is:
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
There is another relatively high efficiency, and more convenient solution is to install libtorrent, in Ubuntu only need to apt-get install python-libtorrent can correspond to the code to convert the magnetic chain:
Import libtorrent as Btinfo = Bt.torrent_info (' test.torrent ') print "magnet:?xt=urn:btih:%s&dn=%s"% (info.info_ Hash (), Info.name ())
Convert a magnetic link to a BT seed file
Here is an inverse process that transforms the magnetic chain into a seed file.
1, need to install Python-libtorrent package, in the Ubuntu environment, you can complete the installation by the following instructions:
# sudo apt-get install python-libtorrent
2, the code is as follows:
#!/usr/bin/env pythonimport shutilimport tempfileimport os.path as Ptimport sysimport libtorrent as ltfrom time import SLE Epdef magnet2torrent (Magnet, Output_name=none): if Output_name and \ Not Pt.isdir (output_name) and \ Not PT.ISD IR (Pt.dirname (Pt.abspath (Output_name))): Print ("Invalid output folder:" + Pt.dirname (Pt.abspath (output_name))) Prin T ("") 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_er Ror ': True} handle = Lt.add_magnet_uri (SES, magnet, params) print ("Downloading Metadata (this could take a while)") whi Le (not Handle.has_metadata ()): Try:sleep (1) except Keyboardinterrupt:print ("aborting ...") Ses.paus E () 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.ISD IR (output_name): output = Pt.abspath (Pt.join (Output_name, Torinfo.name () + ". Torrent")) elif Pt.isdir (pt.d Irname (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.clo SE () 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. Use as follows
# python magnet_to_torrent2.py
[torrent file]