-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_manager.py
58 lines (44 loc) · 1.76 KB
/
twitter_manager.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
import requests
import re
import base64
from requests_oauthlib import OAuth1Session
class TwitterManager:
def __init__(self, config):
self.config = config
self.oauth_v1 = OAuth1Session(
config["TWITTER_API_KEY"],
config["TWITTER_API_SECRET"],
resource_owner_key=config["TWITTER_ACCESS_TOKEN"],
resource_owner_secret=config["TWITTER_ACCESS_TOKEN_SECRET"],
)
def upload_media(self, image_url):
response = requests.get(image_url)
image_data = response.content
headers = {
"Authorization": f"Bearer {self.config['TWITTER_BEARER_TOKEN']}",
}
files = {
"media": image_data,
}
upload_response = self.oauth_v1.post(
"https://upload.twitter.com/1.1/media/upload.json",
headers=headers,
files=files,
)
if upload_response.status_code != 200:
raise Exception(
f"Media upload failed with status code {upload_response.status_code}, response {upload_response.text}"
)
return upload_response.json().get("media_id_string")
def tweet_quote_and_image(self, quote, image_url):
media_id = self.upload_media(image_url)
print(f"Uploaded media ID: {media_id}")
quote_without_hashtags = re.sub(r"#\S+", "", quote)
payload = {"text": quote, "media": {"media_ids": [media_id]}}
response = self.oauth_v1.post("https://api.twitter.com/2/tweets", json=payload)
if response.status_code != 201:
raise Exception(
f"Request returned an error: {response.status_code}, {response.text}"
)
print(f"Tweeted: {quote}")
return quote_without_hashtags, image_url