protovac/main.py

246 lines
8.1 KiB
Python
Raw Normal View History

2022-09-01 20:05:54 +00:00
#!/home/tanner/protovac/env/bin/python
2022-09-01 07:32:53 +00:00
2022-09-01 20:05:54 +00:00
import curses
2022-09-01 01:34:44 +00:00
import requests
2022-09-01 21:00:56 +00:00
import pytz
from datetime import datetime
2022-09-01 01:34:44 +00:00
ENTER_KEY = 10
BACKSPACE_KEY = 263
ESCAPE_KEY = 27
2022-09-01 21:00:56 +00:00
TIMEZONE_CALGARY = pytz.timezone('America/Edmonton')
def format_date(datestr):
d = datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.UTC)
d = d.astimezone(TIMEZONE_CALGARY)
return d.strftime('%a %b %-d, %Y %-I:%M %p')
2022-09-01 20:05:54 +00:00
2022-09-01 01:34:44 +00:00
def sign_send(to_send):
try:
data = dict(sign=to_send, on_behalf_of='protovac')
r = requests.post('https://api.my.protospace.ca/stats/sign/', data=data, timeout=5)
r.raise_for_status()
return 'Success!'
except:
return 'Error.'
def fetch_stats():
try:
r = requests.get('https://api.my.protospace.ca/stats/', timeout=5)
r.raise_for_status()
return r.json()
except:
return 'Error.'
2022-09-01 21:00:56 +00:00
def fetch_classes():
try:
r = requests.get('https://api.my.protospace.ca/sessions/', timeout=5)
r.raise_for_status()
return r.json()
except:
return 'Error.'
2022-09-01 01:34:44 +00:00
skip_input = False
current_screen = 'home'
2022-09-01 20:05:54 +00:00
prev_screen = current_screen
2022-09-01 01:34:44 +00:00
c = 0
# highlighting:
#wattron(menu_win, A_REVERSE)
#mvwaddstr(menu_win, y, x, choices[i])
#wattroff(menu_win, A_REVERSE)
2022-09-01 20:05:54 +00:00
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.curs_set(0)
2022-09-01 01:34:44 +00:00
sign_to_send = ''
stats = {}
2022-09-01 21:00:56 +00:00
classes = {}
classes_start = 0
2022-09-01 01:34:44 +00:00
while True:
if current_screen == 'home':
2022-09-01 20:05:54 +00:00
stdscr.addstr(1, 1, ' _______ _______ ___ _________ ___ ____ ____ _ ______ ')
stdscr.addstr(2, 1, '|_ __ \|_ __ \ .\' `. | _ _ | .\' `.|_ _| |_ _|/ \ .\' ___ |')
stdscr.addstr(3, 1, ' | |__) | | |__) | / .-. \|_/ | | \_|/ .-. \ \ \ / / / _ \ / .\' \_|')
stdscr.addstr(4, 1, ' | ___/ | __ / | | | | | | | | | | \ \ / / / ___ \ | | ')
stdscr.addstr(5, 1, ' _| |_ _| | \ \_\ `-\' / _| |_ \ `-\' / \ \' /_/ / \ \_\ `.___.\'\\')
stdscr.addstr(6, 1, '|_____| |____| |___|`.___.\' |_____| `.___.\' \_/|____| |____|`.____ .\'')
stdscr.addstr(9, 4, '[T] Stats')
stdscr.addstr(11, 4, '[S] Sign')
2022-09-01 21:00:56 +00:00
stdscr.addstr(13, 4, '[C] Classes')
2022-09-01 20:05:54 +00:00
#stdscr.addstr(10, 4, '[F] Forum')
stdscr.addstr(23, 1, ' Copyright (c) 1985 Bikeshed Computer Systems Corp.')
stdscr.clrtoeol()
stdscr.refresh()
2022-09-01 01:34:44 +00:00
elif current_screen == 'debug':
2022-09-01 21:00:56 +00:00
stdscr.addstr(0, 1, 'PROTOVAC')
2022-09-01 20:05:54 +00:00
stdscr.addstr(3, 1, 'Debug Mode')
stdscr.addstr(4, 1, '==========')
stdscr.addstr(6, 1, str.format('Character pressed = {0}', c))
stdscr.clrtoeol()
stdscr.addstr(23, 1, '[B] Back')
stdscr.clrtoeol()
stdscr.refresh()
2022-09-01 01:34:44 +00:00
elif current_screen == 'stats':
2022-09-01 21:00:56 +00:00
stdscr.addstr(0, 1, 'PROTOVAC')
stdscr.addstr(2, 1, 'Protospace Stats')
stdscr.addstr(3, 1, '================')
2022-09-01 01:34:44 +00:00
if stats:
2022-09-01 21:00:56 +00:00
stdscr.addstr(5 , 1, 'Next meeting: {}'.format(format_date(stats['next_meeting'])))
stdscr.addstr(7 , 1, 'Next clean: {}'.format(format_date(stats['next_clean'])))
stdscr.addstr(9, 1, 'Next class: {}'.format(stats['next_class']['name']))
stdscr.addstr(10, 1, ' {}'.format(format_date(stats['next_class']['datetime'])))
stdscr.addstr(12, 1, 'Last class: {}'.format(stats['prev_class']['name']))
stdscr.addstr(13, 1, ' {}'.format(format_date(stats['prev_class']['datetime'])))
stdscr.addstr(15, 1, 'Member count: {} Green: {} Paused / expired: {}'.format(
2022-09-01 01:34:44 +00:00
stats['member_count'],
stats['green_count'],
stats['paused_count'],
))
2022-09-01 21:00:56 +00:00
stdscr.addstr(17, 1, 'Card scans: {}'.format(stats['card_scans']))
2022-09-01 01:34:44 +00:00
else:
2022-09-01 21:00:56 +00:00
stdscr.addstr(5, 1, 'Loading...')
2022-09-01 01:34:44 +00:00
2022-09-01 20:05:54 +00:00
stdscr.addstr(23, 1, '[B] Back')
stdscr.clrtoeol()
stdscr.refresh()
2022-09-01 01:34:44 +00:00
if not stats:
stats = fetch_stats()
2022-09-01 21:00:56 +00:00
stdscr.erase()
skip_input = True
elif current_screen == 'classes':
stdscr.addstr(0, 1, 'PROTOVAC')
stdscr.addstr(2, 1, 'Protospace Classes')
stdscr.addstr(3, 1, '==================')
if classes:
classes_in_view = classes['results'][classes_start:6+classes_start]
lines = []
for session in classes_in_view:
lines.append(session['course_data']['name'])
lines.append('{} Instructor: {} Cost: {} Students: {}'.format(
format_date(session['datetime']),
'Protospace' if session['course_data']['id'] in [413, 317, 273] else session['instructor_name'],
'Free' if session['cost'] == '0.00' else '$' + session['cost'],
str(session['student_count']) + (' / ' + str(session['max_students']) if session['max_students'] else ''),
))
lines.append('')
offset = 5
for num, line in enumerate(lines):
stdscr.addstr(num + offset, 1, line)
else:
stdscr.addstr(5, 1, 'Loading...')
stdscr.addstr(23, 1, '[B] Back [J] Down [K] Up')
stdscr.clrtoeol()
stdscr.refresh()
if not classes:
classes = fetch_classes()
stdscr.erase()
2022-09-01 01:34:44 +00:00
skip_input = True
elif current_screen == 'sign':
2022-09-01 21:00:56 +00:00
stdscr.addstr(0, 1, 'PROTOVAC')
stdscr.addstr(2, 1, 'Protospace Sign')
stdscr.addstr(3, 1, '===============')
stdscr.addstr(5, 1, 'Send a message to the sign in the welcome room and classroom.')
stdscr.addstr(6, 1, 'After sending, turn your head right and wait.')
2022-09-01 01:34:44 +00:00
if sign_to_send:
2022-09-01 21:00:56 +00:00
stdscr.addstr(8, 4, sign_to_send)
2022-09-01 20:05:54 +00:00
stdscr.clrtoeol()
stdscr.addstr(23, 1, '[ENTER] Send [ESC] Cancel')
2022-09-01 01:34:44 +00:00
else:
2022-09-01 21:00:56 +00:00
stdscr.addstr(8, 4, '[E] Edit message')
2022-09-01 20:05:54 +00:00
stdscr.addstr(23, 1, '[B] Back')
stdscr.clrtoeol()
stdscr.refresh()
2022-09-01 01:34:44 +00:00
2022-09-01 20:05:54 +00:00
stdscr.move(23, 79)
2022-09-01 01:34:44 +00:00
if skip_input:
skip_input = False
else:
try:
2022-09-01 20:05:54 +00:00
c = stdscr.getch()
2022-09-01 01:34:44 +00:00
except KeyboardInterrupt:
break
2022-09-01 20:05:54 +00:00
if c == curses.KEY_UP:
2022-09-01 01:34:44 +00:00
pass
2022-09-01 20:05:54 +00:00
elif c == curses.KEY_DOWN:
2022-09-01 01:34:44 +00:00
pass
elif c == 10: # ENTER is pressed
pass
button = chr(c).lower()
if current_screen == 'home':
if button == 't':
current_screen = 'stats'
elif button == 's':
current_screen = 'sign'
2022-09-01 21:00:56 +00:00
elif button == 'c':
current_screen = 'classes'
2022-09-01 01:34:44 +00:00
elif button == 'd':
current_screen = 'debug'
elif current_screen == 'stats':
if button == 'b':
current_screen = 'home'
stats = {}
2022-09-01 21:00:56 +00:00
elif current_screen == 'classes':
if button == 'b':
current_screen = 'home'
classes = {}
classes_start = 0
elif button == 'j':
classes_start += 1
stdscr.erase()
elif button == 'k':
if classes_start > 0:
classes_start -= 1
stdscr.erase()
2022-09-01 01:34:44 +00:00
elif current_screen == 'sign':
if sign_to_send:
if c == BACKSPACE_KEY:
sign_to_send = sign_to_send[:-2] + '_'
elif c == ESCAPE_KEY:
sign_to_send = ''
2022-09-01 21:00:56 +00:00
stdscr.erase()
2022-09-01 01:34:44 +00:00
elif c == ENTER_KEY:
if len(sign_to_send) > 1:
2022-09-01 20:05:54 +00:00
stdscr.addstr(15, 4, 'Sending...')
stdscr.refresh()
2022-09-01 01:34:44 +00:00
sign_send(sign_to_send[:-1])
2022-09-01 20:05:54 +00:00
stdscr.erase()
2022-09-01 01:34:44 +00:00
sign_to_send = ''
else:
if c < 127 and c > 31:
sign_to_send = sign_to_send[:-1] + chr(c) + '_'
elif button == 'b':
current_screen = 'home'
elif button == 'e':
sign_to_send = '_'
2022-09-01 20:05:54 +00:00
if current_screen != prev_screen:
prev_screen = current_screen
stdscr.erase()
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()