Changeset 112a87


Ignore:
Timestamp:
03/17/2017 10:26:17 AM (8 years ago)
Author:
Calum Lind <calumlind+deluge@gmail.com>
Branches:
2.0.x, develop, master
Children:
9e303b
Parents:
642433
git-author:
Calum Lind <calumlind+deluge@gmail.com> (11/08/2016 03:44:30 PM)
git-committer:
Calum Lind <calumlind+deluge@gmail.com> (03/17/2017 10:26:17 AM)
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • deluge/common.py

    r642433 r112a87  
    622622    xt_param = 'xt=urn:btih:'
    623623    dn_param = 'dn='
     624    tr_param = 'tr='
     625    tr0_param = re.compile('^tr.(\d+)=(\S+)')
    624626    if uri.startswith(magnet_scheme):
    625627        name = None
    626628        info_hash = None
     629        trackers = {}
     630        tier = 0
    627631        for param in uri[len(magnet_scheme):].split('&'):
    628632            if param.startswith(xt_param):
     
    640644            elif param.startswith(dn_param):
    641645                name = unquote_plus(param[len(dn_param):])
     646            elif param.startswith(tr_param):
     647                tracker = unquote_plus(param[len(tr_param):])
     648                trackers[tracker] = tier
     649                tier += 1
     650            elif param.startswith('tr.'):
     651                try:
     652                    tier, tracker = re.match(tr0_param, param).groups()
     653                    trackers[tracker] = tier
     654                except AttributeError:
     655                    pass
    642656
    643657        if info_hash:
    644658            if not name:
    645659                name = info_hash
    646             return {'name': name, 'info_hash': info_hash, 'files_tree': ''}
     660            return {'name': name, 'info_hash': info_hash, 'files_tree': '', 'trackers': trackers}
    647661    return False
    648662
    649663
    650664def create_magnet_uri(infohash, name=None, trackers=None):
    651     """
    652     Creates a magnet uri
    653 
    654     :param infohash: the info-hash of the torrent
    655     :type infohash: string
    656     :param name: the name of the torrent (optional)
    657     :type name: string
    658     :param trackers: the trackers to announce to (optional)
    659     :type trackers: list of strings
    660 
    661     :returns: a magnet uri string
    662     :rtype: string
    663 
    664     """
    665     from base64 import b32encode
    666     uri = 'magnet:?xt=urn:btih:' + b32encode(infohash.decode('hex'))
     665    """Creates a magnet uri
     666
     667    Args:
     668        infohash (str): The info-hash of the torrent.
     669        name (str, optional): The name of the torrent.
     670        trackers (dict, optional): The trackers to announce to.
     671
     672    Returns:
     673        str: A magnet uri string.
     674
     675    """
     676
     677    uri = 'magnet:?xt=urn:btih:' + base64.b32encode(infohash.decode('hex'))
    667678    if name:
    668679        uri = uri + '&dn=' + name
    669680    if trackers:
    670         for t in trackers:
    671             uri = uri + '&tr=' + t
     681        try:
     682            for tracker in sorted(trackers, key=trackers.__getitem__):
     683                uri = ''.join([uri, '&tr.%d=' % trackers[tracker], tracker])
     684        except TypeError:
     685            for tracker in trackers:
     686                uri = ''.join([uri, '&tr=', tracker])
    672687
    673688    return uri
Note: See TracChangeset for help on using the changeset viewer.