diff --git a/cli.py b/cli.py index 9fd6d9b..fb29939 100644 --- a/cli.py +++ b/cli.py @@ -15,7 +15,7 @@ from terminaltables import SingleTable scores: List[dict] = [] lastAttempt: float = -1 -lastUpdate: datetime = None +lastUpdate: float = -1 def refreshScores() -> bool: @@ -30,7 +30,7 @@ def refreshScores() -> bool: global lastUpdate, lastAttempt, scores # Send with If-Modified-Since header if this is not the first time - headers = {'If-Modified-Since': lastUpdate.strftime('%a, %d %b %Y %I:%M:%S %Z')} if lastUpdate else {} + headers = {'If-Modified-Since': datetime.fromtimestamp(lastAttempt).strftime('%a, %d %b %Y %I:%M:%S')} if lastAttempt > 0 else {} # Send request with headers try: resp = requests.get('http://127.0.0.1:5000/api/scores/', headers=headers) @@ -44,7 +44,7 @@ def refreshScores() -> bool: pass else: # Changes found, update! - lastUpdate = datetime.now(pytz.utc) + lastUpdate = time.time() scores = resp.json() # Calculate totals, preliminary sort by total @@ -80,7 +80,7 @@ def main(screen) -> None: screen.redrawwin() while True: # Refresh scores every 10 seconds - if time.time() - lastAttempt > 1.5: + if time.time() - lastAttempt > 0.5: refreshScores() # Get current terminal size and clear @@ -104,7 +104,7 @@ def main(screen) -> None: # Terminal Size strpos = str((x, y)) screen.addstr(y - 1, 1, strpos) - screen.addstr(y - 1, 1 + len(strpos) + 1, f'({str(round(1.5 - (time.time() - lastAttempt), 3)).zfill(3)})') + screen.addstr(y - 1, 1 + len(strpos) + 1, f'({str(round(0.5 - (time.time() - lastAttempt), 3)).zfill(3)})') # Update curses screen screen.refresh() diff --git a/trivia/api.py b/trivia/api.py index ea1ed2e..0afd479 100644 --- a/trivia/api.py +++ b/trivia/api.py @@ -6,6 +6,7 @@ Handles backend routes assisting import json import time +from datetime import datetime from flask import request, make_response, current_app @@ -26,12 +27,12 @@ def scores(): try: if request.headers['If-Modified-Since']: # Acquire epoch time from header - epoch = time.mktime(time.strptime(request.headers['If-Modified-Since'], "%a, %d %b %Y %I:%M:%S %Z")) - if epoch < lastChange: + epoch = time.mktime(time.strptime(request.headers['If-Modified-Since'], "%a, %d %b %Y %I:%M:%S")) + if epoch >= lastChange: status_code = 304 except KeyError: pass # Header was not supplied. Ignore. except ValueError: - current_app.logger.warning('If-Modified-Since Header could not be parsed.') # Header could not be parsed. + current_app.logger.warning('If-Modified-Since Header could not be parsed.', exc_info=True) # Header could not be parsed. return r, status_code diff --git a/trivia/config.py b/trivia/config.py index e343e58..8f7c35c 100644 --- a/trivia/config.py +++ b/trivia/config.py @@ -22,6 +22,7 @@ class Config(object): DEMO_ALTERATION_INTERVAL = 0 DEMO_MAX_SCORES = 0 + class DemoConfig(Config): # Main Configuration SCORE_FILE = 'demo.json' diff --git a/trivia/create_app.py b/trivia/create_app.py index 65de3f3..b465566 100644 --- a/trivia/create_app.py +++ b/trivia/create_app.py @@ -27,7 +27,9 @@ def create_app(env=None): if app.config['DEMO']: app.logger.info('Generating Demo Data...') + # Generate initial Demo data utils.generateDemo() + # Begin altering demo data regularly scheduler.add_job(id='altering', func=utils.alterDemo, trigger="interval", seconds=app.config['DEMO_ALTERATION_INTERVAL']) utils.refreshScores() diff --git a/trivia/utils.py b/trivia/utils.py index c29510a..87e9a55 100644 --- a/trivia/utils.py +++ b/trivia/utils.py @@ -5,6 +5,7 @@ Stores important backend application functionality. """ import json import os +import time import random from collections import namedtuple from typing import List @@ -21,7 +22,7 @@ DATA_DIR = os.path.join(BASE_DIR, 'data') SCORES_FILE = os.path.join(DATA_DIR, current_app.config['SCORE_FILE']) # Initialize global data/tracking vars -lastChange: int = -1 +lastChange: float = -1 teams: List[Team] = []