Fixes handling of maps (#792)
* fixes map url, e.g. when no q parameter is given * move maps_args from results to filter where it is usedmain
parent
90e160094d
commit
0d2d5fff5d
|
@ -18,6 +18,8 @@ from app.models.endpoint import Endpoint
|
||||||
from app.models.config import Config
|
from app.models.config import Config
|
||||||
|
|
||||||
|
|
||||||
|
MAPS_ARGS = ['q', 'daddr']
|
||||||
|
|
||||||
minimal_mode_sections = ['Top stories', 'Images', 'People also ask']
|
minimal_mode_sections = ['Top stories', 'Images', 'People also ask']
|
||||||
unsupported_g_pages = [
|
unsupported_g_pages = [
|
||||||
'support.google.com',
|
'support.google.com',
|
||||||
|
@ -45,6 +47,28 @@ def extract_q(q_str: str, href: str) -> str:
|
||||||
return parse_qs(q_str)['q'][0] if ('&q=' in href or '?q=' in href) else ''
|
return parse_qs(q_str)['q'][0] if ('&q=' in href or '?q=' in href) else ''
|
||||||
|
|
||||||
|
|
||||||
|
def build_map_url(href: str) -> str:
|
||||||
|
"""Tries to extract known args that explain the location in the url. If a
|
||||||
|
location is found, returns the default url with it. Otherwise, returns the
|
||||||
|
url unchanged.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
href: The full url to check.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The parsed url, or the url unchanged.
|
||||||
|
"""
|
||||||
|
# parse the url
|
||||||
|
parsed_url = parse_qs(href)
|
||||||
|
# iterate through the known parameters and try build the url
|
||||||
|
for param in MAPS_ARGS:
|
||||||
|
if param in parsed_url:
|
||||||
|
return MAPS_URL + "?q=" + parsed_url[param][0]
|
||||||
|
|
||||||
|
# query could not be extracted returning unchanged url
|
||||||
|
return href
|
||||||
|
|
||||||
|
|
||||||
def clean_query(query: str) -> str:
|
def clean_query(query: str) -> str:
|
||||||
"""Strips the blocked site list from the query, if one is being
|
"""Strips the blocked site list from the query, if one is being
|
||||||
used.
|
used.
|
||||||
|
@ -474,7 +498,7 @@ class Filter:
|
||||||
else:
|
else:
|
||||||
if href.startswith(MAPS_URL):
|
if href.startswith(MAPS_URL):
|
||||||
# Maps links don't work if a site filter is applied
|
# Maps links don't work if a site filter is applied
|
||||||
link['href'] = MAPS_URL + "?q=" + clean_query(q)
|
link['href'] = build_map_url(link['href'])
|
||||||
elif (href.startswith('/?') or href.startswith('/search?') or
|
elif (href.startswith('/?') or href.startswith('/search?') or
|
||||||
href.startswith('/imgres?')):
|
href.startswith('/imgres?')):
|
||||||
# make sure that tags can be clicked as relative URLs
|
# make sure that tags can be clicked as relative URLs
|
||||||
|
|
Loading…
Reference in New Issue