forked from brittneypostma/zerotomastery-twitterbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
38 lines (28 loc) · 1.12 KB
/
config.py
File metadata and controls
38 lines (28 loc) · 1.12 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
from os import getenv
import tweepy
def create_api():
"""This function collects the consumer and access keys, creates an api object and returns the authenticated api
object.
:return: authenticated api object
"""
consumer_key = getenv('CONSUMER_KEY')
consumer_secret = getenv('CONSUMER_SECRET')
access_token = getenv('ACCESS_TOKEN')
access_token_secret = getenv('ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
return authenticate_api(api)
def authenticate_api(api):
"""This function verifies the credentials and returns the authenticated api object
:param api: api object
:return: authenticated api object
:raises TweepError: raised due to an error Twitter Responded with or raised when an API method fails due to hitting
Twitter’s rate limit.
"""
try:
api.verify_credentials()
except tweepy.TweepError as error:
raise error
print("API created")
return api