automeme/upload_best_meme_to_twitter.py

47 lines
1.6 KiB
Python

from io import BytesIO
import tweepy
from automeme import TextLine, get_rdrama, strip_markdown
import utils
import json
from meme_generator import get_image_file_from_url
def load_key_from_file(filename : str) -> str:
with open(utils.get_real_filename(filename), "r") as f:
return f.read()
consumer_key = load_key_from_file("twitter_api_key")
consumer_secret = load_key_from_file("twitter_api_secret")
access_token = load_key_from_file("twitter_access_token")
access_token_secret = load_key_from_file("twitter_access_token_secret")
auth = tweepy.OAuth1UserHandler(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)
api = tweepy.API(auth)
rdrama = get_rdrama()
for comment in rdrama.get_comments(user = "automeme", sort = "top", t="day")['data']:
if "Darrell Brooks" not in comment['body'] and "twitter" not in comment['body']:
best = comment
break
print(json.dumps(best, indent=4))
comment_text = best['body']
cleaned_comment_text = strip_markdown(comment_text)
comment_lines = cleaned_comment_text.split("\n")
comment_lines = [comment_line for comment_line in comment_lines if comment_line != ""]
element_lines = [TextLine(line) for line in comment_lines]
for i in element_lines:
print(i)
image = element_lines[-1].images[0]
print(image.url)
animated_image = get_image_file_from_url(image.url)
file = BytesIO(initial_bytes=animated_image.get_binary_gif())
media = api.media_upload(filename = "foo.gif", file=file, chunked=True, wait_for_async_finalize = True)
media_id = media.media_id_string
api.update_status("#meme", media_ids=[media_id])