Skip to content

Commit

Permalink
Merge branch 'release-7.6' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Dec 15, 2020
2 parents 427f482 + 7521cff commit e535439
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

import async_timeout

from ipv8.messaging.deprecated.encoding import add_url_params
from ipv8.taskmanager import TaskManager

from tribler_core.modules.tunnel.socks5.aiohttp_connector import Socks5Connector
from tribler_core.modules.tunnel.socks5.client import Socks5Client
from tribler_core.utilities.tracker_utils import parse_tracker_url
from tribler_core.utilities.tracker_utils import add_url_params, parse_tracker_url
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import bdecode_compat

Expand Down
3 changes: 1 addition & 2 deletions src/tribler-core/tribler_core/tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging

from ipv8.messaging.deprecated.encoding import add_url_params

from tribler_core import load_logger_config
from tribler_core.utilities.tracker_utils import add_url_params
from tribler_core.utilities.utilities import parse_magnetlink


Expand Down
43 changes: 42 additions & 1 deletion src/tribler-core/tribler_core/utilities/tracker_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from http.client import HTTP_PORT
from urllib.parse import urlparse
from json import dumps
from urllib.parse import ParseResult, parse_qsl, unquote, urlencode, urlparse


class MalformedTrackerURLException(Exception):
Expand Down Expand Up @@ -132,3 +133,43 @@ def parse_tracker_url(tracker_url):
raise MalformedTrackerURLException(u'Missing announce path for HTTP tracker url (%s).' % tracker_url)

return url.scheme, (url.hostname, url.port), url.path


def add_url_params(url, params):
""" Add GET params to provided URL being aware of existing.
:param url: string of target URL
:param params: dict containing requested params to be added
:return: string with updated URL
>> url = 'http://stackoverflow.com/test?answers=true'
>> new_params = {'answers': False, 'data': ['some','values']}
>> add_url_params(url, new_params)
'http://stackoverflow.com/test?data=some&data=values&answers=false'
"""
# Unquoting URL first so we don't loose existing args
url = unquote(url)
# Extracting url info
parsed_url = urlparse(url)
# Extracting URL arguments from parsed URL
get_args = parsed_url.query
# Converting URL arguments to dict
parsed_get_args = dict(parse_qsl(get_args))
# Merging URL arguments dict with new params
parsed_get_args.update(params)

# Bool and Dict values should be converted to json-friendly values
# you may throw this part away if you don't like it :)
parsed_get_args.update(
{k: dumps(v) for k, v in parsed_get_args.items()
if isinstance(v, (bool, dict))}
)

# Converting URL argument to proper query string
encoded_get_args = urlencode(parsed_get_args, doseq=True)
# Creating new parsed result object based on provided with new
# URL arguments. Same thing happens inside of urlparse.
new_url = ParseResult(
parsed_url.scheme, parsed_url.netloc, parsed_url.path,
parsed_url.params, encoded_get_args, parsed_url.fragment
).geturl()

return new_url
2 changes: 1 addition & 1 deletion src/tribler-gui/tribler_gui/widgets/downloadspage.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def on_move_files(self, checked):

TriblerNetworkRequest(
"downloads/%s" % _infohash,
lambda res, _, name=_name, target=dest_dir: self.on_files_moved(res, name, target),
lambda res: self.on_files_moved(res, _name, dest_dir),
data=data,
method='PATCH',
)
Expand Down

0 comments on commit e535439

Please sign in to comment.