diff --git a/cli.py b/cli.py index efbb29c..62c78db 100644 --- a/cli.py +++ b/cli.py @@ -5,50 +5,75 @@ A simple CLI implementation using the application's API. """ import curses -import random -import shutil -import string +from datetime import datetime +from typing import List +import pytz +import requests from terminaltables import SingleTable -def generateTable() -> SingleTable: - tableData = [team() for _ in range(5)] - tableData.insert(0, ['Rank', 'ID', 'Team Name'] + list(map(str, range(1, 18)))) - return SingleTable(tableData) +scores: List[dict] = [] +lastAttempt: float = -1 +lastUpdate: datetime = None -def team(): - return [random.randint(1, 15), random.randint(1, 30), ''.join(random.choices(string.ascii_letters, k=18))] + [random.randint(0, 9) for _ in range(20)] +def refreshScores() -> None: + 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 {} + # Send request with headers + resp = requests.get('http://localhost:5000/api/scores/', headers=headers) + + if resp.ok: + if resp.status_code == 304 and len(scores) != 0: + pass + else: + # Changes found, update! + lastUpdate = datetime.now(pytz.utc) + print(f'"{resp.text}"') + scores = resp.json() -def main(stdscr): +def main(screen): while True: - y, x = stdscr.getmaxyx() - stdscr.clear() + # Get current terminal size and clear + y, x = screen.getmaxyx() + screen.clear() + global scores # Print Table to screen - table = generateTable() - for i, stringRow in enumerate(table.table.split('\n')): - stdscr.addstr(i, 0, stringRow[:x]) - stdscr.addstr(y - 1, 0, str((x, y))) + table = [[-1, team['id'], team['name'], team['total'], *team['scores']] for team in scores[:y - 4]] + table.insert(0, ['Rank', 'ID', 'Team Name', 'Total']) + + table = SingleTable(table) + + for i, stringRow in enumerate(table.table.split('\n')[:y]): + screen.addstr(i, 0, stringRow[:x]) + screen.addstr(y - 1, 1, str((x, y))) + # Check for quit key - key = stdscr.getch() + key = screen.getch() if key == ord('q'): break - stdscr.refresh() + screen.refresh() if __name__ == "__main__": - screen = curses.initscr() + stdscr = curses.initscr() + # Setup curses friendly terminal flags, run app try: + refreshScores() curses.cbreak() - screen.nodelay(1) + stdscr.nodelay(1) curses.noecho() - screen.keypad(True) - main(screen) + curses.curs_set(False) + stdscr.keypad(True) + main(stdscr) + # Undo curses terminal options finally: - screen.clear() + stdscr.clear() curses.nocbreak() curses.echo() - curses.endwin() \ No newline at end of file + curses.endwin() diff --git a/requirements.txt b/requirements.txt index 69daeb4..5afbb23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask~=1.1.2 -APScheduler~=3.6.3 \ No newline at end of file +APScheduler~=3.6.3 +windows-curses \ No newline at end of file