How To Throw Cats At Random Internet People
We all love cats right? Well most of us do, and the internet is absolutely rammed with them. There's even a few API's that give you a random cat image totally free (how awesome). Let's give one of them a whirl...
from requests import get
data = get('http://aws.random.cat/meow').json()
url = data['file']
print(url)
Which gives us a url of an image. It changes each time we request a new one.
Now that we have the url we can move forward and download it. We'll create a function called get_img()
so we can simply pass the image url and have it return the name of the file it downloaded. We'll create another called get_cat()
to manage getting the cat and call the get_img()
function from inside it. Here we go...
from requests import get
def get_img(img_url):
img = get(img_url).content
file = img_url.split('/')[-1]
with open(file, 'wb') as f:
f.write(img)
return file
def get_cat():
data = get('http://aws.random.cat/meow').json()
url = data['file']
return get_img(url)
image = get_cat()
print(image)
Right now what? Ahh yes... throw the cat at someone. Lets find someone who's been talking about cats recently on Twitter. First we have to authenticate with the Twitter API.
You'll need to get access tokens/keys from https://apps.twitter.com to be able to do this.
import tweepy
from random import choice
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX'
access_secret = 'XXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
Great. That's us ready to start searching for someone to throw a cat at. For keeping things random I'm creating a list of the tweets, not retweets, then randomly choosing one (the tweet_bag
).
tweets = api.search(q='cats', count=30, lang='en', result_type='recent')
tweet_bag = []
for tweet in tweets:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
tweet_bag.append(tweet)
tweet = choice(tweet_bag)
print(tweet.user.screen_name)
print(tweet.text)
That will give us a Twitter user we can throw our cat at. So let's compose our message and send it off with the cat image we downloaded.
msg = f'@{tweet.user.screen_name} Random cattack! x'
api.update_with_media(image, msg, tweet.id)
Oh, and we'd probably want to delete the cat image once we're done with it. Since we already have the image stored in a variable we can simply...
import os
os.remove(image)
And BOOM! We have a script that downloads a cat image, finds a user on Twitter that's got something to do with cats and replies to their tweet with a cat picture and a message then deletes the image. Pointless right? Marvellous!
from requests import get
from random import choice
import tweepy
import os
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX'
access_secret = 'XXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
def get_img(img_url):
img = get(img_url).content
file = img_url.split('/')[-1]
file = f'{file}'
with open(file, 'wb') as f:
f.write(img)
return file
def get_cat():
data = get('http://aws.random.cat/meow').json()
url = data['file']
return get_img(url)
def cattack():
image = get_cat()
tweets = api.search(q='cats', count=30, lang='en', result_type='recent')
tweet_bag = []
for tweet in tweets:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
tweet_bag.append(tweet)
tweet = choice(tweet_bag)
msg = f'@{tweet.user.screen_name} Random cattack! x'
api.update_with_media(image, msg, tweet.id)
os.remove(image)
cattack()
UPDATE
I thought it might be nice to draw some text on the cat image if it's a .jpg file. If it's a .gif it'll just post that as it is. Here she blows...
from PIL import ImageFont, ImageDraw, Image
from random import choice
from requests import get
import tweepy
import os
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX'
access_secret = 'XXXX'
print('Authenticating with Twitter')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
def get_img(img_url):
img = get(img_url).content
file = img_url.split('/')[-1]
with open(file, 'wb') as f:
f.write(img)
if img_url.endswith('.jpg'):
draw_text(file)
return file
def get_cat():
print('Getting cat image')
data = get('http://aws.random.cat/meow').json()
url = data['file']
return get_img(url)
def draw_text(image_file):
print('Drawing text on image')
image = Image.open(image_file)
image_size = image.size
image_height = image.size[1]
image_width = image.size[0]
text = 'RANDOM CATTACK'
fillcolor = 'white'
shadowcolor = 'black'
draw = ImageDraw.Draw(image)
fontsize = 1
font = ImageFont.truetype('nexa.otf', fontsize)
img_fraction = 0.50
while font.getsize(text)[0] < img_fraction * image_size[0]:
fontsize += 1
font = ImageFont.truetype('nexa.otf', fontsize)
text_size = draw.textsize(text, font=font)
x = (image_width / 2) - (text_size[0] / 2)
y = image_height - 180
draw.text((x - 2, y), text, font=font, fill=shadowcolor)
draw.text((x + 2, y), text, font=font, fill=shadowcolor)
draw.text((x, y - 2), text, font=font, fill=shadowcolor)
draw.text((x, y + 2), text, font=font, fill=shadowcolor)
draw.text((x - 2, y - 2), text, font=font, fill=shadowcolor)
draw.text((x + 2, y - 2), text, font=font, fill=shadowcolor)
draw.text((x - 2, y + 2), text, font=font, fill=shadowcolor)
draw.text((x + 2, y + 2), text, font=font, fill=shadowcolor)
draw.text((x, y), text, font=font, fill=fillcolor)
image.save(image_file)
def cattack():
image = get_cat()
print('Searching for Twitter user')
tweets = api.search(q='cats', count=30, lang='en', result_type='recent')
tweet_bag = []
for tweet in tweets:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
tweet_bag.append(tweet)
tweet = choice(tweet_bag)
msg = f'@{tweet.user.screen_name} Random cattack! x'
api.update_with_media(image, msg, tweet.id)
print(f'Thrown cat at {tweet.user.screen_name}')
os.remove(image)
cattack()
Thanks for reading. x
Resources
- Python: https://python.org
- Random.cat - http://aws.random.cat
- Tweepy: https://tweepy.readthedocs.io/en/latest
- PIL: https://pillow.readthedocs.io