-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter_bot.py
186 lines (165 loc) · 6.95 KB
/
twitter_bot.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import tweepy
import os
from datetime import date, timedelta
bearer = os.environ['TWIBEARER']
accessa = os.environ['TWIACCTOK']
accessb = os.environ['TWIACCTOKSEC']
consumera = os.environ['TWICONKEY']
consumerb = os.environ['TWICONKEYSEC']
gitworkspace = os.environ['GITHUB_WORKSPACE']
print("Setting date...")
today = date.today()
yesterday = today - timedelta(days = 1)
showdate = yesterday.strftime("%d %B %Y")
api = tweepy.Client(bearer_token=bearer,
access_token=accessa,
access_token_secret=accessb,
consumer_key=consumera,
consumer_secret=consumerb)
# Use old Twitter API for media upload (legacyapi)
legacyauth = tweepy.OAuthHandler(
consumera,
consumerb
)
legacyauth.set_access_token(
accessa,
accessb
)
legacyapi = tweepy.API(legacyauth)
os.chdir(gitworkspace)
print("Uploading 1 of 17...")
filenames = ['img/cases.png', 'img/deaths.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Malaysia as of " + showdate + ". (1/17) \n#COVID19 #COVID19Cases #CoronavirusCases"
original_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 2 of 17...")
filenames = ['img/cases_Johor.png', 'img/deaths_Johor.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Johor as of " + showdate + ". (2/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 3 of 17...")
filenames = ['img/cases_Kedah.png', 'img/deaths_Kedah.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Kedah as of " + showdate + ". (3/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 4 of 17...")
filenames = ['img/cases_Kelantan.png', 'img/deaths_Kelantan.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Kelantan as of " + showdate + ". (4/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 5 of 17...")
filenames = ['img/cases_Melaka.png', 'img/deaths_Melaka.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Melaka as of " + showdate + ". (5/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 6 of 17...")
filenames = ['img/cases_Negeri Sembilan.png', 'img/deaths_Negeri Sembilan.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Negeri Sembilan as of " + showdate + ". (6/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 7 of 17...")
filenames = ['img/cases_Pahang.png', 'img/deaths_Pahang.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Pahang as of " + showdate + ". (7/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 8 of 17...")
filenames = ['img/cases_Perak.png', 'img/deaths_Perak.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Perak as of " + showdate + ". (8/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 9 of 17...")
filenames = ['img/cases_Perlis.png', 'img/deaths_Perlis.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Perlis as of " + showdate + ". (9/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 10 of 17...")
filenames = ['img/cases_Pulau Pinang.png', 'img/deaths_Pulau Pinang.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Pulau Pinang as of " + showdate + ". (10/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 11 of 17...")
filenames = ['img/cases_Sabah.png', 'img/deaths_Sabah.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Sabah as of " + showdate + ". (11/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 12 of 17...")
filenames = ['img/cases_Sarawak.png', 'img/deaths_Sarawak.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Sarawak as of " + showdate + ". (12/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 13 of 17...")
filenames = ['img/cases_Selangor.png', 'img/deaths_Selangor.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Selangor as of " + showdate + ". (13/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 14 of 17...")
filenames = ['img/cases_Terengganu.png', 'img/deaths_Terengganu.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Terengganu as of " + showdate + ". (14/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 15 of 17...")
filenames = ['img/cases_Kuala Lumpur.png', 'img/deaths_Kuala Lumpur.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Kuala Lumpur as of " + showdate + ". (15/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 16 of 17...")
filenames = ['img/cases_Labuan.png', 'img/deaths_Labuan.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Labuan as of " + showdate + ". (16/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)
print("Uploading 17 of 17...")
filenames = ['img/cases_Putrajaya.png', 'img/deaths_Putrajaya.png']
media_ids = []
for filename in filenames:
res = legacyapi.media_upload(filename)
media_ids.append(res.media_id)
tweet = "COVID-19 cases and deaths in Putrajaya as of " + showdate + ". (17/17)"
reply_tweet = api.create_tweet(text=tweet, media_ids=media_ids)