From 43faaee77f930b9105d609da2327edd927765449 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Thu, 27 May 2021 12:01:57 -0400 Subject: [PATCH] Hotfix: remove site filter for maps links The new site filter breaks links to Maps results, so filter.py needed to be updated to handle these links as a unique case. A new method was introduced to easily remove any "-site:..." filters from the query, which is now also used to format queries in the header template rather than manually removing the blocked site list within the template itself. Bumps version to 0.5.1 for releasing the bugfix Fixes #329 --- app/__init__.py | 2 +- app/filter.py | 35 ++++++++++++++++++++++++++--------- app/request.py | 1 + app/routes.py | 3 ++- app/templates/header.html | 6 +++--- app/utils/search.py | 10 ++++++---- setup.py | 2 +- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 06d9306..fe05ede 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -22,7 +22,7 @@ app.default_key = generate_user_key() app.no_cookie_ips = [] app.config['SECRET_KEY'] = os.urandom(32) app.config['SESSION_TYPE'] = 'filesystem' -app.config['VERSION_NUMBER'] = '0.5.0' +app.config['VERSION_NUMBER'] = '0.5.1' app.config['APP_ROOT'] = os.getenv( 'APP_ROOT', os.path.dirname(os.path.abspath(__file__))) diff --git a/app/filter.py b/app/filter.py index 4fede8a..3804822 100644 --- a/app/filter.py +++ b/app/filter.py @@ -1,4 +1,4 @@ -from app.request import VALID_PARAMS +from app.request import VALID_PARAMS, MAPS_URL from app.utils.results import * from bs4 import BeautifulSoup from bs4.element import ResultSet, Tag @@ -9,6 +9,19 @@ import urllib.parse as urlparse from urllib.parse import parse_qs +def strip_blocked_sites(query: str) -> str: + """Strips the blocked site list from the query, if one is being + used. + + Args: + query: The query string + + Returns: + str: The query string without any "-site:..." filters + """ + return query[:query.find('-site:')] if '-site:' in query else query + + class Filter: def __init__(self, user_key: str, mobile=False, config=None) -> None: if config is None: @@ -210,20 +223,20 @@ class Filter: link['target'] = '_blank' result_link = urlparse.urlparse(href) - query_link = parse_qs( + query = parse_qs( result_link.query - )['q'][0] if '?q=' in href else '' + )['q'][0] if 'q=' in href else '' - if query_link.startswith('/'): + if query.startswith('/'): # Internal google links (i.e. mail, maps, etc) should still # be forwarded to Google - link['href'] = 'https://google.com' + query_link + link['href'] = 'https://google.com' + query elif '/search?q=' in href: # "li:1" implies the query should be interpreted verbatim, # which is accomplished by wrapping the query in double quotes if 'li:1' in href: - query_link = '"' + query_link + '"' - new_search = 'search?q=' + self.encrypt_path(query_link) + query = '"' + query + '"' + new_search = 'search?q=' + self.encrypt_path(query) query_params = parse_qs(urlparse.urlparse(href).query) for param in VALID_PARAMS: @@ -234,13 +247,17 @@ class Filter: link['href'] = new_search elif 'url?q=' in href: # Strip unneeded arguments - link['href'] = filter_link_args(query_link) + link['href'] = filter_link_args(query) # Add no-js option if self.nojs: append_nojs(link) else: - link['href'] = href + if href.startswith(MAPS_URL): + # Maps links don't work if a site filter is applied + link['href'] = MAPS_URL + "?q=" + strip_blocked_sites(query) + else: + link['href'] = href # Replace link location if "alts" config is enabled if self.alt_redirect: diff --git a/app/request.py b/app/request.py index 1c0c2c6..ab3853b 100644 --- a/app/request.py +++ b/app/request.py @@ -10,6 +10,7 @@ from stem import Signal, SocketError from stem.control import Controller SEARCH_URL = 'https://www.google.com/search?gbv=1&q=' +MAPS_URL = 'https://maps.google.com/maps' AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/' 'complete/search?client=toolbar&') diff --git a/app/routes.py b/app/routes.py index 4409901..d325fbd 100644 --- a/app/routes.py +++ b/app/routes.py @@ -13,6 +13,7 @@ from flask import jsonify, make_response, request, redirect, render_template, \ from requests import exceptions from app import app +from app.filter import strip_blocked_sites from app.models.config import Config from app.request import Request, TorError from app.utils.bangs import resolve_bang @@ -247,7 +248,7 @@ def search(): 'header.html', config=g.user_config, logo=render_template('logo.html', dark=g.user_config.dark), - query=urlparse.unquote(query), + query=strip_blocked_sites(urlparse.unquote(query)), search_type=search_util.search_type, mobile=g.user_request.mobile) if 'isch' not in search_util.search_type else '')), resp_code diff --git a/app/templates/header.html b/app/templates/header.html index 50bde42..0f17064 100644 --- a/app/templates/header.html +++ b/app/templates/header.html @@ -20,9 +20,9 @@ class="noHIxc" name="q" style="background-color: {{ 'var(--whoogle-dark-result-bg)' if config.dark else 'var(--whoogle-result-bg)' }} !important; - color: {{ 'var(--whoogle-dark-text)' if config.dark else 'var(--whoogle-text)' }}; + color: {{ 'var(--whoogle-dark-text)' if config.dark else 'var(--whoogle-text)' }};" type="text" - value="{{ query[:query.find('-site:')] if '-site:' in query else query }}"> + value="{{ query }}"> @@ -54,7 +54,7 @@ name="q" spellcheck="false" type="text" - value="{{ query[:query.find('-site:')] if '-site:' in query else query }}" + value="{{ query }}" style="background-color: {{ 'var(--whoogle-dark-result-bg)' if config.dark else 'var(--whoogle-result-bg)' }} !important; color: {{ 'var(--whoogle-dark-text)' if config.dark else 'var(--whoogle-text)' }}; border-bottom: {{ '2px solid var(--whoogle-dark-element-bg)' if config.dark else '0px' }};"> diff --git a/app/utils/search.py b/app/utils/search.py index bb24e4b..561ec2f 100644 --- a/app/utils/search.py +++ b/app/utils/search.py @@ -1,10 +1,12 @@ -from app.filter import Filter, get_first_link -from app.request import gen_query +import os +from typing import Any + from bs4 import BeautifulSoup as bsoup from cryptography.fernet import Fernet, InvalidToken from flask import g -from typing import Any, Tuple -import os + +from app.filter import Filter, get_first_link +from app.request import gen_query TOR_BANNER = '

You are using Tor


' CAPTCHA = 'div class="g-recaptcha"' diff --git a/setup.py b/setup.py index 75f7fca..997ea16 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( author='Ben Busby', author_email='benbusby@protonmail.com', name='whoogle-search', - version='0.5.0', + version='0.5.1', include_package_data=True, install_requires=requirements, description='Self-hosted, ad-free, privacy-respecting metasearch engine',