Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first working version of the script - use at your own risks

  • Loading branch information...
commit a2f21d9925f8591fe91d4b3d87b54c19051dda1b 0 parents
@olivierthereaux authored
Showing with 124 additions and 0 deletions.
  1. +4 −0 credentials.txt
  2. +120 −0 oldtweets.py
4 credentials.txt
@@ -0,0 +1,4 @@
+consumer_key 7ZjTeAwORter132Nsj99GQ
+consumer_secret rY2TLc6ZnI2v346LqKic8pu8g6X76eJJy1Y
+access_token_key 1662-fgMGfgfOmu440jo848436oJK6eFrYqTaOb0oU4A3CPVOsu
+access_token_secret CsFqDQCdhdjURQNwA48x484Mxz5cvAUWkG0
120 oldtweets.py
@@ -0,0 +1,120 @@
+"""
+READ ME FIRST!
+
+This script is a way to back up (and delete, if you so desire) all your tweets beyond the latest 100
+
+This script will work if, and only if, you:
+
+1/ Install python-twitter: http://code.google.com/p/python-twitter/ and dependencies
+2/ go to dev.twitter.com, sign up with your account and create a new app (the details can be bogus, your app will be private)
+3/ copy the consumer key and secret from your app in a credentials file
+4/ go to "my access token" in the (righthand) menu of your app and copy the token and key in a credentials file
+(a credentials file is distributed with this script, as a sample)
+
+"""
+
+import time
+import twitter
+import sys
+import getopt
+
+
+help_message = '''
+oldtweets.py - backup or delete your older tweets
+Based on a script by David Larlet @davidbgk
+
+
+** run it to see the output
+cat credentials | ./oldtweets.py
+
+** Want to backup?
+cat credentials | ./oldtweets.py >> mytweetsbackupfile.txt
+(your oldest tweets will be at the top)
+
+** Want to delete old tweets?
+cat credentials | ./oldtweets.py --delete
+
+** Want to backup and delete?
+cat credentials | ./oldtweets.py --delete >> mytweetsbackupfile.txt
+'''
+
+
+class Usage(Exception):
+ def __init__(self, msg):
+ self.msg = msg
+
+
+def main(argv=None):
+ option_delete = 0
+ if argv is None:
+ argv = sys.argv
+ try:
+ try:
+ opts, args = getopt.getopt(argv[1:], "h:v", ["delete", "help"])
+ except getopt.error, msg:
+ raise Usage(msg)
+
+ # option processing
+ for option, value in opts:
+ if option == "-v":
+ verbose = True
+ if option in ("-h", "--help"):
+ raise Usage(help_message)
+ if option == "--delete":
+ option_delete = 1
+
+
+ except Usage, err:
+ print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
+ print >> sys.stderr, "\t for help use --help"
+ return 2
+ consumer_key = ''
+ consumer_secret = ''
+ access_token_key = ''
+ access_token_secret = ''
+
+ for line in sys.stdin.readlines():
+ params = line.split()
+ if len(params) == 2:
+ if params[0] == "consumer_key":
+ consumer_key = params[1]
+ if params[0] == "consumer_secret":
+ consumer_secret = params[1]
+ if params[0] == "access_token_key":
+ access_token_key = params[1]
+ if params[0] == "access_token_secret":
+ access_token_secret = params[1]
+
+ api = twitter.Api(consumer_key=consumer_key,
+ consumer_secret=consumer_secret,
+ access_token_key=access_token_key,
+ access_token_secret=access_token_secret)
+
+
+ tweets_ids = []
+ tweets_len = 0
+ for i in range(2, 32):
+ tweets_ids += [status.id for status in api.GetUserTimeline(page=i+1, count=100)]
+ # print i, tweets_ids, len(tweets_ids)
+ if tweets_len == len(tweets_ids): # haven't received anything new, we're at the end of the list. stop here
+ break
+ else:
+ tweets_len = len(tweets_ids)
+ # wait a bit, throttled api
+ time.sleep(1)
+
+ # output tweets, delete on demand
+ for tweet_id in tweets_ids[::-1]:
+ # verify
+ print "Tweet id: ", tweet_id, " -- Date: ", api.GetStatus(tweet_id).created_at, " || ", api.GetStatus(tweet_id).text.encode('utf-8')
+ # delete
+ if option_delete == 1:
+ status = api.DestroyStatus(tweet_id)
+ # wait a bit, throttled api
+ time.sleep(1)
+
+
+if __name__ == "__main__":
+ sys.exit(main())
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.