[#2716] [Common] Update magnet funcs for tracker tiers tr.x

This commit is contained in:
Calum Lind 2016-11-08 15:44:30 +00:00
commit 112a872bc1

View file

@ -621,9 +621,13 @@ def get_magnet_info(uri):
magnet_scheme = 'magnet:?' magnet_scheme = 'magnet:?'
xt_param = 'xt=urn:btih:' xt_param = 'xt=urn:btih:'
dn_param = 'dn=' dn_param = 'dn='
tr_param = 'tr='
tr0_param = re.compile('^tr.(\d+)=(\S+)')
if uri.startswith(magnet_scheme): if uri.startswith(magnet_scheme):
name = None name = None
info_hash = None info_hash = None
trackers = {}
tier = 0
for param in uri[len(magnet_scheme):].split('&'): for param in uri[len(magnet_scheme):].split('&'):
if param.startswith(xt_param): if param.startswith(xt_param):
xt_hash = param[len(xt_param):] xt_hash = param[len(xt_param):]
@ -639,36 +643,47 @@ def get_magnet_info(uri):
break break
elif param.startswith(dn_param): elif param.startswith(dn_param):
name = unquote_plus(param[len(dn_param):]) name = unquote_plus(param[len(dn_param):])
elif param.startswith(tr_param):
tracker = unquote_plus(param[len(tr_param):])
trackers[tracker] = tier
tier += 1
elif param.startswith('tr.'):
try:
tier, tracker = re.match(tr0_param, param).groups()
trackers[tracker] = tier
except AttributeError:
pass
if info_hash: if info_hash:
if not name: if not name:
name = info_hash name = info_hash
return {'name': name, 'info_hash': info_hash, 'files_tree': ''} return {'name': name, 'info_hash': info_hash, 'files_tree': '', 'trackers': trackers}
return False return False
def create_magnet_uri(infohash, name=None, trackers=None): def create_magnet_uri(infohash, name=None, trackers=None):
""" """Creates a magnet uri
Creates a magnet uri
:param infohash: the info-hash of the torrent Args:
:type infohash: string infohash (str): The info-hash of the torrent.
:param name: the name of the torrent (optional) name (str, optional): The name of the torrent.
:type name: string trackers (dict, optional): The trackers to announce to.
:param trackers: the trackers to announce to (optional)
:type trackers: list of strings
:returns: a magnet uri string Returns:
:rtype: string str: A magnet uri string.
""" """
from base64 import b32encode
uri = 'magnet:?xt=urn:btih:' + b32encode(infohash.decode('hex')) uri = 'magnet:?xt=urn:btih:' + base64.b32encode(infohash.decode('hex'))
if name: if name:
uri = uri + '&dn=' + name uri = uri + '&dn=' + name
if trackers: if trackers:
for t in trackers: try:
uri = uri + '&tr=' + t for tracker in sorted(trackers, key=trackers.__getitem__):
uri = ''.join([uri, '&tr.%d=' % trackers[tracker], tracker])
except TypeError:
for tracker in trackers:
uri = ''.join([uri, '&tr=', tracker])
return uri return uri