12345678910111213141516171819202122232425262728 |
- #!/usr/bin/env python3
- # adapted from https://github.com/DanySK/torrent2magnet
- import sys
- import hashlib
- import base64
- from fastbencode import bencode, bdecode
- def make_magnet_from_file(file):
- with open(file, "rb") as f:
- data = f.read()
- metadata = bdecode(data)
- subj = metadata[b'info']
- hashcontents = bencode(subj)
- digest = hashlib.sha1(hashcontents).digest()
- b32hash = base64.b32encode(digest).decode()
- return 'magnet:?'\
- + 'xt=urn:btih:' + b32hash\
- + '&dn=' + metadata[b'info'][b'name'].decode()\
- + '&tr=' + metadata[b'announce'].decode()\
- + '&xl=' + str(metadata[b'info'][b'length'])
- if __name__ == "__main__":
- magnet = make_magnet_from_file(sys.argv[1])
- print(magnet)
|