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

added .idea to .gitignore
This commit is contained in:
dtookey 2020-09-22 01:38:35 -04:00
parent 9f449a916c
commit c2113116cd
5 changed files with 101 additions and 61 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
tts/ tts/
__pycache__/ __pycache__/
credentials/ 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 import os
from enum import Enum
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
class Credential(Enum): class Credential(Enum):
@ -50,26 +8,32 @@ class Credential(Enum):
Discord_Credential = 2 Discord_Credential = 2
DB_Credential = 3 DB_Credential = 3
class Twitch_Credential(): class Twitch_Credential():
#Username = Twitch Username # Username = Twitch Username
#Helix ID = https://dev.twitch.tv/console/apps # Helix ID = https://dev.twitch.tv/console/apps
#Oauth = https://twitchapps.com/tmi/ # Oauth = https://twitchapps.com/tmi/
#V5 Client ID = https://twitchtokengenerator.com/ # V5 Client ID = https://twitchtokengenerator.com/
def __init__(self): def __init__(self, username, helix, oauth, v5_client):
super().__init__() # super().__init__()
username = "" self.username = username
helix = "" self.helix = helix
oauth = "oauth:" self.oauth = oauth
v5_Client = "" self.v5_client = v5_client
class Discord_Credential(): class Discord_Credential():
#Discord Credentials explanations here. # Discord Credentials explanations here.
def __init__(self): def __init__(self, username, helix, oauth, v5_client):
super().__init__() # super().__init__()
username = "" # 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(): class DB_Credential():
#engine = "mysql+mysqlconnector://root:password@localhost:3306/DatabaseName" # engine = "mysql+mysqlconnector://root:password@localhost:3306/DatabaseName"
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.username = "" self.username = ""
@ -82,3 +46,67 @@ class DB_Credential():
def create_engine_url(self): def create_engine_url(self):
new_engine_url = "mysql+mysqlconnector://" + self.username + ":" + self.password + "@" + self.ipAddress + ":" + self.port + "/" + self.databaseName new_engine_url = "mysql+mysqlconnector://" + self.username + ":" + self.password + "@" + self.ipAddress + ":" + self.port + "/" + self.databaseName
self.engine_url = new_engine_url 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'] 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): class TwitchBotTest(unittest.TestCase):