Creating a Home Alone Instagram bot

I’ve been creating bots for as long as I can remember. As I kid I coded useless IRC-bots, and when Twitter came along I created lots of silly bots.

Because Christmas is nearly here and because I love the movie Home Alone (in a bad way) I decided to create another bot. But how do you create a bot in 2018 and how do I keep myself from repeating past mistakes?

It’s not you, it’s the API.

A lot has happened to the openness of the Internet and it’s quite difficult to create a bot nowadays. Twitter has limited its API capabilities (no more realtime), and in order to post you need their permission. Instagram has ditched its API and replaced it with something horrible for marketeers and other scumbags. And since Facebook leaked all of your data, they’ve closed their doors and now require you to sign contracts in order to do anything fun.

There are of course good & open platforms like Telegram, but I don’t know many people who use them.

Luckily there are backdoors, and in order to put my bot on Instagram I used instagram-private-api, an NPM package that does everything the original API did and more.

Polish

The original idea was to post 1 frame every hour. Home Alone lasts 102 minutes. Times 24 frames, times 60 seconds this would take 146880 hours, or 16 years. This would’ve looked great on the Instagram profile page, but I tested it and it was way too spammy. And I know I can’t maintain a project for 16 years.

So I decided to do 1 post every day, and jump 5 seconds in the movie. This would make the project last a little bit more than 3 years.

Next up: how can people discover this bot? Tags work great on Instagram, but I don’t want to tag 1200 images.

Enter the machine

I’m glad we live in the age of wonders. Computers can analyze pictures and tell us what’s in them. Amazon offers a nice API that allows you to tag images for as little as $1 per 1000. rekognize1.jpg

Before posting an image I send it to this API and tag it with the returned result. I also load the subtitles and extract what’s being said at that exact moment. This makes following the story a bit easier.

Someone asked if it was possible to tag the actors as well. Amazon offers another API to compare faces, so I implemented it and once one of the actors that has Instagram appears it will tag them.

face.jpg

Keeping it up and running and cost

I’m very bad at keeping things running. A Twitter bot I once created died because the cleaning lady unplugged the Raspberry Pi it was running on and the machine wouldn’t boot. Other projects died because I lost interest, or simply because they were costing me too much money.

The very first version of the Home Alone bot ran on a €3.5/month server. In the end it would’ve cost me 140 euros, which is not that much money but still. Running a server has its risks, you need to update it or you’ll get hacked and things tend to break even while doing absolutely nothing.

Buzzword time: serverless. I shut down the server and moved my code to AWS Lambda. Once a day Amazon starts a server, runs my code and shuts it down. This takes away all my Linux worries and gives me peace of mind.

This costs me €0 for computing power (because Amazon gives you 500.000 free seconds of compute time every month), €0.01 for S3 storage (the stills) and €0.15 for machine learning. A grand total of €0.16 every month, or €6 euros at the end of the project.

Nerdstuff

The code for the bot: https://github.com/TimBroddin/dailyhomealone

Generating the stills (1 frame every 5 seconds with burned in subtitles)

ffmpeg -i HomeAlone.mkv -qscale:v 2 -filter:v "subtitles=Home\ Alone.mkv, fps=0.2" images/image%05d.jpg

The end

You can find the bot here: https://www.instagram.com/dailyhomealone.

Gepubliceerd op 23 Dec 2018

Creative coder bij het VRT creative lab.
Tim Broddin on Twitter