Last active
January 5, 2022 10:18
-
-
Save sboger/86f49dfba00ee01fcfa8a13222220964 to your computer and use it in GitHub Desktop.
automated computer generated kitty post to twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import requests | |
import hashlib | |
from hashlib import algorithms_available | |
import tweepy | |
# assign the values accordingly. | |
consumer_key = "SEKRET CONSUMER KEY" | |
consumer_secret = "SEKRET CONSUMER SECRET" | |
access_token = "SEKRET ACCESS TOKEN" | |
access_token_secret = "SEKRET ACCESS TOKEN SECRET" | |
def get_checksum_from_picture(picture: bytes, method: str = "md5") -> str: | |
h = hashlib.new(method.lower()) | |
h.update(picture) | |
return h.hexdigest() | |
def save_picture(picture: bytes, file: str = None) -> int: | |
if file is None: | |
file = get_checksum_from_picture(picture) + ".jpg" | |
with open(file, "wb") as f: | |
return f.write(picture) | |
# authorization of consumer key and consumer secret | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
# set access to user's access key and access secret | |
auth.set_access_token(access_token, access_token_secret) | |
# calling the api | |
api = tweepy.API(auth) | |
r = requests.get("https://thiscatdoesnotexist.com").content | |
s = save_picture(r) | |
# Upload image | |
media = api.media_upload(get_checksum_from_picture(r) + ".jpg") | |
# Post tweet with image | |
tweet = "Computer generated cat of the day. #cgcat (via thiscatdoesnotexist.com)" | |
post_result = api.update_status(status=tweet, media_ids=[media.media_id]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment