Python script to like the latest tweet of your followers!! | Daily Python #3

Ajinkya Sonawane
Daily Python
Published in
3 min readJan 7, 2020

This article is a tutorial on how to like the latest tweets of all your followers using Python.

This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.

Requirements:

  1. Python 3.0
  2. Pip

Install the following packages:

  1. tweepy — Library that provides easy access to Twitter APIs
pip install tweepy

Let’s start by importing the libraries in our code.

import tweepy
import time

In order to access Twitter APIs, we need to create a Twitter developer account and create an application that will provide us the API keys and Access tokens.

You can go through my previous article — Python script to fetch tweets based on hashtags(#), to understand how to create a twitter application.

Store your API keys and Access tokens in the variables as given below:

consumerKey = “YOUR_CONSUMER_KEY”
consumerSecret = “YOUR_CONSUMER_SECRET_KEY”
accessToken = “YOUR_ACCESS_TOKEN”
accessTokenSecret = “YOUR_ACCESS_TOKEN_SECRET”

First, we create an object of the OAuthHandler provided by ‘tweepy’. Here, we use the Consumer Key and the Consumer Secret Key that we got after creating an application on Twitter. We also, provide this OAuthHandler the access tokens that we generated for our application.

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)auth.set_access_token(accessToken, accessTokenSecret)

Now, let’s create the API object which will be used to fetch your followers list and then to like the latest tweet of each of your followers. To create an object of the API we simply provide the ‘auth’ variable to ‘tweepy’.

api = tweepy.API(auth)

Fetching the followers list

For fetching the followers list we use the followers API provided by Twitter.

followers = []for page in tweepy.Cursor(api.followers, screen_name='@sonawaneajinks', wait_on_rate_limit=True,count=200).pages():    try:
followers.extend(page)
except tweepy.TweepError as e:
print("Going to sleep:", e)
time.sleep(60)

The ‘screen_name’ input parameter is to specify your Twitter account’s username. ‘count’ limits the results of the request. ‘wait_one_rate_limit’ — Whether or not to automatically wait for rate limits to replenish.
Change the count limit as per your account’s number of followers.

The followers are stored in the ‘followers’ list. We now traverse this list and fetch the latest tweet of each of these followers. We then like the tweet using ‘api.create_favorite()’.

for user in followers:
try:
print(user.name)
tweet = api.user_timeline(id = user.id, count = 1)[0]
api.create_favorite(tweet.id)
print('----Liking Tweet-----')
print(tweet.text)
except:
pass

The try and except blocks prevent the loop from breaking out if there is an error. Usually, the API object returns an error if the tweet is already liked by you. Adding the except block and passing to the next iteration handles this exception.

Snip of the output

This was a really simple tutorial on how to like the latest tweet of your followers. You can play around with ‘tweepy’ and like trending tweets as well. I hope this article was helpful, do leave some claps if you liked it.

Open to suggestions, do not hesitate to give your valuable feedback.

--

--