-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.py
56 lines (41 loc) · 1.52 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
import argparse
import toml
import tweepy
# Load keys from keys.toml file
with open("keys.toml") as f:
keys = toml.load(f)
# Access individual keys
api_key = keys["API_KEYS"]["api_key"]
api_key_secret = keys["API_KEYS"]["api_key_secret"]
access_token = keys["API_KEYS"]["access_token"]
access_token_secret = keys["API_KEYS"]["access_token_secret"]
def post_on_twitter(tweet, image):
# Use the keys in your Tweepy code
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)
# Upload image
media = api.media_upload(image)
# Create a tweet
post_result = api.update_status(status=tweet, media_ids=[media.media_id])
print("Tweet posted successfully.")
def main():
# Create argument parser
parser = argparse.ArgumentParser(
description="Post a tweet on Twitter with content and image"
)
parser.add_argument("-m", "--message", type=str, help="Tweet content")
parser.add_argument("-i", "--image", type=str, help="Image path")
# Parse command line arguments
args = parser.parse_args()
# Check if message and image arguments are provided
if args.message and args.image:
# Call the function to post on Twitter
post_on_twitter(args.message, args.image)
else:
print(
"Please provide both tweet content and image path using -m and -i arguments respectively."
)
if __name__ == "__main__":
main()