-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_mosquito_update_bdn.py
66 lines (51 loc) · 2.24 KB
/
tweet_mosquito_update_bdn.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
import tweepy
import os
import requests
import time
from datetime import datetime
time.sleep(60)
consumer_key = os.getenv('CONSUMER_TOKEN')
consumer_secret = os.getenv('CONSUMER_TOKEN_SECRET')
access_token = os.getenv('ACCESS_TOKEN')
access_token_secret = os.getenv('ACCESS_TOKEN_SECRET')
def get_twitter_conn_v1(consumer_key, consumer_secret, access_token, access_token_secret) -> tweepy.API:
"""Get twitter conn 1.1"""
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret)
auth.set_access_token(
access_token,
access_token_secret,
)
return tweepy.API(auth)
def get_twitter_conn_v2(consumer_key, consumer_secret, access_token, access_token_secret) -> tweepy.Client:
"""Get twitter conn 2.0"""
client = tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret,
)
return client
client_v1 = get_twitter_conn_v1(consumer_key, consumer_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(consumer_key, consumer_secret, access_token, access_token_secret)
if not all([consumer_key, consumer_secret, access_token, access_token_secret]):
raise ValueError("Twitter API credentials are not set properly")
# URL of the image
image_url = 'https://raw.githubusercontent.com/colebaril/Mosquito_Monitor/main/bdn_mosquito_update_table.png'
local_image_path = 'bdn_mosquito_update_table.png'
# Download the image
response = requests.get(image_url)
if response.status_code == 200:
with open(local_image_path, 'wb') as file:
file.write(response.content)
print(f"Image downloaded and saved to {local_image_path}")
else:
print(f"Failed to download image. Status code: {response.status_code}")
media_path = local_image_path
media = client_v1.media_upload(filename=media_path)
media_id = media.media_id
# Get the current date
current_date = datetime.now().strftime('%Y-%m-%d')
# Create a tweet
message = f"City of Brandon mosquito trap counts have been updated as of {current_date}. See a detailed update here: https://shorturl.at/LkDqU. \n#Brandon #Mosquitoes #Mosquito #CityOfBrandon #HealthAlert"
client_v2.create_tweet(media_ids=[media_id], text=message)
print("Tweeted!")