""" * * twitter_delete_direct_messages.py * Quick n dirty script to Delete all the Direct Messages sitting on your twitter account. * Author: Gubatron - http://twitter.com/gubatron * * ---------------------------------------------------------------------------- * Released under "THE TWITTER-WARE LICENSE" (Revision 1): * http://twitter.com/gubatron wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If you find it useful, follow me * on twitter at http://twitter.com/gubatron or just send me an update letting * me know you deleted all your messages thanks to me. * ---------------------------------------------------------------------------- * """ import urllib import urllib2 import base64 def getDirectMessages(username,password): restCmdUrl = "http://twitter.com/direct_messages.json" req=urllib2.Request(restCmdUrl) base64string = base64.encodestring('%s:%s' % (username, password))[:-1] req.add_header("Authorization", "Basic %s" % base64string) try: handle = urllib2.urlopen(req) except IOError,e: print "\nERROR: Something went wrong with your username and password. If your username and password are fine, then just wait 15 minutes to try again, Twitter has limits on the number of requests per hour\n\nWritten by Gubatron - http://twitter.com/gubatron" import sys sys.exit(1) pass else: #read page output = handle.read() #convert JSON String to Python evaluable string #and pray to got that twitter won't output something evil output = output.replace('true','True') output = output.replace('false','False') return eval(output) def destroyMessage(username,password,message): restCmdUrl = "http://twitter.com/direct_messages/destroy/%s.json" % message['id'] data = urllib.urlencode({'foo':'bar'}) #this method requires POST req=urllib2.Request(restCmdUrl,data) #passing data, forces a POST on urlb2.Request() base64string = base64.encodestring('%s:%s' % (username, password))[:-1] req.add_header("Authorization", "Basic %s" % base64string) handle=None try: handle = urllib2.urlopen(req) except IOError,e: print "\nOOOOPS:\nMaybe the Twitter Whale is out there with the birds. Wait a few minutes and try again\n\nWritten by Gubatron - http://twitter.com/gubatron\n" import sys sys.exit(1) pass else: #read page output = handle.read() print "\nMESSAGE %d DELETED!\n" % message['id'] print output def deleteMessages(username,password,messages,senderNameFilter=None): ''' Will delete a message If the senderNameFilter variable has something, we'll only delete messages that match it. ''' for m in messages: if senderNameFilter is not None: if m['sender']['screen_name'] == senderNameFilter: destroyMessage(username,password,m) else: continue else: destroyMessage(username,password,m) if __name__=="__main__": print """ ------------------------------------------------------------------------------------ DELETE ALL THOSE DAMN DIRECT MESSAGES: ------------------------------------------------------------------------------------ Released under "THE TWITTER-WARE LICENSE" (Revision 1): Greenpoint/Brooklyn, NY - Feb Friday the 13th 2009 Written by @gubatron. (http://twitter.com/gubatron) As long as you retain this notice you can do whatever you want with this stuff. If you find it useful, follow me on twitter at http://twitter.com/gubatron or just send me an update letting me know you deleted all your messages thanks to me. ------------------------------------------------------------------------------------ Now let's start wiping all those nasty direct messages sitting there! """ username = raw_input("Enter Your Twitter Username: ") password = raw_input("Enter Your Twitter Password: ") print "\n"*200 print "Want to delete messages ONLY FROM 1 SENDER? (optional - [Press Enter] to delete all)" senderName = raw_input("Enter sender screen name:") if senderName=="": senderName = None messages = getDirectMessages(username,password) while messages is not None or len(messages): deleteMessages(username,password,messages,senderName) messages = getDirectMessages(username,password) if len(messages)==0 or messages is None: print "No more messages, Yay!!\n" break print "Now send me a Twitter Holla at http://twitter.com/gubatron or Follow me if you liked it!\n" print "Written by Gubatron - http://twitter.com/gubatron - Feb.2008/Brooklyn,NY\n\n"