From e6f14bdda34e261299ed50a4bb291459568c4f53 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Mon, 1 May 2023 00:34:56 -0400 Subject: [PATCH] Setup the main loop! --- contentapi.py | 7 +++++- main.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ scrolltest.py | 31 ++++++++++++++++++++++++++ setup.bat | 2 +- 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 scrolltest.py diff --git a/contentapi.py b/contentapi.py index 5e6d5c9..c3c1f9e 100644 --- a/contentapi.py +++ b/contentapi.py @@ -69,12 +69,17 @@ class ApiContext: # check if only the given token is valid def is_token_valid(self): try: - return self.token and self.get("user/me") + return self.token and self.user_me() except Exception as ex: self.logger.debug("Error from endpoint: %s" % ex) 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 def login(self, username, password, expire_seconds = False): data = { diff --git a/main.py b/main.py index 00f1fb1..a14432a 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,10 @@ import json import logging import toml import getpass +import textwrap +import readchar + +from colorama import Fore, Back, Style, init as colorama_init import contentapi import myutils @@ -14,18 +18,69 @@ CONFIGFILE="config.toml" config = { "api" : "https://oboy.smilebasicsource.com/api", "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 + "appear_in_global" : False, "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(): print("Program start") + colorama_init() # colorama init load_or_create_global_config() logging.info("Config: " + json.dumps(config, indent = 2)) context = contentapi.ApiContext(config["api"], logging) logging.info("Testing connection to API at " + config["api"]) logging.debug(json.dumps(context.api_status(), indent = 2)) 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") # 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) 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 if __name__ == "__main__": diff --git a/scrolltest.py b/scrolltest.py new file mode 100644 index 0000000..54eee26 --- /dev/null +++ b/scrolltest.py @@ -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) + diff --git a/setup.bat b/setup.bat index d2ba89e..1c1d0b5 100644 --- a/setup.bat +++ b/setup.bat @@ -4,7 +4,7 @@ REM Change python whatever set pyexe=python34\python.exe 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? %pyexe% -m pip install %packages% \ No newline at end of file