Anatomy of a Pinterest Follow Bot

Anatomy of a Pinterest Follow Bot

This is a simplified version of the anatomy of my Pinterest Follow Bot, available for free to the mailing list subscribers of the site. Although this is one of the less complicated bots I have built using UBot Studio 5.5, there is a lot going on under the hood. The idea of this article is not to give you the actual code, but to walk you through the process of creating the bot and give you a few tips on how to solve some of the problems I have encountered while building it.

So, where do we start? I am going to divide the functionality into two sections, the first one dealing with all the database operations (yes, we need a database) and the second one will be focused on the actual Pinterest follow/unfollow activities.

Requirements for this tutorial:

The Pinterest follow bot database

For the Pinterest follow bot, I used a simple SQLite database (the only portable database format that you can use in UBot Studio) consisting of two tables, one to save the settings like account information and configuration options and another one to track if and when we have followed a blog and when it was unfollowed.

In order to do that I have created the two tables as follows:

  • Settings table – Here we have two fields, ID and VALUE, with a unique index on the ID field.
  • Usernames table – The usernames table has the following fields USERNAME (unique), FOLLOWED, UNFOLLOWED, FOLLOW_ERRORS, UNFOLLOW_ERRORS. The first field is the username, the second one is when it was followed, the third one is when it was unfollowed. The follow errors and unfollow errors are used for detection of errors during the process of following and unfollowing users. These two fields have a default starting value of zero, which means that no errors were encountered so far.

I am not going to go in depth with the functions and UBot commands that deal with the settings table, saving and retrieving the settings from the database in this article, since I plan on posting another one that will focus on this topic very soon.

The database functions and commands for the blogs table should include the following:

  • Add user to the list (username) – this function will take one parameter (the blog username) and add it to the blogs table setting all the other fields to 0. The idea is if we set the followed and unfollowed fields to zero, it will be easier to find the next blog to follow.
  • Mark user as followed (username) – this function will update the followed field with the timestamp the blog was followed so the bot can keep track when to unfollow it. For this we need the free Ubot plugin DATETIME Manipualtion since UBot Studio does not support timestamps out of the box.
  • Mark user as unfollowed (username) – update the unfollowed field with the timestamp the blog was unfollowed on.
  • Mark user as not found (username) – if for some reason the page returns 404 and this user is gone, update all the fields with some values that will mark this username as followed (1), unfollowed (1), follow errors (3) and unfollow errors(3). This way the username will not be processed again.
  • Follow error (username) – We are going to use this function to increment the follow errors filed for the blog.
  • Unfollow error (username) – We are going to use this function to increment the unfollow errors filed for the blog.
  • Next user to follow – this command should return one user to follow, it will fetch a user who has 0 in the followed field and less then 3 follow errors ordering the users by the number of follow errors ascending.
  • Next user to unfollow – this command should return one user to unfollow. It will fetch a user who has a value greater than 0 in the followed field, 0 in the unfollowed field and less then 3 unfollow errors ordering the users by the number of unfollow errors ascending. Since we do not want users to be unfollowed immediately after being followed we would make an additional condition for the query to get only the users who have been followed X days ago, again using timestamps, so the user_followed should be greater than zero and smaller than the current timestamp (now) – X days * 86400.

Now, when you have your database functions and commands ready, it is time to continue to the actual Pinterest part.

Pinterest functions and commands

This part of the tutorial will fucus on the functionality you will need in order to create the part of the bot dealing with the actual following and unfollowing of users. Here is a short overview of the functions and commands:

  • Login – This function will perform the Pinterest sign in process, basically what you need to do is navigate to the login page, enter your login details (email / password) and click the login button. A check ensuring that you have logged in successfully is required too so that the function can return true or false on success / failure.
  • Follow user (username) – Create the user’s URL by adding the username passed as parameter to the base URL and navigate to this URL. Once everything is loaded check if the follow button is found on the page. If the follow button is found, I suggest that you wait for a while as any real user would do, just a random amount of seconds wait($rand(x,y)). Here you might want to check for deleted usernames and update the database so you do not waste any more resources on them.
    Now click the follow button wait for everything to be loaded and check if the follow button has changed to unfollow. If the process is completed successfully, update the database, marking the blog as followed. If there is an error like you are unable to find the follow button for some reason (dropped Internet connection, server maintenance…) update the follow errors for the blog.
  • Unfollow user (username) – This command does the same as the follow user, except that all checks are made for the unfollow button and when clicked, the follow button. You can lower your waiting times here, since in the real world, when users unfollow someone they do not spend a lot of time just siting on that page.

These 3 simple functions and commands are the basic building blocks of the bot.

Connecting it all together

Now, that you have your database and Pinterest functions it is time to connect everything and start building the Follow Bot. Since this is a single account bot, the functionality that you will need to implement is not very complicated. You should create a simple start/stop control to start and stop the bot with a attached variable #bot running set to $false when the bot is not active and $true when the bot is working.

Once the bot is started and you update the #bot running to $true, execute the Pinterest login. If the login is successful,  loop while the bot is running, adding a 1 second wait at the beginning or the end of the loop (this wait is used to decrease the CPU usage once every user in the database is followed and unfollowed). If the login is unsuccessful, stop the bot.

What else should go in this loop? Here is the full list:

  1. Get the next user to follow from the database
  2. If the username is not blank (username is returned from the database) execute the follow command and wait for a while so that you do not hit the limits and get flagged as a spammer. Also, this is a good place to check if you have already reached the limits and stop following users while still being able to unfollow those who need to be unfollowed.
  3. Get the next user to unfollow from the database
  4. If the returned username is not blank, execute the unfollow command for this username
  5. Wait 1 second

This is the basic layout, but if you want to be more effective in following users on a per hour basis (follow more users per hour), it is a good idea to execute the whole unfollow process in a separate thread. This will create a new problem, since multithreading is kind of problematic with SQLite databases and for some reasons that I will not discuss here, your database functions will stop working as expected, thus breaking everything.

How do you fix this? The way I did it was to add an additional step between steps 2 and 3 in the above loop and change the Unfollow user command by removing all the database related updates and using global lists instead, where the usernames unfollowed / unfollow errors were recorded. Then in the additional step that I added I would check these lists, process them and finally clear them. Also each time a unfollow thread was launched I would set a global variable telling me that a unfollow was in progress and I should not start another thread while this one was running.

These little changes ensured that only one users is followed at a time and while the next is followed, in a separate thread simultaneously I was unfollowing another user. Also the additional check to see if there was already a unfollow thread running ensured that no more than one user can be unfollowed at a time, thus solving the problem with the loop starting a new unfollow thread every second if there were no more users to follow.

The presented layout here is a simplified version of a Pinterest follow bot, just like the one that is available here. While in the one that I give away for free, there are some extras like checking for follow limits, advanced timing configuration and synchronization of the follow/unfollow activities, etc. this should help you build you first bot.

Although this template is made for Pinterest, you can easily adopt it for any other social networking site or any other bot. I hope this tutorial was helpful.