From ff885e4fdec7b5df87132d8ec89ca7c0729ea729 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Thu, 14 Oct 2021 18:58:13 -0600 Subject: [PATCH] Disable autocomplete via WHOOGLE_AUTOCOMPLETE var Setting WHOOGLE_AUTOCOMPLETE to 0 now disables the autocomplete/search suggestion feature. Closes #462 --- README.md | 33 +++++++++++++++++---------------- app/models/config.py | 7 +------ app/routes.py | 5 +++++ app/utils/misc.py | 7 +++++++ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8877c23..0fe72db 100644 --- a/README.md +++ b/README.md @@ -296,22 +296,23 @@ There are a few optional environment variables available for customizing a Whoog - With `docker-compose`: Uncomment the `env_file` option - With `docker build/run`: Add `--env-file ./whoogle.env` to your command -| Variable | Description | -| ------------------ | ----------------------------------------------------------------------------------------- | -| WHOOGLE_DOTENV | Load environment variables in `whoogle.env` | -| WHOOGLE_USER | The username for basic auth. WHOOGLE_PASS must also be set if used. | -| WHOOGLE_PASS | The password for basic auth. WHOOGLE_USER must also be set if used. | -| WHOOGLE_PROXY_USER | The username of the proxy server. | -| WHOOGLE_PROXY_PASS | The password of the proxy server. | -| WHOOGLE_PROXY_TYPE | The type of the proxy server. Can be "socks5", "socks4", or "http". | -| WHOOGLE_PROXY_LOC | The location of the proxy server (host or ip). | -| EXPOSE_PORT | The port where Whoogle will be exposed. | -| HTTPS_ONLY | Enforce HTTPS. (See [here](https://github.com/benbusby/whoogle-search#https-enforcement)) | -| WHOOGLE_ALT_TW | The twitter.com alternative to use when site alternatives are enabled in the config. | -| WHOOGLE_ALT_YT | The youtube.com alternative to use when site alternatives are enabled in the config. | -| WHOOGLE_ALT_IG | The instagram.com alternative to use when site alternatives are enabled in the config. | -| WHOOGLE_ALT_RD | The reddit.com alternative to use when site alternatives are enabled in the config. | -| WHOOGLE_ALT_TL | The Google Translate alternative to use. This is used for all "translate ____" searches. | +| Variable | Description | +| -------------------- | ----------------------------------------------------------------------------------------- | +| WHOOGLE_DOTENV | Load environment variables in `whoogle.env` | +| WHOOGLE_USER | The username for basic auth. WHOOGLE_PASS must also be set if used. | +| WHOOGLE_PASS | The password for basic auth. WHOOGLE_USER must also be set if used. | +| WHOOGLE_PROXY_USER | The username of the proxy server. | +| WHOOGLE_PROXY_PASS | The password of the proxy server. | +| WHOOGLE_PROXY_TYPE | The type of the proxy server. Can be "socks5", "socks4", or "http". | +| WHOOGLE_PROXY_LOC | The location of the proxy server (host or ip). | +| EXPOSE_PORT | The port where Whoogle will be exposed. | +| HTTPS_ONLY | Enforce HTTPS. (See [here](https://github.com/benbusby/whoogle-search#https-enforcement)) | +| WHOOGLE_ALT_TW | The twitter.com alternative to use when site alternatives are enabled in the config. | +| WHOOGLE_ALT_YT | The youtube.com alternative to use when site alternatives are enabled in the config. | +| WHOOGLE_ALT_IG | The instagram.com alternative to use when site alternatives are enabled in the config. | +| WHOOGLE_ALT_RD | The reddit.com alternative to use when site alternatives are enabled in the config. | +| WHOOGLE_ALT_TL | The Google Translate alternative to use. This is used for all "translate ____" searches. | +| WHOOGLE_AUTOCOMPLETE | Controls visibility of autocomplete/search suggestions. Default on -- use '0' to disable | ### Config Environment Variables These environment variables allow setting default config values, but can be overwritten manually by using the home page config menu. These allow a shortcut for destroying/rebuilding an instance to the same config state every time. diff --git a/app/models/config.py b/app/models/config.py index f865a17..7d0da92 100644 --- a/app/models/config.py +++ b/app/models/config.py @@ -1,15 +1,10 @@ +from app.utils.misc import read_config_bool from flask import current_app import os class Config: def __init__(self, **kwargs): - def read_config_bool(var: str) -> bool: - val = os.getenv(var, '0') - if val.isdigit(): - return bool(int(val)) - return False - app_config = current_app.config self.url = os.getenv('WHOOGLE_CONFIG_URL', '') self.lang_search = os.getenv('WHOOGLE_CONFIG_SEARCH_LANGUAGE', '') diff --git a/app/routes.py b/app/routes.py index b5bcca2..35cff9b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -16,6 +16,7 @@ from app import app from app.models.config import Config from app.request import Request, TorError from app.utils.bangs import resolve_bang +from app.utils.misc import read_config_bool from app.utils.session import generate_user_key, valid_user_session from app.utils.search import * @@ -178,6 +179,10 @@ def search_html(): @app.route('/autocomplete', methods=['GET', 'POST']) def autocomplete(): + ac_var = 'WHOOGLE_AUTOCOMPLETE' + if os.getenv(ac_var) and not read_config_bool(ac_var): + return jsonify({}) + q = g.request_params.get('q') if not q: # FF will occasionally (incorrectly) send the q field without a diff --git a/app/utils/misc.py b/app/utils/misc.py index 9327826..01e6935 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -8,3 +8,10 @@ def gen_file_hash(path: str, static_file: str) -> str: filename_split = os.path.splitext(static_file) return filename_split[0] + '.' + file_hash + filename_split[-1] + + +def read_config_bool(var: str) -> bool: + val = os.getenv(var, '0') + if val.isdigit(): + return bool(int(val)) + return False