Skip to content

Instantly share code, notes, and snippets.

@chrisalbon
Last active February 25, 2022 08:01
Show Gist options
  • Select an option

  • Save chrisalbon/b9bd4a6309c9f5f5eeab41377f27a670 to your computer and use it in GitHub Desktop.

Select an option

Save chrisalbon/b9bd4a6309c9f5f5eeab41377f27a670 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisalbon revised this gist Jul 5, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions tweet_deleting_script.py
    Original file line number Diff line number Diff line change
    @@ -45,11 +45,11 @@ def wipe(account_name=USER_NAME, favorite_threshold=100, days=62):
    # Get whether you have favorited the tweet yourself
    status_favorited = status._json['favorited']

    # If the difference between the current datetime and the tweet's
    # If the difference between the current datetime and the tweet's
    # is more than a day threshold
    if (datetime.utcnow() - status_date).days > days:
    # If the number of favorites is over the favorite threshold
    if status_favorites > favorite_threshold:
    # If the number of favorites is lower than the favorite threshold
    if status_favorites < favorite_threshold:
    # If you haven't favorited the tweet yourself
    if status_favorited == False:
    # Delete the tweet
    @@ -58,4 +58,4 @@ def wipe(account_name=USER_NAME, favorite_threshold=100, days=62):

    # Run main function
    if __name__ == '__main__':
    wipe(account_name=USER_NAME, favorite_threshold=100, days=62)
    wipe(account_name='chrisalbon', favorite_threshold=100, days=62)
  2. chrisalbon renamed this gist Jul 4, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. chrisalbon created this gist Jul 4, 2020.
    61 changes: 61 additions & 0 deletions tweet_deleting_script
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # -*- coding: utf-8 -*-

    """ Deletes all tweets below a certain retweet threshold.
    """

    import tweepy
    from datetime import datetime

    # Constants
    CONSUMER_KEY = ''
    CONSUMER_SECRET = ''
    ACCESS_TOKEN = ''
    ACCESS_SECRET = ''
    USER_NAME = ''

    # Connect To Your Twitter Account via Twitter API
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

    api = tweepy.API(auth,
    wait_on_rate_limit=True,
    wait_on_rate_limit_notify=True,
    retry_count=3,
    retry_delay=5,
    retry_errors=set([401, 404, 500, 503]))

    # For the account name
    def wipe(account_name=USER_NAME, favorite_threshold=100, days=62):
    # Get the current datetime
    current_date = datetime.utcnow()

    # For each tweet
    for status in tweepy.Cursor(api.user_timeline, screen_name='@'+account_name).items():
    # Get the tweet id
    status_id = status._json['id']

    print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), 'Examining', status_id)

    # Get the number of favorites
    status_favorites = status._json['favorite_count']

    # Get the datetime of the tweet
    status_date = datetime.strptime(status._json['created_at'], '%a %b %d %H:%M:%S +0000 %Y')

    # Get whether you have favorited the tweet yourself
    status_favorited = status._json['favorited']

    # If the difference between the current datetime and the tweet's
    # is more than a day threshold
    if (datetime.utcnow() - status_date).days > days:
    # If the number of favorites is over the favorite threshold
    if status_favorites > favorite_threshold:
    # If you haven't favorited the tweet yourself
    if status_favorited == False:
    # Delete the tweet
    api.destroy_status(status_id)
    print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), 'Deleting', status_id)

    # Run main function
    if __name__ == '__main__':
    wipe(account_name=USER_NAME, favorite_threshold=100, days=62)