Track deletions on Twitter - AKA build your own Politwoops

Twitter, as part of its never-ending quest to alienate users and appease the rich and powerful, have shut down Politwoops accounts. Politwoops monitored politicians' Twitter accounts and noted when they deleted a tweet.

Most of the time deletions were done for the same reason we all deleted content - mispellings, broken links, etc - but occaisionally they caught politicians attempting to flush history down the memory hole.

If you're a dab hand with Python, and have a server upon which to run code, it's fairly easy to make an ersatz Politwoops service of your very own.


The Script

Once you have all these, here is a very quick and dirty script I knocked up one evening. It works - but it's not pretty. Don't like it? Fork it on GitHub!

It saves every Tweet made by the accounts you follow. If it sees a Tweet being deleted, it makes a note of it.

import sys
import json
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener

# Go to and create an app.
# The consumer key and secret will be generated for you after
consumer_key        = 'aaaaaaaaaaa'
consumer_secret     = 'bbbbbbbbbbb'
access_token        = 'ccccccccccc'
access_token_secret = 'ddddddddddd'

# Set up the authorisation to use the Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# Handle the output generated from the stream
class listener(StreamListener):

    def on_data(self, data):
        # Raw output to screen.
        print data

        # Convert the message to JSON
        json_data = json.loads(data)

        if 'id_str' not in json_data:
            # If this isn't a status, do nothing.
            print "no ID"
        else :
            print json_data['id_str']
            # Write a file "123456789.json" containing the Tweet and all the metadata
            text_file = open(json_data['id_str'] + ".json", "w")

        if 'delete' in data:
            # Deleted Tweets look like this
            print "DELETED!"
            # Write a file "123456789-DELETED.json" containing the metadata. Do NOT delete the original Tweet
            text_file = open(json_data['delete']['status']['id_str'] + "-DELETED.json", "w")

        return True

    def on_error(self, status):
        print status

#   Start consuming from the stream.  This will get all the Tweets & Deletions from the users the user is following.
twitterStream = Stream(auth, listener())

The latest version can be found on GitHub.

As I say, this is very basic - but it does work. It will faithfully track every Tweet deleted by those accounts you follow. Use it wisely...


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.