From 0d2d5fff5d096b1e018a8e9a9f60f32eaf65d314 Mon Sep 17 00:00:00 2001 From: "Joao A. Candido Ramos" Date: Mon, 27 Jun 2022 20:33:08 +0200 Subject: [PATCH] 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 used --- app/filter.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/filter.py b/app/filter.py index 26fcc9b..3dcfc03 100644 --- a/app/filter.py +++ b/app/filter.py @@ -18,6 +18,8 @@ from app.models.endpoint import Endpoint from app.models.config import Config +MAPS_ARGS = ['q', 'daddr'] + minimal_mode_sections = ['Top stories', 'Images', 'People also ask'] unsupported_g_pages = [ '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 '' +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: """Strips the blocked site list from the query, if one is being used. @@ -474,7 +498,7 @@ class Filter: else: if href.startswith(MAPS_URL): # 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 href.startswith('/imgres?')): # make sure that tags can be clicked as relative URLs