Created
July 11, 2020 06:29
-
-
Save meoww-bot/986745d23f5495328e9361ed0902e860 to your computer and use it in GitHub Desktop.
convert torrent to magnet link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import bencode | |
import hashlib | |
import base64 | |
import urllib | |
def make_magnet_from_file(file) : | |
with open(file,'r') as f: | |
torrent = f.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']} | |
paramstr = urllib.urlencode(params) | |
magneturi = 'magnet:?%s' % paramstr | |
return magneturi | |
if __name__ == "__main__": | |
magnet = make_magnet_from_file(sys.argv[1]) | |
print(magnet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment