Sunday, November 1, 2020

Building an Instagram Posting Bot


As a result of the 35mm film slide converter project that I had built a while back, I have managed to digitized a good number of film slides. The tragedy of all this is that while I definitely enjoyed looking at and preserving images that haven't been viewed in decades, I didn't just want to have them be just stored away again on some hard drive, only to be faced with being obsolete and lost to time again in another 10 or 20 years when technology moves us in a direction that I can't even conceive of now. 

Instead, I wanted to somehow share these treasures with the world. I started out by setting up a new webpage, "A Slide A Day" where I released a new photo each day with the capability of loading up several years worth of photos - so theoretically, if something catastrophic ever happened to me, I can pretty much guarantee that this website will continue to serve up new pictures way into the future. 

While the website is a nice archive for the photos. it may not ever get the broad audience reach that I think these photos deserve. 

While trying to figure out this quandary, I happened to chance upon Instagram. I knew that Instagram was a fairly popular photo-sharing app, but I didn't really pay a whole lot of attention to it until now.  

When I looked into Instagram a bit more, I was actually quite intrigued by how easy it was to share a photo with a large audience.  

The problem that I had with Instagram was that it was very much focused on people that used smartphones, so much so that the only way that you can post a picture on Instagram is via a smartphone. If you pulled up Instagram on a Windows-based computer web browser, you can only view pictures, not post them.

The other issue was that Instagram was very much an "in the moment" type app - meaning that you could only post in realtime. You do not have the capability to schedule your posts ahead of time.

This was a bit of a problem for me since I really didn't want to have to stop what I was doing at some point during the day and remember to post something - I really want to have a bunch of pictures loaded up in a folder and just drip-feed pictures at a regular daily schedule - publish "a slide a day" like the name of my site suggested.  

Instagram web page on a desktop computer

Since all my pictures are stored in a file folder, it didn't really make them very smartphone friendly - I needed to have a way to post my pictures from a proper computer. 

After some digging around, I quickly found that Instagram had an almost fanatic aversion to allowing you the ability to post from anything but a Smartphone.  While most apps will have some sort of API hook in to allow you to feed things, Instagram pretty much goes out of their way to not allow the use of APIs. 

There are 3rd party services like Later that allow you to schedule Instagram posts, however, they are very much geared to business accounts, which makes it a bit economically unfeasible for an individual like myself.

There is however one way to solve the issue of not being able to post on a desktop computer, and it involves invoking a bit of a web developer's trick. 

Google Chrome has a feature that allows you to fake the Chrome browser to think that the browser is running on a Smartphone. 

This is done by activating the Developer Tools function, which is accessible by clicking on the Chrome options menu (the 3 vertical dots on the top right of the browser), selecting the More Tools option from the drop-down menu, and selecting the Developer Tools option from the next drop-down menu 

Accessing Developer Tools

This puts your browser in developer mode, which is normally used to test a webpage that you may be developing under a number of conditions. One of these conditions is to have the webpage simulate working on a smartphone. This is done by clicking on the Toggle Device Toolbar option, which is a screen icon to the right of the Elements option at the top of the developer window.

This triggers the browser to act as if it's on a Smartphone and you can configure the browser to act as if it's on a particular type of smartphone by clicking on the drop-down menu on the far left of the browser window - In my case, I set my browser to emulate running under the iPhone 6. 

Once I did that, I refreshed my browser, and like magic, the "Post Photo" button on my Instagram page appeared 

In Developer Mode

Once I had that, I closed the Developer Tools window and I now had an Instagram page on my desktop that allowed me to post. 

Browser with posting now available

So now I solved my ability to post from my computer, but how can I get it so that I can automatically schedule my posts?

In my day job, we use robotic testing tools that simulate a person sitting at a keyboard and mouse. 

These tools are typically used to test user interfaces for computer applications and websites. They really take the drudgery of doing the same actions over and over again when doing software testing. 

However, even though they are quite useful, these tools can also be pretty complex to set up and expensive to buy. 

I still liked the idea of having a little bot that would take a file from my picture folder on my PC and post it on Instagram while I was off doing other things. 

Eventually, I came across a tool called Auto Mouse which is produced by MurGee. This tool basically emulates a person doing mouse clicks on a desktop, along with other features like entring text into fields. I had the option of trying the program out for a few weeks with the option to buy a full version for less than $10. It also had the ability to schedule when to execute my scripts and looked to be fairly easy to record and playback actions on my desktop. 

Auto Mouse


After playing around with Auto Mouse for a bit, I was satisfied that it would do what I wanted to do. 

After some doing some manual posting to Instagram, I settled on a routine that I wanted the bot to do:
  • Open Chrome
  • Go to Instagram in the Chrome browser
  • Activate Chrome's Developer tools and put the browser in a mobile emulator mode
  • Close Developer tools window. 
  • Upload first picture from my Pictures folder list to Instagram 
  • Add hashtags in comments
  • Post picture to Instagram
  • Close Chrome browser
  • Open Windows Explorer and go to the Pictures Folder
  • Delete the first picture in the folder 
  • Close Windows Explorer and wait to do the next post tomorrow 
To record the script all I had to do was to assign a hotkey in Auto Mouse that I would use to record the position of my mouse where I wanted an action to take place. In my particular case, I chose the F2 key as an appropriate hotkey since it wasn't going to be used as part of my planned posting activities. 

Since it is critical that all the various buttons that I need to push always stayed in the same locations, I first locked the taskbar in order to fix the Chrome and Windows Explorer icons in place and I make sure that the browser and explorer window opened in full-screen mode.  

To record a step I placed my mouse on the spot where I wanted to make a mouse click and pressed the F2 Key. By default, Auto Mouse records this action as a Left Mouse click at that location on the screen, which is the action that I want to take for about 80 percent of the time.

When I needed to do some sort of action other than a left-click (such as enter some text in a field or do a right mouse click). I could easily change it by accessing the settings for the step I just recorded and set it to one of a number of actions from a drop-down menu. In my case, I predominantly used the "Type Text From Comment" for entering input into a field (in my case, the URL on the browser and hashtags for my posts). 

Editing a step

Since I also wanted to make sure that the script would be able to run as reliably as possible, I also increased the duration between step executions in order to allow for any lags in the loading of webpages or files. To do that I increased the default Delay Before Action by a factor of 10. 

By manually doing a post and making sure I hit the F2 at the appropriate times, I was able to map out the steps needed to post a picture to Instagram and deleting the picture from my Pictures folder afterward. 
Recorded steps


After running the script manually a couple of times to make sure things were running smoothly, I saved the script. 

The tool also has the ability to schedule scripts to run on an automated schedule. The schedule functions accessed by clicking on the Menu button and select Schedule Script from the drop-down menu. 

Scheduler

The tool provides a number of ways to schedule the execution of a script, including using the built-in scheduler within Auto Mouse. 

In my case, I elected to use the built-in Scheduler that is part of the Windows O/S. 

By clicking on that option, I was provided the needed parameters to put into Scheduler.

Scheduler parms


The tool then sends you to the scheduler, and you simply create a schedule to run the script when you want it to run, using the parameters that Auto Mouse provides

Setting a schedule

At the point, I closed Auto Mouse and waiting until the scheduled time for my script to run. 


So far I have had this script running once a day posting images to my Instagram account - I have been quite pleased with how robust it has been.  I am going to look into using Auto Mouse for a couple of other tasks that I have in mind 

No comments:

Post a Comment