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
main
Ben Busby 2021-05-27 12:01:57 -04:00
parent cf55765933
commit 43faaee77f
No known key found for this signature in database
GPG Key ID: 3B08611DF6E62ED2
7 changed files with 40 additions and 19 deletions

View File

@ -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__)))

View File

@ -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:

View File

@ -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&')

View File

@ -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

View File

@ -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 }}">
<input style="color: {{ 'var(--whoogle-dark-text)' if config.dark else 'var(--whoogle-text)' }}" id="search-reset" type="reset" value="x">
<input name="tbm" value="{{ search_type }}" style="display: none">
<input type="submit" style="display: none;">
@ -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' }};">

View File

@ -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 = '<hr><h1 style="text-align: center">You are using Tor</h1><hr>'
CAPTCHA = 'div class="g-recaptcha"'

View File

@ -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',