Search code examples
twitterapi-python

Is there a way to access the original tweets from reply tweets?


I am currently working on a project using the twitter api and I have a dataset of reply tweets containing a certain word. Is there a way to access the original i.e. main tweets from these reply tweets?


Solution

  • Welcome to StackOverflow, serenayyildiz

    You could find the original tweet from one of its replies using tweepy library:

    auth = tweepy.OAuthHandler(key, secret)
    auth.set_access_token(token, token_secret)
    api = tweepy.API(auth)
    
    with open('tweets.csv','w') as f1:
        writer = csv.writer(f1)
    
        for reply_id in list_of_replies:
            reply_tweet = api.get_status(id=reply_id)
            original_tweet_id = reply_tweet.in_reply_to_status_id
            original_tweet = api.get_status(original_tweet_id )
            row = original_tweet.text
            writer.writerow(row)
    

    Here are the list of attributes in the Status object, returned when you call api.get_status:

    • created_at : The time the status was posted.
    • id : The ID of the status.
    • id_str : The ID of the status as a string.
    • text : The text of the status.
    • entities : The parsed entities of the status such as hashtags, URLs etc.
    • source : The source of the status.
    • source_url : The URL of the source of the status.
    • in_reply_to_status_id : The ID of the status being replied to.
    • in_reply_to_status_id_str : The ID of the status being replied to in as a string.
    • in_reply_to_user_id : The ID of the user being replied to.
    • in_reply_to_user_id_str : The ID of the user being replied to as a string.
    • in_reply_to_screen_name : The screen name of the user being replied to
    • user : The User object of the poster of the status.
    • geo : The geo object of the status.
    • coordinates : The coordinates of the status.
    • place : The place of the status.
    • contributors : The contributors of the status.
    • is_quote_status : Indicates whether the status is a quoted status or not.
    • retweet_count : The number of retweets of the status.
    • favorite_count : The number of likes of the status.
    • favorited : Indicates whether the status has been favourited by the authenticated user or not.
    • retweeted : Indicates whether the status has been retweeted by the authenticated user or not.
    • possibly_sensitive : Indicates whether the status is sensitive or not.
    • lang : The language of the status.