diff --git a/app/__init__.py b/app/__init__.py index f97bfc2..83f8847 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,6 +3,7 @@ from app.request import send_tor_signal from app.utils.session import generate_user_key from app.utils.bangs import gen_bangs_json from app.utils.misc import gen_file_hash, read_config_bool +from datetime import datetime, timedelta from flask import Flask from flask_session import Session import json @@ -71,8 +72,12 @@ app.config['BANG_PATH'] = os.getenv( app.config['BANG_FILE'] = os.path.join( app.config['BANG_PATH'], 'bangs.json') + +# Config fields that are used to check for updates app.config['RELEASES_URL'] = 'https://github.com/' \ 'benbusby/whoogle-search/releases' +app.config['LAST_UPDATE_CHECK'] = datetime.now() - timedelta(hours=24) +app.config['HAS_UPDATE'] = '' # The alternative to Google Translate is treated a bit differently than other # social media site alternatives, in that it is used for any translation diff --git a/app/routes.py b/app/routes.py index a112ad8..350621f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -7,7 +7,7 @@ import os import pickle import urllib.parse as urlparse import uuid -from datetime import timedelta +from datetime import datetime, timedelta from functools import wraps import waitress @@ -16,7 +16,8 @@ from app.models.config import Config from app.models.endpoint import Endpoint from app.request import Request, TorError from app.utils.bangs import resolve_bang -from app.utils.misc import read_config_bool, get_client_ip, get_request_url +from app.utils.misc import read_config_bool, get_client_ip, get_request_url, \ + check_for_update from app.utils.results import add_ip_card, bold_search_terms,\ add_currency_card, check_currency, get_tabs_content from app.utils.search import Search, needs_https, has_captcha @@ -31,15 +32,6 @@ from cryptography.fernet import Fernet # Load DDG bang json files only on init bang_json = json.load(open(app.config['BANG_FILE'])) or {} -# Check the newest version of WHOOGLE -update = bsoup(get(app.config['RELEASES_URL']).text, 'html.parser') -newest_version = update.select_one('[class="Link--primary"]').string[1:] -current_version = int(''.join(filter(str.isdigit, - app.config['VERSION_NUMBER']))) -newest_version = int(''.join(filter(str.isdigit, newest_version))) -newest_version = '' if current_version >= newest_version \ - else newest_version - ac_var = 'WHOOGLE_AUTOCOMPLETE' autocomplete_enabled = os.getenv(ac_var, '1') @@ -106,6 +98,14 @@ def session_required(f): def before_request_func(): global bang_json + # Check for latest version if needed + now = datetime.now() + if now - timedelta(hours=24) > app.config['LAST_UPDATE_CHECK']: + app.config['LAST_UPDATE_CHECK'] = now + app.config['HAS_UPDATE'] = check_for_update( + app.config['RELEASES_URL'], + app.config['VERSION_NUMBER']) + g.request_params = ( request.args if request.method == 'GET' else request.form ) @@ -214,7 +214,7 @@ def index(): return render_template('error.html', error_message=error_message) return render_template('index.html', - newest_version=newest_version, + has_update=app.config['HAS_UPDATE'], languages=app.config['LANGUAGES'], countries=app.config['COUNTRIES'], themes=app.config['THEMES'], @@ -363,7 +363,7 @@ def search(): return render_template( 'display.html', - newest_version=newest_version, + has_update=app.config['HAS_UPDATE'], query=urlparse.unquote(query), search_type=search_util.search_type, config=g.user_config, diff --git a/app/templates/footer.html b/app/templates/footer.html index ed66e42..f9ed4cc 100644 --- a/app/templates/footer.html +++ b/app/templates/footer.html @@ -2,7 +2,7 @@
diff --git a/app/utils/misc.py b/app/utils/misc.py index 7f1f67b..7829403 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -1,6 +1,8 @@ +from bs4 import BeautifulSoup as bsoup from flask import Request import hashlib import os +from requests import exceptions, get def gen_file_hash(path: str, static_file: str) -> str: @@ -30,3 +32,18 @@ def get_request_url(url: str) -> str: return url.replace('http://', 'https://', 1) return url + + +def check_for_update(version_url: str, current: str) -> int: + # Check for the latest version of Whoogle + try: + update = bsoup(get(version_url).text, 'html.parser') + latest = update.select_one('[class="Link--primary"]').string[1:] + current = int(''.join(filter(str.isdigit, current))) + latest = int(''.join(filter(str.isdigit, latest))) + has_update = '' if current >= latest else latest + except (exceptions.ConnectionError, AttributeError): + # Ignore failures, assume current version is up to date + has_update = '' + + return has_update