Retrieve a list of user mentions from a thread of Tweet replies
Tags: python, twitter, tutorial
Published: 2022-01-25 17:10:07

---
title: Retrieve a list of user mentions from a thread of Tweet replies
published: true
description: Python script to extract user mentions from replies to a Tweet of interest. The script will return an ordered list of usernames mentioned (from most to least frequently mentioned) and a list of IDs for the replies.
tags: python, twitter, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmmqzdvh3h7pirr1y0b9.jpg
---

*The code for this tutorial is available on [GitHub](https://github.com/twitterdev/extract-usernames-from-tweet-replies).*

Have you ever wanted a quick way to retrieve all usernames mentioned in a thread of [replies](https://help.twitter.com/en/using-twitter/mentions-and-replies) on Twitter? This [Python script](https://github.com/twitterdev/extract-usernames-from-tweet-replies) does just that using the Twitter API.  

## Set up

1. Clone the repository [here](https://github.com/twitterdev/extract-usernames-from-tweet-replies).

2. Ensure you have a Twitter developer account with access to [GET /2/tweets/search/all](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-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](https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api).

3. 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: 
```python
search_url = "https://api.twitter.com/2/tweets/search/recent"
```

* On line 49, make sure the `max_results` parameter is set to: 
```python
"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 a `400 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.