-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.py
63 lines (53 loc) · 1.74 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
import json
import sys
from requests_oauthlib import OAuth1Session
import vars
def load_credentials():
try:
import vars
print("Loading credentials from vars.py")
return (
vars.OAUTH_CONSUMER_KEY,
vars.OAUTH_CONSUMER_SECRET,
vars.OAUTH_ACCESS_TOKEN,
vars.OAUTH_ACCESS_TOKEN_SECRET,
)
except ImportError:
print(
"vars.py not found, attempting to load credentials from environment variables"
)
return (
os.getenv("OAUTH_CONSUMER_KEY"),
os.getenv("OAUTH_CONSUMER_SECRET"),
os.getenv("OAUTH_ACCESS_TOKEN"),
os.getenv("OAUTH_ACCESS_TOKEN_SECRET"),
)
# Check if tweet content was provided as argument
if len(sys.argv) != 2:
print("Usage: python tweet.py \"your tweet content\"")
sys.exit(1)
tweet_content = sys.argv[1]
# Load credentials
consumer_key, consumer_secret, access_token, access_token_secret = load_credentials()
if not all([consumer_key, consumer_secret, access_token, access_token_secret]):
raise ValueError(
"Twitter OAuth credentials are not set in vars.py or environment variables."
)
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret,
)
payload = {"text": tweet_content}
response = oauth.post(
"https://api.twitter.com/2/tweets",
json=payload,
)
if response.status_code != 201:
raise Exception(
"Request returned an error: {} {}".format(response.status_code, response.text)
)
print("Response code: {}".format(response.status_code))
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))