Setup the main loop!

This commit is contained in:
Carlos Sanchez 2023-05-01 00:34:56 -04:00
parent 60cb4f816e
commit e6f14bdda3
4 changed files with 99 additions and 2 deletions

View File

@ -69,12 +69,17 @@ class ApiContext:
# check if only the given token is valid # check if only the given token is valid
def is_token_valid(self): def is_token_valid(self):
try: try:
return self.token and self.get("user/me") return self.token and self.user_me()
except Exception as ex: except Exception as ex:
self.logger.debug("Error from endpoint: %s" % ex) self.logger.debug("Error from endpoint: %s" % ex)
return False return False
# Return info about the current user based on the token. Useful to see if your token is valid
# and who you are
def user_me(self):
return self.get("user/me")
# Basic login endpoint, should return your token on success # Basic login endpoint, should return your token on success
def login(self, username, password, expire_seconds = False): def login(self, username, password, expire_seconds = False):
data = { data = {

61
main.py
View File

@ -4,6 +4,10 @@ import json
import logging import logging
import toml import toml
import getpass import getpass
import textwrap
import readchar
from colorama import Fore, Back, Style, init as colorama_init
import contentapi import contentapi
import myutils import myutils
@ -14,18 +18,69 @@ CONFIGFILE="config.toml"
config = { config = {
"api" : "https://oboy.smilebasicsource.com/api", "api" : "https://oboy.smilebasicsource.com/api",
"default_loglevel" : "WARNING", "default_loglevel" : "WARNING",
"default_room" : 0, # Zero means it will ask you for a room
"expire_seconds" : 31536000, # 365 days in seconds, expiration for token "expire_seconds" : 31536000, # 365 days in seconds, expiration for token
"appear_in_global" : False,
"tokenfile" : ".qcstoken" "tokenfile" : ".qcstoken"
} }
class WebsocketContext:
def __init__(self, api_context, user_info):
self.api_context = api_context
self.current_room = 0
self.user_info = user_info
self.connected = False # Should this be a var? IDK
self.pause
def main(): def main():
print("Program start") print("Program start")
colorama_init() # colorama init
load_or_create_global_config() load_or_create_global_config()
logging.info("Config: " + json.dumps(config, indent = 2)) logging.info("Config: " + json.dumps(config, indent = 2))
context = contentapi.ApiContext(config["api"], logging) context = contentapi.ApiContext(config["api"], logging)
logging.info("Testing connection to API at " + config["api"]) logging.info("Testing connection to API at " + config["api"])
logging.debug(json.dumps(context.api_status(), indent = 2)) logging.debug(json.dumps(context.api_status(), indent = 2))
authenticate(config, context) authenticate(config, context)
ws_context = WebsocketContext(context, context.user_me())
ws_context.current_room = config["default_room"]
# - Connect to websocket, be ready to receive junk
# - Alert user they're not connected to any room, maybe have a status line that lists controls + room
# - Enter input loop, but check room number on "input" mode, don't let messages send in room 0
# - h to help
# - s to search rooms, enter #1234 to connect directly, empty string to quit
# - g to list global users
# - u to list users in room
# - i to input
# - q to quit entirely
printstatus = True
# The infinite input loop! Or something!
while True:
if printstatus:
print_statusline(ws_context)
printstatus = True
key = readchar.readkey()
if key == "h":
print("not yet")
elif key == "s":
print("not yet")
elif key == "g":
print("not yet")
elif key == "u":
print("not yet")
elif key == "i":
print("not yet")
elif key == "q":
break
else:
printstatus = False
# TODO: Will the websocket end if the program just ends?
print("Program end") print("Program end")
# Loads the config from file into the global config var. If the file # Loads the config from file into the global config var. If the file
@ -81,6 +136,12 @@ def authenticate(config, context: contentapi.ApiContext):
print("ERROR: %s" % ex) print("ERROR: %s" % ex)
message = "Please try logging in again:" message = "Please try logging in again:"
def print_statusline(ws_context: WebsocketContext):
if ws_context.connected:
bg = Back.GREEN
else:
bg = Back.RED
print(bg + Fore.BLACK + "\n User: " + ws_context.user_info["username"] + " CTRL: h s g u i q " + Style.RESET_ALL)
# Because python reasons # Because python reasons
if __name__ == "__main__": if __name__ == "__main__":

31
scrolltest.py Normal file
View File

@ -0,0 +1,31 @@
import threading
import time
import sys
import curses
# from colorama import init, Fore, Style
# init() # initialize Colorama
# def print_and_scroll(msg):
# sys.stdout.write(Fore.GREEN + msg + Style.RESET_ALL + '\n')
# sys.stdout.flush()
#
# # function to print a message every 2 seconds
# def thread_function():
# while True:
# time.sleep(2)
# print_and_scroll("Another thread output")
#
# # start the thread
# t = threading.Thread(target=thread_function)
# t.start()
#
# # print the input prompt
# print_and_scroll("Enter input:")
#
# # wait for input
# user_input = input()
#
# # print the input
# print_and_scroll("You entered: " + user_input)

View File

@ -4,7 +4,7 @@ REM Change python whatever
set pyexe=python34\python.exe set pyexe=python34\python.exe
REM The list of packages this thing requires (maybe?) REM The list of packages this thing requires (maybe?)
set packages=requests colorama==0.4.1 websocket-client toml set packages=requests colorama==0.4.1 websocket-client toml readchar
REM Actually install the packages... maybe? REM Actually install the packages... maybe?
%pyexe% -m pip install %packages% %pyexe% -m pip install %packages%