-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (61 loc) · 2.37 KB
/
Copy pathutils.py
File metadata and controls
73 lines (61 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 27 10:29:25 2017
@author: Sarai
"""
import os
import pymysql
import tweepy
from flask import session
consumer_key = os.environ['consumer_key']
consumer_secret = os.environ['consumer_secret']
def db_connect():
# Connect to the database
# The connection will be sent to crud.py methods as needed.
try:
connection = pymysql.connect(host = os.environ['host'],
user = os.environ['user'],
password = os.environ['password'],
db = os.environ['database'],
charset = 'utf8mb4',
cursorclass = pymysql.cursors.DictCursor)
return connection
except BaseException as e:
print("Error in db_connect():", e)
raise OSError("Cannot connect to database.")
return
def get_api():
# Rebuild OAuthHandler and return tweepy.API(auth) if session exists.
# Otherwise, return error_msg.
try:
# Build OAuthHandler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(session['key'], session['secret'])
return tweepy.API(auth)
except tweepy.TweepError:
print('Error in get_api(): Failed to build OAuthHandler!')
raise OSError("Cannot connect to Twitter API.")
return
def get_user_api(twitter_id):
# Rebuild OAuthHandler and return tweepy.API(auth) if session exists.
# Otherwise, return error_msg.
try:
# Fetch key and secret
connection = db_connect()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `screen_name`, `twitter_id`,"
sql += " `oauth_key`, `oauth_secret`"
sql += " FROM `users`"
sql += " WHERE twitter_id = %s"
cursor.execute(sql, (twitter_id,))
result = cursor.fetchone()
connection.close()
# Build OAuthHandler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(result['oauth_key'], result['oauth_secret'])
return tweepy.API(auth)
except tweepy.TweepError:
print('Error in get_user_api(): Failed to build OAuthHandler!')
raise OSError("Cannot connect to Twitter API.")
return