Feral Cat

Building just because.
python

Making A Twitter Bot With Tweepy

Want help making a Twitter bot with Tweepy? It’s actually pretty easy after you apply for a developer’s account and setup an app on Twitter’s side. You can do the application and setup here.

My Computer Setup

  • Python3 (Finally getting used to adding parentheses around my print statements )
  • macOS

Getting your Environment Setup

You can find some info on installing pip here. The article actually covers pip installation for Python2 and the Python3 pip installation slightly differs. I’ll be sure to add and link another article in the near future. So assuming you have pip for python3 installed, execute the following to get the latest tweepy package up and running:

sudo pip3 install tweepy

Making A Twitter Bot Like Tweets With Tweepy

Once the developer account is all squared away and you’ve collected your consumer key, consumer secret, access token, and access token secret you can do the following to like 7 different tweets. We’ll pull tweets that are hash tagged Tech, check if the user of each tweet has more than 0 followers, and check if we’ve liked or favorited the tweet before to avoid an error. We then have to sleep for .2 seconds to avoid being blocked or flagged by Twitter as a bot.

import tweepy
import time
consumer_key = 'yourConsumerKey'
consumer_secret = 'yourConsumerSecret'
access_token = 'yourAccessToken'
access_token_secret = 'yourAccessTokenSecret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
# This is where you'll define what you want to search for, notice tech
# You can change ...followers_count > 0 to the amount of friends
# you want the Twitter users to have
for tweet in tweepy.Cursor(api.search, q='#Tech', rpp=100, result_type="mixed", include_entities=True).items(7):
    if tweet.user.followers_count > 0:
        if not tweet.favorited:
            tweet.favorite()
            time.sleep(.2)

Code Explanation

We import tweepy which does the interaction with Twitter and ‘time’ to pace our code and avoid getting flagged. The following four lines are strings you pull from the Twitter developer platform.

consumer_key = 'yourConsumerKey'
consumer_secret = 'yourConsumerSecret'
access_token = 'yourAccessToken'
access_token_secret = 'yourAccessTokenSecret'

We then create an auth object, set the access token, and create an api object using the auth object as a param. The forloop passes through each tweet that we find from our search on #Tech. The result_type=’Mixed’ parameter makes the API return a mix of new and popular tweets. That’s then followed by two if statements that filter the items that are returned to try avoid bots and tweets we’ve already interacted with. You can find more tweet methods here.

I’ll probably follow up with more complicated scripts in the future. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *