Skip to content

Commit

Permalink
Check for updates using 24 hour time delta
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benbusby committed Feb 14, 2022
1 parent d33e824 commit 23402e2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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
Expand Down Expand Up @@ -71,8 +72,12 @@
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
Expand Down
26 changes: 13 additions & 13 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pickle
import urllib.parse as urlparse
import uuid
from datetime import timedelta
from datetime import datetime, timedelta
from functools import wraps

import waitress
Expand All @@ -16,7 +16,8 @@
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
Expand All @@ -31,15 +32,6 @@
# 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')

Expand Down Expand Up @@ -106,6 +98,14 @@ def decorated(*args, **kwargs):
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
)
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
17 changes: 17 additions & 0 deletions app/utils/misc.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 23402e2

Please sign in to comment.