mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-07 03:16:48 -06:00
get data from API automatically (with cache logic), table generation/display with API data
This commit is contained in:
71
cli.py
71
cli.py
@@ -5,50 +5,75 @@ A simple CLI implementation using the application's API.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import curses
|
import curses
|
||||||
import random
|
from datetime import datetime
|
||||||
import shutil
|
from typing import List
|
||||||
import string
|
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
import requests
|
||||||
from terminaltables import SingleTable
|
from terminaltables import SingleTable
|
||||||
|
|
||||||
def generateTable() -> SingleTable:
|
scores: List[dict] = []
|
||||||
tableData = [team() for _ in range(5)]
|
lastAttempt: float = -1
|
||||||
tableData.insert(0, ['Rank', 'ID', 'Team Name'] + list(map(str, range(1, 18))))
|
lastUpdate: datetime = None
|
||||||
return SingleTable(tableData)
|
|
||||||
|
|
||||||
|
|
||||||
def team():
|
def refreshScores() -> None:
|
||||||
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)]
|
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:
|
while True:
|
||||||
y, x = stdscr.getmaxyx()
|
# Get current terminal size and clear
|
||||||
stdscr.clear()
|
y, x = screen.getmaxyx()
|
||||||
|
screen.clear()
|
||||||
|
|
||||||
|
global scores
|
||||||
# Print Table to screen
|
# Print Table to screen
|
||||||
table = generateTable()
|
table = [[-1, team['id'], team['name'], team['total'], *team['scores']] for team in scores[:y - 4]]
|
||||||
for i, stringRow in enumerate(table.table.split('\n')):
|
table.insert(0, ['Rank', 'ID', 'Team Name', 'Total'])
|
||||||
stdscr.addstr(i, 0, stringRow[:x])
|
|
||||||
stdscr.addstr(y - 1, 0, str((x, y)))
|
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
|
# Check for quit key
|
||||||
key = stdscr.getch()
|
key = screen.getch()
|
||||||
if key == ord('q'):
|
if key == ord('q'):
|
||||||
break
|
break
|
||||||
|
|
||||||
stdscr.refresh()
|
screen.refresh()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
screen = curses.initscr()
|
stdscr = curses.initscr()
|
||||||
|
# Setup curses friendly terminal flags, run app
|
||||||
try:
|
try:
|
||||||
|
refreshScores()
|
||||||
curses.cbreak()
|
curses.cbreak()
|
||||||
screen.nodelay(1)
|
stdscr.nodelay(1)
|
||||||
curses.noecho()
|
curses.noecho()
|
||||||
screen.keypad(True)
|
curses.curs_set(False)
|
||||||
main(screen)
|
stdscr.keypad(True)
|
||||||
|
main(stdscr)
|
||||||
|
# Undo curses terminal options
|
||||||
finally:
|
finally:
|
||||||
screen.clear()
|
stdscr.clear()
|
||||||
curses.nocbreak()
|
curses.nocbreak()
|
||||||
curses.echo()
|
curses.echo()
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
Flask~=1.1.2
|
Flask~=1.1.2
|
||||||
APScheduler~=3.6.3
|
APScheduler~=3.6.3
|
||||||
|
windows-curses
|
||||||
Reference in New Issue
Block a user