Twitter Bot to Retweet Every Post of Your Loved Ones
Sit back and relax, your bot will take care of the rest
I know your life is superrrrrr busy!
You don't have time to check your Twitter, see the posts and tweets of your loved ones, like, and retweet them.
But, don’t worry! I have got a solution. Why not use our programming skills and code a bot that will do the work for you?
Sounds interesting, right?
In this post, we’ll create a Twitter Bot to like and retweet tweets using Python.
Let’s start!
We’ll be using Twitter API and Python module Tweepy.
Step 1: Getting the API Keys
To work with the Twitter API, we will need the API keys. Browser to https://developer.twitter.com/en and login with your Twitter account.
Once you’re logged in, it will take you to the below page,
Here you can select the option, Making a bot.
Next, it will ask you for a few details such as your country, why do you want to use the Twitter API, etc. Enter the details required and submit them.
Once done, it will show a developer agreement. Just accept that, and confirm your email.
It will take you back to the landing page. There, you can browse to the Get Started option.
It will take you to the below screen,
Select Create an app option and hit create an app. It will ask for some details of the app you want to create. Simply fill it, and submit them.
Once done, your app will be created. You can browse the option, Keys, and tokens. Here you’ll be able to see your API keys and Access token that we will be using later.
Step 2: Installing the module
We will be using Tweepy — a Python module that helps to interact and work with Twitter API. Hit the below command to install it,
pip install tweepy
Step 3: Importing the required modules
import tweepy
import time
Step 4: Authenticating the app and accessing Twitter data
We’ll authenticate our application using the OAuthHandler. We’ll need to pass our API keys — API Key and Secret API key.
auth = tweepy.OAuthHandler(“YOUR_API_KEY”, “YOUR_SECERET_API_KEY”)
Setting the access token, again we will need our access token and secret access token.
auth.set_access_token(“YOUR_ACCESS_TOKEN”, “SECERET_ACCESS_TOKEN”)
Step 5: Making the API call
Once we are ready with our API keys and access tokens, we can make the API call.
data = tweepy.API(auth, wait_on_rate_limit = True, wait_on_limit_notify= True)
user=api.me()
So that we don’t get blocked by Twitter if we make any request, we’ll set two more parameters such as wait_on_rate_limit and wait_on_limit_notify that will notify us when the limit is reached and will stop making the request for some time.
Step 6: Liking and retweeting
We’ll accept a search term from the user that can be a tag that is used by others. Our bot will search for that tag or term and will like and retweet all those posts. Also, we’ll ask for the number of tweets to be pulled,
search = input(“Enter a term to search:”)
numTweets = int(input(“Enter the number of tweets to pull:”))for tweet in tweepy.Cursor(api.search, search).items(numTweets):
try:
tweet.favourite()
tweet.retweet()
time.sleep(10)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
Here, we use the Cursor class that pulls the data and store it, we loop through it and like and retweet the tweets using the respective methods.
Congratulations! Just run the program! Sit back and relax, let Python do the work for you.
The Whole Code:
import tweepy
import timeauth = tweepy.OAuthHandler(“YOUR_API_KEY”, “YOUR_SECERET_API_KEY”)
auth.set_access_token(“YOUR_ACCESS_TOKEN”, “SECERET_ACCESS_TOKEN”)
data = tweepy.API(auth, wait_on_rate_limit = True, wait_on_limit_notify= True)
user=api.me()search = input(“Enter a term to search:”)
numTweets = int(input(“Enter the number of tweets to pull:”))for tweet in tweepy.Cursor(api.search, search).items(numTweets):
try:
tweet.favourite()
tweet.retweet()
time.sleep(10)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
Want to learn Python and more such tricks using Python? Try Programming Hero, a new way to learn to code in a fun way!