Praxis_Bot/credentials.py
Alex Orid ada9d2ecb8 Added DB Module to the Twitch Module
Expanded main() a little to add DB_Module to the Twitch_Module.

Added a db_manager to the Twitch_Module class.

Added the nickname string to the db credential.
2020-09-23 15:01:15 -04:00

125 lines
4.4 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, nickname, username, password, ipAddress, port, databaseName, engine_url):
#super().__init__()
self.nickname = nickname
self.username = username
self.password = password
self.ipAddress = ipAddress
self.port = port
self.databaseName = databaseName
self.engine_url = 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
return 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)
self.Twitch_Credentials_List.append(output)
if file.lower().find("discord") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.Discord_Credential)
output = credential_loading_function(self, file)
self.Discord_Credentials_List.append(output)
if file.lower().find("db") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.DB_Credential)
output = credential_loading_function(self, file)
self.DB_Credentials_List.append(output)
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):
file_path = os.path.join(self.get_credentials_dir(), fileName)
f = open(file_path)
raw_json = json.loads(f.read())
tobj = DB_Credential(**raw_json)
return tobj
def find_Credential(self):
pass
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()