Search code examples
pythontwittertweepy

Checking if tweet is in reply to deleted tweet


I'm writing a program to scrape tweets between two specific dates from a user, while making sure that they are not retweets or replies. I am using snscrape and tweepy.

for i, tweet in enumerate(sntwitter.TwitterSearchScraper('from:' + lines[x] + ' since:' + startDate + ' until:' + endDate).get_items()):
    if tweet.retweetedTweet is None and tweet.inReplyToTweetId is None and tweet.inReplyToUser is None:

This is what I have for the check, however, if the tweet is in reply to a tweet that has been deleted, then the tweet is no longer considered a reply and the check passes as None. Is there a way around this? I'm looking at pulling tweets from large companies like Tesco and Sainsburys and manually sorting through their tweets by hand will be tedious and want to find a way to fix this within the code.

An example of this is this tweet, as the code passes the check for inReplyToTweetId is None

Any help would be greatly appreciated, thank you.


Solution

  • I actually solved this a lot quicker than I thought I would. Turns out, in the tweet object, the mentionedUsers array is empty for these specific tweets, so I added the following if statement which solved the problem:

    if not ('@' in tweet.content and not tweet.mentionedUsers):
    

    This just checks whether the user has a mention (@ symbol) in the actual text of the tweet and also whether the mentionedUsers array is empty to discard it as being a reply.