Praxis_Bot/credentials.py

113 lines
3.9 KiB
Python

import json
import os
from enum import Enum
class Credential(Enum):
Twitch_Credential = 1
Discord_Credential = 2
DB_Credential = 3
class Twitch_Credential():
# Username = Twitch Username
# Helix ID = https://dev.twitch.tv/console/apps
# Oauth = https://twitchapps.com/tmi/
# V5 Client ID = https://twitchtokengenerator.com/
def __init__(self, username, helix, oauth, v5_client):
# super().__init__()
self.username = username
self.helix = helix
self.oauth = oauth
self.v5_client = v5_client
class Discord_Credential():
# Discord Credentials explanations here.
def __init__(self, username, helix, oauth, v5_client):
# super().__init__()
# all of this is completely made up, i just wanted to make sure your file name switch worked right
self.username = username
self.helix = helix
self.oauth = oauth
self.v5_client = v5_client
class DB_Credential():
# engine = "mysql+mysqlconnector://root:password@localhost:3306/DatabaseName"
def __init__(self):
super().__init__()
self.username = ""
self.password = ""
self.ipAddress = ""
self.port = ""
self.databaseName = ""
self.engine_url = ""
def create_engine_url(self):
new_engine_url = "mysql+mysqlconnector://" + self.username + ":" + self.password + "@" + self.ipAddress + ":" + self.port + "/" + self.databaseName
self.engine_url = new_engine_url
class Credentials_Module():
def __init__(self):
super().__init__()
self.Twitch_Credentials_List: list
self.Discord_Credentials_List: list
self.DB_Credentials_List: list
def load_credentials(self):
fileList = self.list_credential_files()
for file in fileList:
if file.lower().find("twitch") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.Twitch_Credential)
output = credential_loading_function(self, file)
if file.lower().find("discord") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.Discord_Credential)
output = credential_loading_function(self, file)
if file.lower().find("db") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.DB_Credential)
output = credential_loading_function(self, file)
def list_credential_files(self):
credentialPath = self.get_credentials_dir()
fileList: list = []
for dirName, subdirList, fileList in os.walk(credentialPath):
break
return fileList
# Based on similar function in tts.py
def get_credentials_dir(self):
dir = os.path.join(os.getcwd(), "credentials") # this is platform-agnostic
if not os.path.exists(dir):
os.mkdir(dir)
return dir
def load_Twitch_Credential(self, fileName: str):
file_path = os.path.join(self.get_credentials_dir(), fileName)
f = open(file_path)
raw_json = json.loads(f.read())
tobj = Twitch_Credential(**raw_json)
return tobj
def load_Discord_Credential(self, fileName: str):
file_path = os.path.join(self.get_credentials_dir(), fileName)
f = open(file_path)
raw_json = json.loads(f.read())
tobj = Discord_Credential(**raw_json)
return tobj
def load_DB_Credential(self, fileName: str):
return None
credentialLoadingFunctions = { # this is a mapping of the Credential enum to function pointers
Credential.Twitch_Credential: load_Twitch_Credential,
Credential.Discord_Credential: load_Discord_Credential,
Credential.DB_Credential: load_DB_Credential
}
if __name__ == '__main__':
creds = Credentials_Module()
creds.load_credentials()