DEV Community

Suhem Parack
Suhem Parack

Posted on

How to analyze edited Tweets with the Twitter API v2 using Python

As announced in this blog post, the Twitter API v2 supports the ability to get metadata about edited Tweets. In this short guide, I will showcase how developers and researchers can get information about edited Tweets from the Twitter API v2 in Python using Tweepy.

Prerequisite

In order to use the Twitter API v2, you need to apply for a Twitter developer account. Once you have an approved Twitter developer account, follow the instruction here to obtain your BEARER TOKEN that you will use to connect to the Twitter API v2 in you code in Python. We will be using the Tweepy package in Python to get Tweets from the Twitter API v2, so you will need to have Python as well as the Tweepy package installed on your machine. Instructions on installing the Tweepy package can be found in this tutorial.

Some fundamental background on Edit Tweets

Currently, some Tweets on Twitter Blue are editable. These Tweets are editable for 30 minutes and up to 5 times. Each time a Tweet is edited, a new Tweet ID is created for the edited Tweet.

There are three components to a Tweet’s edit history:

  1. By default, the Tweet response from the Twitter API v2 will contain an array of Tweet IDs that are part of a Tweet’s edit history. This information is specified by the edit_history_tweet_ids, which is a default field in the Tweet response. This array will contain at least one ID, the ID of the original, unedited Tweet. When there is only one ID that means the Tweet has no edit history.

  2. You can get information such as whether a Tweet was editable at the time it was created, how much time (if any) is remaining for a Tweet to be edited, and how many edits remain. This information is specified by requesting the edit_controls field in the tweet.fields.

    {
    "edits_remaining": "5",
    "is_edit_eligible": true,
    "editable_until": "2022-09-30T18:30:01.000Z"
    }
    
  3. Finally, you can get the Tweet objects for each Tweet in a Tweet’s edit history, by specifying the edit_history_tweet_ids using the expansion parameter

Also, while most Tweets are eligible for edit, Tweets such as those with polls, nullcast, promoted etc. are not eligible for edit. The is_edit_eligible field in edit_controls will indicate this. Full list of editable Tweet categories can be found here.

Getting Edit Tweets metadata with Tweepy in Python

In the example below, the Tweet with ID 1575590109000323073 is a Tweet that has been edited. By default, the edit_history_tweet_ids will be returned containing a list of Tweet IDs associated with the revised version(s) of this Tweet. To get information such as how many more edits remaining, how much more time left to make edits to this Tweet, we include the edit_controls field. We are also including the created_at field to get the timestamp for when the Tweet was created. We simply print the Tweet ID, text, the creation time, edit controls related information and the list of Tweet IDs associated with this Tweet (in terms of revisions) to the console.

import tweepy

# Replace bearer token below
client = tweepy.Client(bearer_token='REPLACE_ME')

response = client.get_tweet(id='1575590109000323073', tweet_fields=['edit_controls,created_at'])

tweet = response.data

print(tweet.id)
print(tweet.text)
print(tweet.created_at)
print(tweet.edit_controls)
print(tweet.edit_history_tweet_ids)
Enter fullscreen mode Exit fullscreen mode

The response will look something like this:

1575590109000323073
hello

this is a test to make sure the edit button works https://t.co/NBkYlCIPrA
2022-09-29 20:55:29+00:00
{'edits_remaining': '4', 'is_edit_eligible': True, 'editable_until': '2022-09-29T21:25:29.000Z'}
['1575590109000323073', '1575590534529556480']
Enter fullscreen mode Exit fullscreen mode

Because this Tweet is not a poll, nullcast, promoted Tweet etc., is_edit_eligible is marked as true.

If you want the Tweet objects for each Tweet in a Tweet’s edit history, you will have to use the edit_history_tweet_ids expansion as shown below:

import tweepy

# Replace bearer token below
client = tweepy.Client(bearer_token='REPLACE_ME')

response = client.get_tweet(id='1575590109000323073', tweet_fields=['edit_controls,created_at'],
                            expansions=['edit_history_tweet_ids'])

for tweet in response.includes['tweets']:
    print(tweet.id)
    print(tweet.text)
    print(tweet.created_at)
Enter fullscreen mode Exit fullscreen mode

The response will look something like this:

hello

this is a test to make sure the edit button works https://t.co/NBkYlCIPrA
2022-09-29 20:55:29+00:00
1575590534529556480
hello

this is a test to make sure the edit button works, we’ll let you know how it goes https://t.co/XbZmX572Xc
2022-09-29 20:57:11+00:00
Enter fullscreen mode Exit fullscreen mode

Checking if a Tweet can be edited any longer

In order to check if the Tweet is editable any longer, we can check if:

  • edits_remaining is greater than 0 AND
  • editable_until is in the future from the current time (this will also give us how many more minutes left until a Tweet can no longer be edited)

The example below shows how you can do this with Tweepy:

import tweepy
from datetime import datetime

# Replace bearer token below
client = tweepy.Client(bearer_token='REPLACE_ME')

response = client.get_tweet(id='1575590109000323073', tweet_fields=['edit_controls,created_at'],
                            expansions=['edit_history_tweet_ids'])

tweet = response.data

edits_remaining = tweet.edit_controls['edits_remaining']

editable_until = tweet.edit_controls['editable_until']

time_now = datetime.now()

formatted_time_now = time_now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

if edits_remaining == 0:
    print("This Tweet can no longer be edited")
else:
    if editable_until[:-1] < formatted_time_now:
        print("This Tweet can no longer be edited")
    else:
        print("This Tweet can be edited until {}".format(editable_until))
Enter fullscreen mode Exit fullscreen mode

How to check if a Tweet with a given ID is the most recent version of a Tweet

If a Tweet has edits, the edit_history_tweet_ids will have more than one Tweet ID. So, we will simply check if the length of this array is equal to 1. If it is, that means there are no edits for this Tweet. If the length of this array is greater than 1, then the last Tweet ID in this array is the latest 'edited' Tweet.

import tweepy

# Replace bearer token below
client = tweepy.Client(bearer_token='REPLACE_ME')

response = client.get_tweet(id='1575590109000323073')

tweet = response.data

edit_history_tweet_ids = tweet.edit_history_tweet_ids

if len(edit_history_tweet_ids) == 1:
    print("There are no edits to this Tweet")
else:
    print("The latest version of this Tweet is {}".format(edit_history_tweet_ids[-1]))
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial is helpful in studying and understanding edited Tweets. Feel free to reach out to me with any questions and feedback!

Top comments (0)