Project, Python, Research

Automating Making Memes & Instagram Posts 2: Instagram-API-Python

Last time, I learned how to automatically generate memes using meme-generator-python. Now that I’m able to automatically create memes, it’s time to automate posting it to Instagram. I signed up for a new account called DavidRoseBot and started looking around for open source projects to borrow code.

My first auto-generated meme. <3

I found a couple programs to try on github, one is called Instabot.py and the other is python-instagram. There is also what appears to be a more simple program called InstaPoster that I may take a look at just to see what kind of code it uses in case I want to create my own custom scripts.

I decided to start by installing Instabot.py and see how it goes. This program requires Python 3.6+, so I had to make another virtual environment.

mkvirtualenv instabotpy_env
workon instabotpy_env
python3 -m pip install instabot-py
instabot-py

After starting the bot using instabot-py, it asked me for user input to fill out the settings. I left everything at default, but then noticed that this bot doesn’t actually post, it just automates liking other accounts, following, and commenting based on hashtags. As the article Instagram Bots: The Pros and Cons for Business points out, this type of automation can do more harm than good. I’m not interested in getting my new account shadowbanned, so I’ll just uninstall instabot-py and go a different route.

python3 -m pip uninstall instabot-py
deactivate
rmvirtualenv instabotpy_env

I’ve decided to pursue InstaPoster since I’m not sure python-instagram can actually make posts either, as it seems to be more about retreiving information about a user’s account. InstaPoster also requires setting up a cron-job account, which would be beneficial to learn about as it can be useful for other projects down the road.

I started by downloading the InstaPoster files and exporting them into my instameme_env virtual environment. I then opened a command terminal in that folder and ran the following:

sudo apt install composer
composer install

I then edited the config.php file to reference the newly made David Rose Instagram and autogenerated meme. Unfortunately, when I went to run the script using the command php -f index.php an error occurred. Apparently the “composer install” step errored and I did not notice.

Well that doesn’t look good

A quick google sent me to this thread to try installing a couple missing packages.

sudo apt-get install php-curl 
sudo apt-get install php-gd
sudo apt-get install php-mbstring
sudo apt-get install php-bcmath
composer install
php index.php

This time the code tried to execute, but it ran into a problem: Undefined variable img_url in index.php file. So I opened up the file, located the two instances of “img_url” and changed them to “imgurl” to match the config.php file’s variable.

And that’s not much better….

After getting the above error I tried to debug it, but eventually gave up and went back to google to find yet another way to post a picture onto Instagram. Eventually I installed Instagram-API-python since it seemed like I could install with pip and possibly get lucky. To start, I opened a new terminal and navigated to my virtual environment.

cd .virtualenvs
workon memebot_env
pip install InstagramAPI
python
from InstagramAPI import InstagramAPI

I then went into my memebot_env folder and created a text file called “upload_photo.py” and pasted in the following.

!/usr/bin/env python
-- coding: utf-8 --
#
Use text editor to edit the script and type in valid Instagram username/password
from InstagramAPI import InstagramAPI
InstagramAPI = InstagramAPI("type_username_here", "type_password_here")
InstagramAPI.login() # login
photo_path = 'filepath/photo.jpg'
caption = "Sample photo"
InstagramAPI.uploadPhoto(photo_path, caption=caption)

I went back to my terminal to give permission to the file to be ran as an executable by using the chmod command.

chmod u+x '/home/thecatgeek/.virtualenvs/memebot_env/upload_photo.py' 

The first time I ran the script I received an unknown error, but I received a message that it successfully logged in. I started looking into other avenues but ran into some code that specifically mentioned making sure the photo was the correct dimensions for it to be posted properly. I manually cropped my sample photo to be a square dimension and reran the script and I finally posted my FIRST automated IG picture! REJOICE!

So naturally my next step is going to be making scripts to automatically call the two programs and to change around the variables. So far it’s only two lines, but this is a great start. ๐Ÿ™‚

python memegen.py "Here's to all the memes" "That make us smile" photo.jpg
'/home/thecatgeek/.virtualenvs/memebot_env/meme-generator-python-master/upload_photo.py' 
Oh, you’re too kind! *blush*

Soon I’ll be using my neural networks to make memes and post them out to Instagram by themselves, I can’t wait to see what they come up with! Plus I can use these scripts to post my billions of nice Nikon pictures of Madam Crookshanks onto my TheCatGeek IG a few times a day to share with friends and family!

Eventually if this is something I want running all the time to help with automatically posting social media content for small businesses, I could buy a raspberry pi and have my own dedicated server connected to my harddrive that’s always working on my behalf! But that’s for another day, for now…. more Madam Crookshanks!

Leave a Reply