Skip to content

Commit

Permalink
Update authentication system to pull credentials from DB
Browse files Browse the repository at this point in the history
	inpreparation for multi-account support
  • Loading branch information
pattonwebz committed Sep 11, 2015
1 parent 3ff1466 commit 53223db
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
28 changes: 19 additions & 9 deletions bot.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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',
Expand All @@ -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):
Expand Down
Empty file modified colors.py
100644 → 100755
Empty file.
24 changes: 15 additions & 9 deletions dbconnect.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

Empty file modified twitterfunctions.py
100644 → 100755
Empty file.

0 comments on commit 53223db

Please sign in to comment.