Skip to content

Instantly share code, notes, and snippets.

@davidbgk
Created April 15, 2010 07:59
Show Gist options
  • Save davidbgk/366819 to your computer and use it in GitHub Desktop.
Save davidbgk/366819 to your computer and use it in GitHub Desktop.
Delete your old tweets
"""
Delete your old tweets (by old I mean all except latest 100)
without removing favorites of your own tweets and DM.
1/ Install python-twitter: http://code.google.com/p/python-twitter/ as ptwitter
2/ Patch it with http://code.google.com/p/python-twitter/issues/detail?id=60
3/ Populates USERNAME and PASSWORD
Note that you're limited to 150 calls to the API per hour and that you can't
retrieve more than the 3200 latest tweets, it means that even if you relaunch
the script a lot of times you'll be screwed if you've got more that
3200 favorites of your own tweets.
Thanks to @Pierce_D and @brutasse, I'll rewrite the end of the script
given those recent info.
"""
import time
from sets import Set
from ptwitter import twitter
USERNAME = 'foo'
PASSWORD = 'bar'
api = twitter.Api(username=USERNAME, password=PASSWORD)
public_api = twitter.Api()
## retrieve all favs
def get_favorites_ids(user):
"""
Retrieve all favorites ids for the given user.
"""
favorites_ids = []
favs_len = 0
for i in range(100):
favorites_ids += [status.id for status in public_api.GetFavorites(user=user, page=i)]
print i, favorites_ids, len(favorites_ids)
if favs_len == len(favorites_ids):
break
else:
favs_len = len(favorites_ids)
# wait a bit, throttled api
time.sleep(1)
return favorites_ids
favorites_ids = get_favorites_ids(user=USERNAME)
# You can cache the result if you plan multiple launches
#favorites_ids = ['result of get_favorites_ids']
# retrieve tweets to delete
def get_all_tweets_ids(user):
"""
Retrieve all tweets ids for a given user, except the latest 100 (first page).
"""
tweets_ids = []
tweets_len = 0
for i in range(9, 18):
tweets_ids += [status.id for status in public_api.GetUserTimeline(screen_name=user, page=i+1, count=100)]
print i, tweets_ids, len(tweets_ids)
if tweets_len == len(tweets_ids):
break
else:
tweets_len = len(tweets_ids)
# wait a bit, throttled api
time.sleep(1)
return tweets_ids
tweets_ids = get_all_tweets_ids(USERNAME)
# cache
#tweets_ids = ['result of get_all_tweets_ids']
# because twitter api is unstable, really
tweets_already_done = ['printed ids deleted']
tweets_todelete_ids = list(Set(tweets_ids)-Set(favorites_ids)-Set(tweets_already_done))
print len(tweets_todelete_ids)
# delete tweets
for tweet_id in tweets_todelete_ids[::-1]:
# verify
#print public_api.GetStatus(tweet_id).text
# delete
status = api.DestroyStatus(tweet_id)
print tweet_id
# wait a bit, throttled api
time.sleep(1)
@brunobord
Copy link

starting from June, 30th, you won't be able to log into the Twitter API this way. You'll have to use OAuth.

http://apiwiki.twitter.com/OAuth-FAQ

@davidbgk
Copy link
Author

You're absolutely right :/

@karlcow
Copy link

karlcow commented Mar 2, 2011

To link threads :) Python wrapper using OAuth http://code.google.com/p/python-twitter/

@davidbgk
Copy link
Author

Checks https://github.com/olivierthereaux/oldtweets for a maintained version of the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment