The code for this tutorial is available on GitHub.
Have you ever wanted a quick way to retrieve all usernames mentioned in a thread of replies on Twitter? This Python script does just that using the Twitter API.
Set up
Clone the repository here.
Ensure you have a Twitter developer account with access to GET /2/tweets/search/all. If you don't have access to this endpoint, follow the instructions below to use the recent search endpoint instead. More information on getting access to the API is available here.
In the root directory, rename
.env_example
to.env
and add the credentials for your developer App in between the""
. Remember to add.env
to your.gitignore
file to keep your credentials secure.
Running the code
Use the following command to run the code:
$ python3 replies.py -t <TWEET-ID>
The following flags are available to use:
Flags | |||
---|---|---|---|
-t |
Tweet ID | Required | ID of the Tweet for which you want to pull data. |
-h |
Help | Optional | View a list of available flags and a description for each of these. |
-s |
Start time | Optional | The oldest UTC timestamp from which the replies will be provided. Format: YYYY-MM-DDTHH:mm:ssZ; for example: 2021-12-04T01:30:00Z. If unspecified, will default to returning replies from up to 30 days ago. |
-e |
End time | Optional | The newest, most recent UTC timestamp to which the replies will be provided. Format: YYYY-MM-DDTHH:mm:ssZ; for example: 2021-12-04T01:30:00Z. If unspecified, will default to [now - 30 seconds]. |
Output
Returned in the command life interface
- Ordered dictionary containing a list of all usernames present in the replies and the number of times each username was mentioned. Note: the user who authored the original Tweet is not included.
- Number of replies to the Tweet.
- Number of usernames mentioned in these replies.
- Number of requests made to the Search Tweets endpoint to pull all replies.
Returned in a new file
- A new file,
replies.txt
, will be created. This file will contain a list of Tweet IDs for the replies.
What if I don't have access to Twitter's full-archive search endpoint?
If you only have access to the recent search endpoint, you can still use this script. Simply make the following two modifications to replies.py
:
- On line 63, change the endpoint to:
search_url = "https://api.twitter.com/2/tweets/search/recent"
- On line 49, make sure the
max_results
parameter is set to:
"max_results": "500"
- When you run the code, if you specify a start time and/or end time using the
-s
and-e
flags, these must be within the last seven days, otherwise you will get back a400 Bad Request
error.
You can read more about Search Tweets and the difference between the recent search and full-archive search endpoints here: https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction
Use case examples
The above can be helpful in the following scenarios:
- You've asked a question on Twitter and want to quickly retrieve a list of all accounts mentioned in the replies.
- Identify a list of influencers, as mentioned in a conversation thread.