Hire the author: Samuel M
In this tutorial I’ll cover how to create a python twitter unfollower script. If your highly active on twitter like I am and a bit popular you’ll it’s hard to manually check unfollowers. The reason why someone would follow then unfollow can vary. Sometimes tweeps just want to have more followers than following so they play the game of following you then unfollowing you a while later, hoping you won’t notice.
pickle – a python data saving module that saves any data type into a file that can be retrieved and read later
pip – a way to install python libraries directly from the terminal
There’s websites that offer this feature but they will ask for permission to your twitter account and can even tweet on your behalf if you let them. With something along the lines “I have 3 followers and 2 unfollowers tracked by appX”. That’s one way to do it, the other way is I’m a coder, I know how to connect with twitter api and I can do it myself in as few lines as possible.
Step 1: Elaborate how code works
So first thing we need for this python twitter unfollower is I need is to get a list of followers and people I’m following. To better elaborate this I’m going to go through these few steps on jupyter notebook which allows me to show you the output of multiple lines of code.
So here I’m going to show you using “python-twitter” which you’ll need to install with pip, and that’s it 🙂 that’s the only requirement. Once you have this you’ll need to head over to twitter developer and setup your api keys. Remember to keep them private.
Note if you try and get keys during Covid 19 lockdown you’ll likely have this delayed, usually it took prior to this one day to be authenticated now I really can’t tell and as twitter and many other companies ramp down on non-essential personnel things like this might take longer to be verified. I suggest talking to the official twitter support to know more.
Step 1.1: How you could unintentionally leak your api keys
Now one thing I learnt the hard way is sometimes you do personal projects like this, my python twitter unfollower that include the key in your code. Then you do a couple of git commits and when you push to github wanting to share the project to the public you delete the key and make a final commit. Your key is safe right? Wrong! it’s in the commit history and some clever pen testers are combing github repos looking for them in the history. I’d recommend making sure this never gets added to the logs, my preference is creating a file “keys.py” with all the keys. Adding it to .gitignore this way git will ignore it and then importing it in the main file, this way at no point are my keys compromised 🙂
Step 1.2: How you can verify your keys are working
Now that your sure your covered the next step is to confirm your keys are working ok and the python twitter unfollower script is working fine. Now to confirm this the code “api.VerifyCredentials()” will either throw an error if the key is invalid, maybe even for Time Out Error. Since twitter being twitter is bogged down with millions of requests per minute just from normal app/website usage and with api usage they won’t allow you bog down their system making it slow for normal usage. Therefore the cap which seems to be one request every 15 minutes. Now you can contact them and have them remove this default limit but in my case, it’s not an issue, since I’m going to be using it rarely.
Step 1.3: How to get list of followers and following
Ok, now for the good parts, I can query twitter to know my followers and friends (you could probably do this for anyone/public account). But your reasons need to be valid and stated before hand or you’ll run into trouble with twitter.
The returned user objects are class objects so they have a number of functions linked to each for starters
and one of these x.screen_name which isn’t a function per say since you need to close with braces “()” for it to be that. This is a variable linked to it and it’s what I want, a unique identifier, the username. Which won’t be shared between two users. Plus I can use it to check who you are and figure out why you just un-followed me.
Step 1.4: How to filter only the usernames from the results
Now I can run a list iterator like so “followers_usernames = [x.screen_name for x in followers]” and I now have a list of only the usernames for my followers. I did the same for people I’m following and with that I have what I need to take the next step.
There’s four relevant sections here: new follower, new following, un follower and unfollowing. These describe the 4 possible scenarios from someone following/unfollowing you to you following/unfollowing someone. To quickly test this I’d simply unfollow/follow someone knowing well enough the next query will generate a different list.
Step 1.5: How to keep track of changes
To keep track of the last time I checked, if ever which by the way if you clone the repo listed in the last line of this article you should delete “save.p” which contains my followers/following list. A few iterations will resolve the issue still, so take your pick on how you want to deal with the file. Now the save.p file is a pickle file which saves my list of followers and following. That’s what I’ll compare to with the next query to find out the four classes of possible changes to my twitter circle. Pickle is used to save any form of python data and I’ll be using it to keep track of my followers and following so I can be able to tell what changed over time.
Quickest way to do this is with set which takes a python list and removes duplicates, if you think of it from a lay-man’s perspective. What it actually does that’s useful for me is I can do this “unique = set(A) – set(B)” and this will return a set of unique values in set A not found in set B. Reverse the order and you’ll find the same for set B. This simple one liner allows me to get the four groups this way I can track using python twitter unfollower.
Step 1.6: Run the code
The filename is “twitter-followers.py”. If you run it from the terminal this is what you’ll get the first time. A y/n guides prompt that returns relevant info of if your number of followers/following changes who they are.
As you can tell the first line “Loaded previous data” tells you that “save.p” exists in the same folder. The other lines that follow: “New Following”, “New Followers”, “Unfollowers”, “unfollowed”. These will returns the usernames if you have any user that meets that criteria.
At this point in the snapshot below, I’d finished testing “unfollowed” and “new following”. So I had a friend follow me so it could register under “New followers” and then “save the reference point”. Then had him unfollow me and this time come up as an “unfollower”.
If you have multiple users that meet this criteria they’re usernames will be separated by a comma. Now at this point normally, I’d paste the code below here. But given I’m likely to edit it and will forget to edit it here also it’s better to check it out from the repo.
There’s some cases where I prefer how it’s done like here using github gist. Which comes with code highlighting an extra plus. But I think this case it would be a redundant thing to do.
And with that you have your twitter followers checker code.
Mind you I could additionally rewrite the script to check for new followers and follow automatically. Mmmhh probably not how I want to run my twitter account. Same for unfollow anyone who just unfollowed me which isn’t adequate for me. Maybe your tweets are worth it regardless of the follow.
And with that I’ve reached the end of my tutorial for python twitter unfollower. If you want to see the code you can check it out here.
When I started this the first thing that came into mind was how to implement it. I’d already used python-twitter before at the time as a way to automate a tweet once you’ve published an article at a client’s request. So what I needed to find out is how to get something else, a user’s followers and following list.
This then came with another bug I noted that when I ran the script from the terminal it would timeout for 15 minutes. After some research I learnt that this can be lifted to allow instant querying. You’ll need to make a ticket to their support and explain the reason you need to make multiple request. In other words why you’re building your script and if it aligns with their company’s ethos then they’ll allow you to do so and lift the default limit.
The next hurdle came in how to compare the before and after lists. Now there’s the simple way of iterating over the list and finding out if “X” is in the new list if not then it must be removed and vice versa to find new items to the list. But then there’s set which reduces this to a single line but unless your very familiar with using set logic or use it frequently it might come off as counter intuitive so I tinkered with the code till it was working right.
The “bug” your likely to note is when twitter suspends, bans an account you’ll get a username that doesn’t exist that unfollowed you and in the case of a suspension once it’s done you get a follower who doesn’t show on the app.
Also note that pickle is machine specific encoding (to the CPU I believe) so likely a pickle file you created on one PC of one model won’t work on another PC especially if it’s a different model or make.
One last thing I learnt is a user might change their username and that will lead to a new follower and unfollower where you don’t need to unfollow someone or follow the new follower. I’m sure twitter has a user ID that doesn’t change and could then be used together to ignore this.
As it stands the list of changes that could be added to make the code work perfectly is ignoring accounts if they’re banned/suspended to avoid the hiccup of new invisible followers and unfollowers (if you check your current numbers on the app/website they won’t change).
Another way of making it interactive might be to load it into jupyter which can do better at presenting html and that way you can show more info about a user like the profile pic and the bio or that additional info of suspended or blocked.
How about a final cool project of keeping track of people who twitter blocks or suspends, they might be an interesting rebel group to work with, maybe form a meeting place for all of them that might become the birth of a new social media app. Go system wide though and keep track of all tweeps if you want to do that, find the rebels and recruit them, if you like they way they act that it is 🙂 Have fun, it’s the best way to learn.