-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweeter.py
58 lines (49 loc) · 2.63 KB
/
tweeter.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
'''
Module that takes the data from COVanal and puts them into daily tweets.
Requires a developer twitter account to obtain access to the API values
below.
'''
import os
import datetime as dt
import tweepy
import COVanal as CV
#twitter API settings, this are from the user's twitter developer account
#Set as environment variables here, but can just set equal to the string
consumer_key = os.environ.get('COVID_CONSUMER_KEY')
consumer_secret = os.environ.get('COVID_CONSUMER_SECRET')
access_token = os.environ.get('COVID_ACCESS_TOKEN')
access_token_secret = os.environ.get('COVID_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def dailytweets():
'''Twitterbot that publishes COVID-19 data
Meant to be run once a day that runs COVID data analysis modules
and then posts the results as tweets. It also deletes the files
following creation.
'''
#Runs the functions from the COVanal module to generate the tables and graphs to be published
CV.create_daily_top_10_tables()
CV.create_daily_top_10_changes_tables()
CV.create_daily_totals_graphs()
CV.create_daily_US_graphs()
#Tweet 1 - file to be tweeted, actual tweet with text, delete figure
tweettopublish1 = dt.date.today().strftime('%m%d%y') + 'total.png'
api.update_with_media(tweettopublish1, f'COVID-19 Global Top 10 totals ({dt.date.today()}) #COVID #COV #Data #CoronaVirus')
os.remove(dt.date.today().strftime('%m%d%y') + 'total.png')
#Tweet 2 - file to be tweeted, actual tweet with text, delete figure
tweettopublish2 = dt.date.today().strftime('%m%d%y') + 'daily.png'
api.update_with_media(tweettopublish2, f'COVID-19 Global Top 10 changes over 24 hours ({dt.date.today()}) #COVID #COV #Data #CoronaVirus')
os.remove(dt.date.today().strftime('%m%d%y') + 'daily.png')
#Tweet 3 - file to be tweeted, actual tweet with text, delete figure
tweettopublish3 = dt.date.today().strftime('%m%d%y') + 'dailytotalgraph.png'
api.update_with_media(tweettopublish3, f'COVID-19 Global Total Graphs ({dt.date.today()}) #COVID #COV #Data #CoronaVirus')
os.remove(dt.date.today().strftime('%m%d%y') + 'dailytotalgraph.png')
#Tweet 4 - file to be tweeted, actual tweet with text, delete figure
tweettopublish4 = dt.date.today().strftime('%m%d%y') + 'dailyUSgraph.png'
api.update_with_media(tweettopublish4, f'COVID-19 US Total Graphs ({dt.date.today()}) #COVID #COV #Data #CoronaVirus')
os.remove(dt.date.today().strftime('%m%d%y') + 'dailyUSgraph.png')
def main():
dailytweets()
if __name__ == '__main__':
main()