Creating an Instagram Posting Bot.

Jake Mellichamp
CodeX
Published in
4 min readOct 21, 2022

--

Lets talk about how to make an Instagram Bot

Abstract

Instagram is one of the most popular social media platforms around today. As a fellow user myself, I use it fairly often, but in different way. My primary reason for opening Instagram isn’t to see my acquaintance’s most recent vacation to Mexico or laughing at memes (which valid, is also fun) — but is instead to consume ‘inspirational content’.

An example of an ‘Inspirational Content’ account.

A lot of these pages have 100k + followings, and post… quotes from famous individuals… It got me thinking, “Hmmm, instead of scrolling down consuming someone’s idols, I should make one myself!”

So the quest was on — I was going to create an Instagram Bot that could automatically post a quote a day from people who I personally take inspiration from!

A Quick Note

If you’re here because you want to do this yourself, simply scroll to the bottom! There’s a link to the open source code on Github, and I’ll also outline how to setup the repo to work for you personally!

This Article also has a corresponding video for Visual Learners

Where to Begin

As software engineers, it’s in our DNA to break problems up into smaller ones. The ultimate goal is to create an Instagram account that can post random quotes automatically, with that criteria we need to build a software application that can:

  1. Get a Random Quote from a predefined list of People.
  2. Generate an Image that combines a picture of the author with the quote
  3. Post the generated picture to Instagram

If we follow the principle of ‘Separation of Concerns’ we can also create a organized code base that accomplishes each one of these objectives. Let’s dive in!

I. Random Quote Selection

The process of selecting a random quote can be done many ways. We are starting with a predefined list of authors, so the first algorithm that came to mind was:

  1. Select a random author (literally just a random string within a list of strings)
  2. Select a random Quote from the selected author. (Again, just selecting a random string within a list of strings)

Pretty straightforward right? The list of authors is hardcoded into the application, but the list of quotes are generated at runtime by pulling quotes from the website www.goodreads.com using the Python library beautifulsoup. This allows the end-user to easily add additional authors to the hardcoded list without having to manually go searching for cool quotes by said author.

Optimization through Caching: The code will also ‘cache’ the quotes obtained from the www.goodreads.com website in a text file so that ‘re-fetching’ quotes from the website is unnecessary.

II. Image Generation

If this bot was made for twitter, having the Author and Quote would be the only thing we need. However, because this is Instagram, we have to post a photo. This means we need to morph our random quote onto an image before we are ready to post.

Instagram requires that photos be a certain dimension at the time of post:

  • 1:1 Ratio Images (1080 x 1080 pixels)
  • 4:5 Ratio Images(1080 x 1350 pixels)

I won’t lie, I was struggling to consistently find images of authors at this size. This struggle led me to the decision that I would simply manually add photos to the application directly. Using my photo editing software of choice, Adobe Photoshop, I was able to create photos using the dimensions described and add them to the software application. Once these Images were saved to the application, the python library ‘Pillow’ (Python Image Library) was used to modify the image and write text onto the image before resaving.

The main photo edits that needed to be performed were the following:

  • Lower the Contrast of the Image by 50% (That way the photo text would ‘pop’)
  • Center a arbitrary length string onto the center of the photo.

III. Posting to Instagram

Now the part we’ve all been waiting for, actually posting to Instagram. There does exist an official Instagram API for developers to interact with various endpoints. When it comes to personal programming projects, I hesitate to re-invent the wheel when I don’t have too. Therefore I utilized the instabot python wrapper for interacting with Instagram. Instead of having to manually create/configure requests, we can call the wrapper functions to do this for us! With an astonishing 12 lines of code in our application we can post to instagram!

Code used to publish to instagram

Hats off to Daniil Okhlopkov for making a very intuitive library!

Conclusion

So there you have it folks! After coming up with an initial project idea and breaking it down into smaller sub-problems — we were successfully able to create a Instagram Posting Bot! The main takeaway from this post is by breaking a problem into sub-problems or ‘individual challenges’ I was able to create a concise code base that is easy to configure and modify. I hope this code breakdown was easy to understand. Feel free to clone the repository and try it out yourself!

Github Link: https://github.com/Jacob-Mellichamp/Instagram-bot

Follow me on Twitter: https://twitter.com/JakeMellichamp

--

--