Created
May 17, 2023 06:55
-
-
Save sangheonhan/b4f2d8b9accfa584f49ae2f518713349 to your computer and use it in GitHub Desktop.
Delete all Retweets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
import time | |
import pprint | |
def get_all_retweets(api, max_tweets=3200): | |
user = api.verify_credentials() | |
retweets = [] | |
retweet_count = 0 | |
# Fetch tweets in pages of 200 (maximum allowed per request) | |
for page in tweepy.Cursor( | |
api.user_timeline, | |
user_id=user.id, | |
count=200, | |
tweet_mode="extended", | |
include_rts=True, | |
).pages(): | |
for tweet in page: | |
if "retweeted_status" in tweet._json: # Check if the tweet is a retweet | |
retweets.append(tweet) | |
retweet_count += 1 | |
# Maximum of 3200 tweets can be fetched from user_timeline, break loop if limit is reached | |
if len(retweets) >= max_tweets: | |
break | |
print(f"Retweet count = {retweet_count}") | |
time.sleep(1) | |
return retweets | |
def delete_retweets(api): | |
retweets = get_all_retweets(api) | |
rate_limit = 240 # Rate limit for destroy_status endpoint | |
safe_limit = int(rate_limit * 0.8) # Use only 80% of the allowed limit | |
sleep_time = (15 * 60) / safe_limit # Calculate sleep time between API calls | |
for retweet in retweets: | |
try: | |
api.destroy_status(retweet.id) | |
print(f"Deleted retweet: {retweet.id}") | |
time.sleep(sleep_time) # Sleep to avoid exceeding the rate limit | |
except tweepy.TweepError as e: | |
print(f"Error deleting retweet: {e}") | |
consumer_key = "your_consumer_key" | |
consumer_secret = "your_consumer_secret" | |
access_token = "your_access_token" | |
access_token_secret = "your_access_token_secret" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
delete_retweets(api) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
certifi==2023.5.7 | |
charset-normalizer==3.1.0 | |
idna==3.4 | |
oauthlib==3.2.2 | |
requests==2.30.0 | |
requests-oauthlib==1.3.1 | |
tweepy==4.14.0 | |
urllib3==2.0.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment