Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created May 5, 2014 08:21
Show Gist options
  • Save lsongdev/644dfee919827eb6b321 to your computer and use it in GitHub Desktop.
Save lsongdev/644dfee919827eb6b321 to your computer and use it in GitHub Desktop.
import sys
import bencode
import hashlib
import base64
import urllib
torrent_file = sys.argv[1]
torrent = open(torrent_file, 'r').read()
metadata = bencode.bdecode(torrent)
hashcontents = bencode.bencode(metadata['info'])
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest)
print "magnet:?xt=urn:btih:%s" % b32hash
params = {
'xt': 'urn:btih:%s' % b32hash,
'dn': metadata['info']['name'],
'tr': metadata['announce'],
'xl': metadata['info']['length']}
paramstr = urllib.urlencode(params)
magneturi = 'magnet:?%s' % paramstr
print magneturi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment