Skip to content

Instantly share code, notes, and snippets.

@intentionally-left-nil
Forked from flesueur/deloldtweets.py
Last active July 27, 2022 10:33

Revisions

  1. Anil Kulkarni revised this gist Jul 29, 2019. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions deloldtweets.py
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@

    import json
    from datetime import datetime, timedelta, timezone
    import os
    from os import path

    consumer_key = ''
    consumer_secret = ''
    @@ -34,14 +36,22 @@

    print(cutoff_date)

    fp = open("tweet.js","r")
    myjson = json.load(fp)
    data_dir = path.join(os.getcwd(), 'data', 'js', 'tweets')

    for tweet in myjson:
    d = datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y")
    tweets = []

    for filename in os.listdir(data_dir):
    with open(path.join(data_dir, filename), 'r', encoding='UTF-8') as fp:
    js_file = fp.read()
    contents = json.loads(js_file[js_file.find('\n') + 1:])
    tweets.extend(contents)

    for tweet in tweets:
    d = datetime.strptime(tweet['created_at'], "%Y-%m-%d %H:%M:%S %z")
    if d < cutoff_date:
    print(tweet['created_at'] + " " + tweet['id_str'])
    try:
    api.destroy_status(tweet['id_str'])
    print(tweet['created_at'] + " " + tweet['id_str'])
    except:
    pass
    print("failed to delete " + tweet['id_str'])
    pass
  2. @flesueur flesueur created this gist Jun 26, 2018.
    47 changes: 47 additions & 0 deletions deloldtweets.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/python3
    # Largely copied from http://www.mathewinkson.com/2015/03/delete-old-tweets-selectively-using-python-and-tweepy
    # However, Mathew's script cannot delete tweets older than something like a year (these tweets are not available from the twitter API)
    # This script is a complement on first use, to delete old tweets. It uses your twitter archive to find tweets' ids to delete
    # How to use it :
    # - download and extract your twitter archive (tweet.js will contain all your tweets with dates and ids)
    # - put this script in the extracted directory
    # - complete the secrets to access twitter's API on your behalf and, possibly, modify days_to_keep
    # - delete the few junk characters at the beginning of tweet.js, until the first '[' (it crashed my json parser)
    # - review the script !!!! It has not been thoroughly tested, it may have some unexpected behaviors...
    # - run this script
    # - forget this script, you can now use Mathew's script for your future deletions
    #
    # License : Unlicense http://unlicense.org/

    import tweepy

    import json
    from datetime import datetime, timedelta, timezone

    consumer_key = ''
    consumer_secret = ''
    access_token = ''
    access_token_secret = ''
    days_to_keep = 365

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)



    cutoff_date = datetime.now(timezone.utc) - timedelta(days=days_to_keep)

    print(cutoff_date)

    fp = open("tweet.js","r")
    myjson = json.load(fp)

    for tweet in myjson:
    d = datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y")
    if d < cutoff_date:
    print(tweet['created_at'] + " " + tweet['id_str'])
    try:
    api.destroy_status(tweet['id_str'])
    except:
    pass