From 53223dbef0906826fa0f97897ad9a1c969b5b803 Mon Sep 17 00:00:00 2001 From: William Patton Date: Fri, 11 Sep 2015 18:45:19 +0100 Subject: [PATCH] Update authentication system to pull credentials from DB inpreparation for multi-account support --- .gitignore | 0 LICENSE | 0 README.md | 0 bot.py | 28 +++++++++++++++++++--------- colors.py | 0 dbconnect.py | 24 +++++++++++++++--------- twitterfunctions.py | 0 7 files changed, 34 insertions(+), 18 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 bot.py mode change 100644 => 100755 colors.py mode change 100644 => 100755 dbconnect.py mode change 100644 => 100755 twitterfunctions.py diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/bot.py b/bot.py old mode 100644 new mode 100755 index ca1ba31..257d924 --- a/bot.py +++ b/bot.py @@ -15,6 +15,7 @@ parser.add_argument("--timetowait", help="time in seconds between how long to wait between loops", default="60", type=int ) parser.add_argument("--cacheoffset", help="time in seconds between mysql connections", default="300", type=int ) parser.add_argument("--notifyonruns", help="issue a message after x runs to show program is still active", default="60", type=int ) +parser.add_argument("--user", help="user credentials to use", default="default", type=str ) args = parser.parse_args() @@ -25,15 +26,6 @@ CACHEOFFSET = args.cacheoffset NOTIFYONRNS = args.notifyonruns -## enter the corresponding information from your Twitter application: -CONSUMER_KEY = '123456789'#keep the quotes, replace this with your consumer key -CONSUMER_SECRET = '123456789'#keep the quotes, replace this with your consumer secret key -ACCESS_KEY = '123456789'#keep the quotes, replace this with your access token -ACCESS_SECRET = '123456789'#keep the quotes, replace this with your access token secret -auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) -auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) -api = tweepy.API(auth) - # enter database connection information dbconfig = { 'user': 'bot-twitter', @@ -43,6 +35,24 @@ 'raise_on_warnings': True, } +authcnx = dbconnect.dbconnect(dbconfig) +authcursor = dbconnect.dbcursor(authcnx) + +getKeySecretQuery = ("SELECT CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET FROM Accounts WHERE user = %r" % (args.user)) + +gotKeySecretResult = authcursor.execute(getKeySecretQuery) + +KeySecretResult = authcursor.fetchall() + +for (CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET) in KeySecretResult : + + THE_CONSUMER_KEY = CONSUMER_KEY + THE_CONSUMER_SECRET = CONSUMER_SECRET + THE_ACCESS_KEY = ACCESS_KEY + THE_ACCESS_SECRET = ACCESS_SECRET + +api = twitterfunctions.authenticatetwitter(THE_CONSUMER_KEY, THE_CONSUMER_SECRET, THE_ACCESS_KEY, THE_ACCESS_SECRET) + # this is the main function of the program # accepts a databse configuiration and a time to wait between loops def runner(dbconfig, waitTime): diff --git a/colors.py b/colors.py old mode 100644 new mode 100755 diff --git a/dbconnect.py b/dbconnect.py old mode 100644 new mode 100755 index 37e7012..b75ac47 --- a/dbconnect.py +++ b/dbconnect.py @@ -10,17 +10,23 @@ from mysql.connector import errorcode def dbconnect(config): - + # note: returning cnx effectively closes the connection try: - cnx = mysql.connector.connect(**config) + cnx = mysql.connector.connect(**config) except mysql.connector.Error as err: - if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: - print("Something is wrong with your user name or password") - elif err.errno == errorcode.ER_BAD_DB_ERROR: - print("Database does not exist") - else: - print(err) + if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: + print("Something is wrong with your user name or password") + elif err.errno == errorcode.ER_BAD_DB_ERROR: + print("Database does not exist") + else: + print(err) else: - return cursor + return cnx + +def dbcursor(cnx): + + cursor = cnx.cursor() + return cursor + diff --git a/twitterfunctions.py b/twitterfunctions.py old mode 100644 new mode 100755