Continuing this tutorial on how to create an Instagram bot who tells you who are your unfollowers via email with the second part. If you missed the first part you can read it here.
After we saw how can we use selenium and python to scrap our Instagram data and save it somewhere locally, now it’s time to turn that data into useful information.
Manipulating data is something that python is great at. It’s simple syntax and built-in functions around the data types makes it a programming language extremely friendly for working with data.
If you followed the first part of this tutorial, by now, you should one file generated by the scraping part of this tutorial. The existing file contains the number of the your testing account followers on the first line and the list of the followers with one follower per line.
Your file should look similar, but obviously your followers and their number on the first line.
Connecting the dots
Now that we have the data that we need, let’s see what we can do about it. First we would need to turn our data from files to a variable. That is doable by importing it as a file and assign it to a variable.
To get our new followers/unfollowers number we’ll need a list of users to compare our current list with so this will work only with two files or two scraps. To solve that and don’t get a failure at the first scrap, in this function we check the number of the files inside our directory. If that number is less than 2 we’ll add a new blank file.
To continue, we’ll turn our imported data into beautiful lists. To do that we’ll just need to add their lines into a variable, remove the useless blank spaces and also remove the 1st row.
Now that we have our lists of people, what we need to do is to compare these lists. In this way we can get our unfollowers and followers as well.
And finally we have the results. People who unfollowed our test account and also the ones who started following it, but this will looks just as a python list now:
['follower1', 'follower2', 'so', 'on'] . Not very esthetic, right?
So let’s create a simple function to turn our results into more readable strings:
This function, applied to a list, will generate a more readable string which can be inserted in an email.
Moving on from the data preparation part, all we have to do now is to set up a notification system. The easiest way (I believe) to do this is to set up a script which sends you an email with your results. Fortunately, Python has some built-in libraries who let’s you automate the sending of an email. The libraries that we’ll need for this are: smtplib, ssl and email.
SMTPLIB is a package which helps us to sent an email to any machine with an active SMTP or ESMTP listener. It provides the ability to create an object which can create a SMTP session. We will use it to create a connection with an email server.
SSL provides access to ssl encryption for network sockets on both sides, client and server. This package uses OpenSSL library and works on most systems as long they have OpenSSL installed on. We will use it to secure our connection the the mail server.
Email is a package that is not related to sending emails, but rather to manage email messages. It’s capabilities we’ll help us to compose the email message we send to the user. It can properly manage an email fields like To, Subject, Cc, Bcc etc.
“It’s Not About Money, It’s About Sending A Message”
Having the great quote related to the subject checked and all this information in mind, let’s put it to work. First we’ll have to create a file to store our usernames and emails. This file would contain the username on the first position of the line and the username’s email separated by a space.
To continue, we’ll need a function to extract this information and pass it forward. A function like this will do the job:
Having this information we can now work towards sending the email. To do that we’ll follow the next format:
- Set up the parameters required for smtplib.
- Set up the message parameters.
- Set up the message content.
- Create the message in a proper way to send it.
- Open the SMTP server and send the message.
For this we’ll create, again, a function to simplify the whole process.
This function is going to take as parameters the string of followers and unfollowers returned by our already written list_to_string() function applied on get_new_followers() and get_unfollowers(), the email and name of the user and finally the email and it’s password of the sender. I advise you to set up a new gmail account for this too. Setting up a new gmail account is quite easy, however this new account will not permit you to send automatic emails yet. This is due to a security policy set up by google. To get rid of this, all you’ll have to do is:
- Go to Manage your account.
- Next, go to Security.
- Search for Less secure app access and turn it on.
Putting it all together
With all our pieces written, now it’s time to assemble them into a function to do all the work for us. This function will import and call most of our written functions.
In my case, I made this function inside my python file which contains all the scraping part seen on the first part.
After this is done, you can call this function periodically or set up a loop which calls it everyday for you. Please keep in mind that using this too often might get your account banned, however, using this once a in a while didn’t got me into problems. Also my testing account is fine after being scrapped over and over again in minutes.
Building an Instagram bot is a pretty nice way to train your python skills and also make something practical and fun, which can help you or your friends. This idea can be extended in many ways and many useful features can be added. If you feel so, feel free to use this code. You can find it on my github page: https://github.com/razvanvilceanu/Shitstagram