summaryrefslogtreecommitdiff
path: root/searx/utils.py
diff options
context:
space:
mode:
authorJohannes Schauer Marin Rodrigues <josch@debian.org>2022-09-25 08:04:59 +0200
committerJohannes Schauer Marin Rodrigues <josch@debian.org>2022-09-25 08:04:59 +0200
commitfd6764405d464159c648526bc73bf11714a41633 (patch)
tree5ec2466cff9a796e3b5428c41b53d2a8db80e8b6 /searx/utils.py
parent32d4b6a638456caf50a5f99f2a0b57d60d418c5f (diff)
New upstream version 1.1.0+dfsg1
Diffstat (limited to 'searx/utils.py')
-rw-r--r--searx/utils.py18
1 files changed, 12 insertions, 6 deletions
diff --git a/searx/utils.py b/searx/utils.py
index 3172ad8..c60edf3 100644
--- a/searx/utils.py
+++ b/searx/utils.py
@@ -7,7 +7,7 @@ from numbers import Number
from os.path import splitext, join
from random import choice
from html.parser import HTMLParser
-from urllib.parse import urljoin, urlparse
+from urllib.parse import urljoin, urlparse, urlunparse
from lxml import html
from lxml.etree import ElementBase, XPath, XPathError, XPathSyntaxError, _ElementStringResult, _ElementUnicodeResult
@@ -214,6 +214,16 @@ def normalize_url(url, base_url):
return url
+def add_scheme_to_url(url, scheme="https"):
+ """Add schema to URL: if scheme is missing from the URL, then add it."""
+
+ parsed = urlparse(url)
+ if parsed.scheme == '':
+ parsed_with_scheme = parsed._replace(scheme=scheme)
+ return urlunparse(parsed_with_scheme)
+ return url
+
+
def extract_url(xpath_results, base_url):
"""Extract and normalize URL from lxml Element
@@ -262,11 +272,7 @@ def dict_subset(d, properties):
>>> >> dict_subset({'A': 'a', 'B': 'b', 'C': 'c'}, ['A', 'D'])
{'A': 'a'}
"""
- result = {}
- for k in properties:
- if k in d:
- result[k] = d[k]
- return result
+ return {k: d[k] for k in properties if k in d}
def get_torrent_size(filesize, filesize_multiplier):