From ada9d2ecb86e3f6f4612bfbe59caf45086617336 Mon Sep 17 00:00:00 2001 From: Alex Orid Date: Wed, 23 Sep 2020 15:01:15 -0400 Subject: [PATCH] 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. --- .../credential_template_db.json | 1 + credentials.py | 7 ++++++- db.py | 20 +++++++++++++++---- main.py | 15 +++++++++++--- twitch_script.py | 6 +++++- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/credential_templates/credential_template_db.json b/credential_templates/credential_template_db.json index 3e25cc8..5500814 100644 --- a/credential_templates/credential_template_db.json +++ b/credential_templates/credential_template_db.json @@ -1,4 +1,5 @@ { + "nickname": "AnythingYouWant", "username": "", "password": "", "ipAddress": "", diff --git a/credentials.py b/credentials.py index 9e7017e..07b28c7 100644 --- a/credentials.py +++ b/credentials.py @@ -34,8 +34,9 @@ class Discord_Credential(): class DB_Credential(): # engine = "mysql+mysqlconnector://root:password@localhost:3306/DatabaseName" - def __init__(self, username, password, ipAddress, port, databaseName, engine_url): + 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 @@ -46,6 +47,7 @@ class DB_Credential(): 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(): @@ -106,6 +108,9 @@ class Credentials_Module(): 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, diff --git a/db.py b/db.py index 253afb1..f4fe217 100644 --- a/db.py +++ b/db.py @@ -1,4 +1,6 @@ -import db_cred as db_credentials +import credentials + +import config as config import pandas as pd from sqlalchemy import create_engine @@ -7,12 +9,22 @@ from sqlalchemy import create_engine class db_module(): def __init__(self): super().__init__() + self.dbCredential: credentials.DB_Credential self.currentWorkingDB: str self.engine = None - def setup_engine(self): - self.engine = create_engine(db_credentials.engine_url) - print("Engine Created") + def setup_engine(self, credential: credentials.DB_Credential): + createEngine = True + if credential is None: + if self.dbCredential is None: + createEngine = False + else: + credential = self.dbCredential + + if createEngine: + self.engine = create_engine(credential.engine_url) + self.currentWorkingDB = credential.databaseName + print("Engine Created") def create_table(self, tableName: str = ""): pass diff --git a/main.py b/main.py index 62e4179..bda265c 100644 --- a/main.py +++ b/main.py @@ -22,18 +22,27 @@ def main(): twitch_chat = twitch_script.Twitch_Module() + dbCert: credentials.DB_Credential + if credentials_manager.DB_Credentials_List is not None: + for cert in credentials_manager.DB_Credentials_List: + if cert.nickname == "praxis_bot": + print("DB Certificate Found: {" + cert.nickname + "}") + dbCert = cert + + twitchCert: credentials.Twitch_Credential if credentials_manager.Twitch_Credentials_List is not None: for cert in credentials_manager.Twitch_Credentials_List: if cert.username == "praxis_bot": print("Twitch Certificate Found: {" + cert.username + "}") - mainCert = cert + twitchCert = cert + twitch_chat.db_manager.setup_engine(dbCert) print("Loading Credential: {" + cert.username + "} into Twitch_Module") - twitch_chat.twitchCredential = mainCert + twitch_chat.twitchCredential = twitchCert print("Connecting to Channel: " + "thecuriousnerd") twitch_chat.join_channel(None, "thecuriousnerd") - #twitch_chat.send_message("activated") + # twitch_chat.send_message("activated") if __name__ == "__main__": diff --git a/twitch_script.py b/twitch_script.py index a25189b..ccc5156 100644 --- a/twitch_script.py +++ b/twitch_script.py @@ -5,7 +5,7 @@ import twitch import twitch.chat import config as config -#import db +import db import tts import credentials @@ -14,7 +14,11 @@ class Twitch_Module(): def __init__(self): super().__init__() self.twitchCredential: credentials.Twitch_Credential + self.dbCredential: credentials.DB_Credential + + self.db_manager: db.db_module = db.db_module() self.chat: twitch.Chat + self.tts_enabled: bool = False self.tts_whitelist_enabled: bool = False self.links_allowed: bool = True