Python Friday #119: Follow, Like and Retweet With Tweepy

Tweepy is not only good for housekeeping, but we can also use it for more positive interactions on Twitter. Let’s see how we can like and retweet tweets.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Preparation

As with the examples before, we need the user authentication tokens to work on behalf of that user. We use the V2 endpoints for this post:

As a reminder, with this code you can get the user id from a screen name:

id of user ‘@ThePSF’: 63873759

 

Follow an account

You can follow an account with the follow() method:

user followed? True

 

Unfollow an account

If you have called the follow() method with the wrong id, you can correct this with the unfollow() method.

user followed? False

 

Like a tweet

We can like a tweet with the method like():

liked the tweet? True

 

Retweet a tweet

The names for the methods in Tweepy are predictable, that means we can us the retweet() method to make a retweet:

retweeted the tweet? True

 

Send a tweet with the V2 client

So far, we only used the V1 API to send a tweet. With the V2 client we can write a tweet with the create_tweet() method:

Tweet #1501292904916021249 created

The V2 endpoint offers all the new features that Twitter introduced lately. The Tweepy documentation offers a good overview on all the settings you can use.

 

Working with bookmarks

Twitter added an endpoint for bookmarks that I explore in this post.

Bookmarks are one of the few features of Twitter without any API endpoints. Therefore, we cannot use Tweepy or any other Twitter client to access our bookmarked tweets.

However, since Twitter has a web interface that can show you the bookmarks, you can use the developer tools in the browser and some Python code to extract the interesting parts of your bookmarks. If you are interested in this approach, you should read “Exporting your Twitter bookmarks in markdown file” by Divyajyoti Ukirde. She has code samples that you can modify to reflect the current state of the “API”.

 

Parting Thoughts

Over the last 10 weeks, I’ve been working intensively with the Twitter API and Tweepy. I have covered all the parts that I think you need to work effectively with Tweepy. Some parts like the filtering rules for streams offer nasty surprises.

Nonetheless, the Twitter API works well, and you should now have all the pieces together to create your own Twitter bot. For the time being, this is where I stop. I figured out what I need to know about the capabilities of the Twitter API and now is time for something different.

I hope you enjoyed the journey.

2 thoughts on “Python Friday #119: Follow, Like and Retweet With Tweepy”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.