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.no_cookie_ips = []
app.config['SECRET_KEY'] = os.urandom(32) app.config['SECRET_KEY'] = os.urandom(32)
app.config['SESSION_TYPE'] = 'filesystem' 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.config['APP_ROOT'] = os.getenv(
'APP_ROOT', 'APP_ROOT',
os.path.dirname(os.path.abspath(__file__))) 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 app.utils.results import *
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from bs4.element import ResultSet, Tag from bs4.element import ResultSet, Tag
@ -9,6 +9,19 @@ import urllib.parse as urlparse
from urllib.parse import parse_qs 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: class Filter:
def __init__(self, user_key: str, mobile=False, config=None) -> None: def __init__(self, user_key: str, mobile=False, config=None) -> None:
if config is None: if config is None:
@ -210,20 +223,20 @@ class Filter:
link['target'] = '_blank' link['target'] = '_blank'
result_link = urlparse.urlparse(href) result_link = urlparse.urlparse(href)
query_link = parse_qs( query = parse_qs(
result_link.query 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 # Internal google links (i.e. mail, maps, etc) should still
# be forwarded to Google # be forwarded to Google
link['href'] = 'https://google.com' + query_link link['href'] = 'https://google.com' + query
elif '/search?q=' in href: elif '/search?q=' in href:
# "li:1" implies the query should be interpreted verbatim, # "li:1" implies the query should be interpreted verbatim,
# which is accomplished by wrapping the query in double quotes # which is accomplished by wrapping the query in double quotes
if 'li:1' in href: if 'li:1' in href:
query_link = '"' + query_link + '"' query = '"' + query + '"'
new_search = 'search?q=' + self.encrypt_path(query_link) new_search = 'search?q=' + self.encrypt_path(query)
query_params = parse_qs(urlparse.urlparse(href).query) query_params = parse_qs(urlparse.urlparse(href).query)
for param in VALID_PARAMS: for param in VALID_PARAMS:
@ -234,11 +247,15 @@ class Filter:
link['href'] = new_search link['href'] = new_search
elif 'url?q=' in href: elif 'url?q=' in href:
# Strip unneeded arguments # Strip unneeded arguments
link['href'] = filter_link_args(query_link) link['href'] = filter_link_args(query)
# Add no-js option # Add no-js option
if self.nojs: if self.nojs:
append_nojs(link) append_nojs(link)
else:
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: else:
link['href'] = href link['href'] = href

View File

@ -10,6 +10,7 @@ from stem import Signal, SocketError
from stem.control import Controller from stem.control import Controller
SEARCH_URL = 'https://www.google.com/search?gbv=1&q=' SEARCH_URL = 'https://www.google.com/search?gbv=1&q='
MAPS_URL = 'https://maps.google.com/maps'
AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/' AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/'
'complete/search?client=toolbar&') '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 requests import exceptions
from app import app from app import app
from app.filter import strip_blocked_sites
from app.models.config import Config from app.models.config import Config
from app.request import Request, TorError from app.request import Request, TorError
from app.utils.bangs import resolve_bang from app.utils.bangs import resolve_bang
@ -247,7 +248,7 @@ def search():
'header.html', 'header.html',
config=g.user_config, config=g.user_config,
logo=render_template('logo.html', dark=g.user_config.dark), 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, search_type=search_util.search_type,
mobile=g.user_request.mobile) mobile=g.user_request.mobile)
if 'isch' not in search_util.search_type else '')), resp_code if 'isch' not in search_util.search_type else '')), resp_code

View File

@ -20,9 +20,9 @@
class="noHIxc" class="noHIxc"
name="q" name="q"
style="background-color: {{ 'var(--whoogle-dark-result-bg)' if config.dark else 'var(--whoogle-result-bg)' }} !important; 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" 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 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 name="tbm" value="{{ search_type }}" style="display: none">
<input type="submit" style="display: none;"> <input type="submit" style="display: none;">
@ -54,7 +54,7 @@
name="q" name="q"
spellcheck="false" spellcheck="false"
type="text" 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; 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)' }};
border-bottom: {{ '2px solid var(--whoogle-dark-element-bg)' if config.dark else '0px' }};"> 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 import os
from app.request import gen_query from typing import Any
from bs4 import BeautifulSoup as bsoup from bs4 import BeautifulSoup as bsoup
from cryptography.fernet import Fernet, InvalidToken from cryptography.fernet import Fernet, InvalidToken
from flask import g 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>' TOR_BANNER = '<hr><h1 style="text-align: center">You are using Tor</h1><hr>'
CAPTCHA = 'div class="g-recaptcha"' CAPTCHA = 'div class="g-recaptcha"'

View File

@ -8,7 +8,7 @@ setuptools.setup(
author='Ben Busby', author='Ben Busby',
author_email='benbusby@protonmail.com', author_email='benbusby@protonmail.com',
name='whoogle-search', name='whoogle-search',
version='0.5.0', version='0.5.1',
include_package_data=True, include_package_data=True,
install_requires=requirements, install_requires=requirements,
description='Self-hosted, ad-free, privacy-respecting metasearch engine', description='Self-hosted, ad-free, privacy-respecting metasearch engine',