-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (36 loc) · 1.22 KB
/
main.py
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
import random
import time
import json
import requests
import tweepy
import os
consumer_key = os.environ.get('API_KEY')
consumer_secret_key = os.environ.get('API_SECRET_KEY')
access_token = os.environ.get('ACCESS_TOKEN')
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')
def get_affirmations():
response = requests.get('https://iaffirm.herokuapp.com/affirmations').text
affirmation = json.loads(response)
return (affirmation["affirmations"])
def get_random_affirmation():
affirmations = get_affirmations()
random_affirmation = random.randint(0, len(affirmations) - 1)
return (affirmations[random_affirmation]['text'])
def create_tweet():
affirmation = get_random_affirmation()
tweet = (affirmation)
return tweet
def tweet_message():
interval = 60 * 30
auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
while True:
try:
tweet = create_tweet()
api.update_status(tweet + " #iAffirm")
time.sleep(interval)
except tweepy.TweepError as e:
print(e.reason)
if __name__ == "__main__":
tweet_message()