-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathtweet.py
77 lines (58 loc) · 2.12 KB
/
tweet.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
tweet.py: post tweets to twitter.com
11 September 2020
Vicki Langer (@vicki_langer)
'''
import tweepy # type: ignore
import time
import random
from os import environ, remove
from get_tweet_content import get_tweet_content
from get_img_for_tweet import get_img_for_tweet
from get_reply import get_reply
consumer_key = environ['consumer_key']
consumer_secret = environ['consumer_secret']
access_token = environ['access_token']
access_token_secret = environ['access_token_secret']
def authenticate_api():
try:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
except Exception as error:
print(f"An error occurred when attempting to authenticate with the twitter API. reason: {error}")
def main():
api = authenticate_api()
# choose tweet
print("finding a tweet...")
tweet_content = get_tweet_content()
print("chose tweet: " + tweet_content["text"])
try:
# obtain image
image_path = get_img_for_tweet(tweet_content["text"])
# prepare image
image = api.media_upload(image_path)
# add alt-text
api.create_media_metadata(
media_id=image.media_id, alt_text=tweet_content["text"])
except Exception as error:
print(f"An error occurred while generating image. reason: {error}")
# Post tweet
tweet = api.update_status(status=tweet_content["text"],
media_ids=[image.media_id]) # variable used later for reply to this tweet
print('tweet has been tweeted')
# delete image once posted
remove(image_path)
# Reply to tweet
# Get reply
reply_with = get_reply(tweet_content["label"])
# Check if a reply was returned
if (reply_with):
api.update_status(status=reply_with, in_reply_to_status_id=tweet.id,
auto_populate_reply_metadata=True)
print('chose reply:' + reply_with)
print('reply has been tweeted')
else:
print(f"Don't have a definition for {tweet_content['label']}")
if __name__ == "__main__":
main()