Image for post
Image for post
“This is Tina.” http://redd.it/ctj32b #aww

How to make a Twitter cuteness bot.

The easy way and the hard way.

Kevin McElwee
Aug 25, 2019 · 3 min read

The “aww” subreddit is arguably the cuteness singularity of the internet. With more than 20 million members, usually tens of thousands of users judge the cuteness of every post. We can leverage Twitter and Reddit’s developer APIs to share the best content of r/aww to Twitter.

Here’s the Twitter account @reddit_says_aww. And here is a link to the code. Naturally, I’ve removed the public and private keys for my Reddit and Twitter accounts, but if you apply for a developer account at both companies, they can generate authorization tokens for you. Reddit’s python wrapper is called praw and Twitter’s python wrapper is called tweepy.

The bot requests the top five posts on r/aww’s “hot” tab. If the content has not been tweeted before, then it properly formats the content and posts to Twitter. Here’s a flowchart that breaks down how the code functions.

Image for post
Image for post
reddit_says_aww.py flowchart

Uploading media to Twitter through the API can get complicated. For this reason, the bot only handles images and gifs, not videos, at least for now. If the content is hosted on imgur or gfycat, then Twitter will automatically load a preview, so instead of uploading the media, we can simply post the link. Reddit’s video, however, is not supported in Twitter’s embed system, so the bot must miss some cute videos. There are many exceptions and if statements used to handle how different file formats are uploaded. Another exception to consider is the Twitter character limit. If Reddit titles exceed the twitter count, there’s a function to shorten the title and add ellipses.

UPDATE: This bot used to rely on Heroku, but it has now migrated to GitHub Actions, a free alternative. More information is available here to set up a bot with one’s own configuration. This allows for anyone to quickly deploy their own Reddit-to-Twitter bot without ever having to leave GitHub!

The IFTTT (If This Then That) platform has made it easy to create straightforward Twitter bots. For example, my Mindfulness Bot was incredibly simple to make. This is many times faster than the hard-coding approach (and free!). If you’re trying to make a similar bot, I would recommend to always start with IFTTT before trying to make a bot by hand. Because the bot had to ignore video and manage different file formats, however, IFTTT was ineffective at consistently posting material clearly. For this project it was not best suited. If you’re interested in replicating this bot on a text-based reddit, then IFTTT might be the right fit!

Questions? Comments? Puppy photos? Contact me and see more projects on my website.

The Startup

Medium's largest active publication, followed by +752K people. Follow to join our community.

Kevin McElwee

Written by

🏳️‍🌈 Machine learning engineer and data journalist. Learn about me and my projects at www.BrownAnalytics.com

The Startup

Medium's largest active publication, followed by +752K people. Follow to join our community.

Kevin McElwee

Written by

🏳️‍🌈 Machine learning engineer and data journalist. Learn about me and my projects at www.BrownAnalytics.com

The Startup

Medium's largest active publication, followed by +752K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store