Skip to content

Instantly share code, notes, and snippets.

@ickas
Created January 16, 2023 10:04
Show Gist options
  • Save ickas/7e5c676540479ca6a6870a002a95b8a3 to your computer and use it in GitHub Desktop.
Save ickas/7e5c676540479ca6a6870a002a95b8a3 to your computer and use it in GitHub Desktop.
Check if the original tweet you replied was deleted
# Tweepy is an easy-to-use Python library for accessing the Twitter API
# https://www.tweepy.org
from datetime import datetime, timedelta
import tweepy
import sys, os, time
import json
# The best way to manage your Twitter data is through the archive of all your account data
# Request archive at https://twitter.com/settings/download_your_data
# Read js archive file as json
with open("data/tweets.js") as f:
archive = json.load(f)['window.YTD.tweets.part0']
# Create a new Twitter app at https://developer.twitter.com
# On "keys and tokens" tab generate Consumer Keys and Authentication Tokens
consumer_key="your_api_key"
consumer_secret="you_api_secret"
access_token="your_api_token"
access_token_secret="your_api_access_token"
# Calling the API and set authorization
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Check tweets before a specific date
# Or use a cutoff date based on days cutoff_date = datetime.now() - timedelta(days=30)
# Or use start and end dates after_date = datetime(2017, 12, 31)
before_date = datetime(2009, 1, 1)
# Check how many tweets match the rule
# Use start and end dates to choose between dates interval after_date.timestamp() < [...] < before_date.timestamp()
# Create a new array with matching tweets
matching_tweets = []
for tweet in archive:
if datetime.strptime(tweet["tweet"]["created_at"], "%a %b %d %H:%M:%S %z %Y").timestamp() < before_date.timestamp():
matching_tweets.append(tweet)
total_matching_tweets = len(matching_tweets)
print(f"Start analysing {total_matching_tweets} tweets...")
time.sleep(5)
counter = 0
for tweet in matching_tweets:
counter += 1
id = tweet["tweet"]["id"]
date = tweet["tweet"]["created_at"]
created_at = datetime.strptime(tweet["tweet"]["created_at"], "%a %b %d %H:%M:%S %z %Y").timestamp()
# Check if is a reply
is_reply = tweet["tweet"].get("in_reply_to_status_id_str")
try:
if is_reply:
reply_id = tweet["tweet"]["in_reply_to_status_id_str"]
if created_at > after_date.timestamp() and created_at < before_date.timestamp():
api.get_status(tweet["tweet"]["in_reply_to_status_id_str"])
time.sleep(2)
except tweepy.errors.TweepyException as e:
if e.response.status_code == 404:
print(f"Check: {str(id)} ({counter}/{total_matching_tweets})")
continue
else:
print("Error with: " + str(id))
print("Done! ✅")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment