Check for updates using 24 hour time delta

Rather than only checking for an available update on app init, the check
for updates now performs the check once every 24 hours on the first
request sent after that period.

This also now catches the requests.exceptions.ConnectionError that is
thrown if the app is initialized without an active internet connection.

Fixes #649
main
Ben Busby 2022-02-14 12:19:02 -07:00
parent d33e8241dc
commit 23402e27e1
No known key found for this signature in database
GPG Key ID: B9B7231E01D924A1
4 changed files with 36 additions and 14 deletions

View File

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

View File

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

View File

@ -2,7 +2,7 @@
<p class="footer">
Whoogle Search v{{ version_number }} ||
<a class="link" href="https://github.com/benbusby/whoogle-search">{{ translation['github-link'] }}</a>
{% if newest_version %}
{% if has_update %}
|| <span class="update_available">Update Available 🟢</span>
{% endif %}
</p>

View File

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