params = {'xt': 'urn:btih:%s' % b32hash, 'dn': metadata['info']['name'],
    'tr': metadata['announce'], 'xl': metadata['info']['length']}
import urllib.request, urllib.parse, urllib.error
paramstr = urllib.parse.urlencode(params)
magneturi = 'magnet:?%s' % paramstr
magneturi
Given a .torrent file how do I generate a magnet link in python?