Merge pull request 'implemented quick and easy way for storing credentials in json files. Most of the stuff in here is template ideas, season them to taste for yourself' (#2) from class_credentials_loader into credentials-class-idea

Reviewed-on: #2
Awesome, thanks.
This commit is contained in:
alex_orid 2020-09-22 22:04:14 +00:00
commit 97166feaf0
5 changed files with 101 additions and 61 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
tts/
__pycache__/
credentials/
.idea/

View File

@ -0,0 +1,6 @@
{
"username": "discord",
"helix": "secret discord",
"oauth": "token discord",
"v5_client": "I have no idea here"
}

View File

@ -0,0 +1,6 @@
{
"username": "something",
"helix": "secret",
"oauth": "token",
"v5_client": "I have no idea here"
}

View File

@ -1,48 +1,6 @@
import json
import os
class Credentials_Module():
def __init__(self):
super().__init__()
self.Twitch_Credentials_List:list
self.Discord_Credentials_List:list
self.DB_Credentials_List:list
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
}
def load_credentials(self):
credentialPath = self.get_credentials_dir()
fileList:list = []
for file in fileList:
fileName = ""
if fileName.lower().find("twitch") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.Twitch_Credential)
output = credential_loading_function(fileName)
if fileName.lower().find("discord") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.Discord_Credential)
output = credential_loading_function(fileName)
if fileName.lower().find("db") != -1:
credential_loading_function = self.credentialLoadingFunctions.get(Credential.DB_Credential)
output = credential_loading_function(fileName)
#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):
pass
def load_Discord_Credential(self, fileName:str):
pass
def load_DB_Credential(self, fileName:str):
pass
from enum import Enum
class Credential(Enum):
@ -50,23 +8,29 @@ class Credential(Enum):
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):
super().__init__()
username = ""
helix = ""
oauth = "oauth:"
v5_Client = ""
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):
super().__init__()
username = ""
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"
@ -82,3 +46,67 @@ 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
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()

View File

@ -4,7 +4,7 @@ import twitch
testValidUrls = ['https://shady.ru', 'http://stolencards.zn', 'https://i.imgur.com/FL6slHd.jpg']
testInvalidUrls = ['this is just a sentence. With a period', 'gotta have some other stuff', 'bad punctuation.does not produces false positives']
testInvalidUrls = ['this is just a sentence. With a period', 'gotta have some other stuff', 'bad punctuation. does produces false positives']
class TwitchBotTest(unittest.TestCase):