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.
Equipment
- Python
- Tweepy.
- A new Twitter account which is following the people you wish to track.
- A Twitter developer account & API Keys either in your name, or your new accounts name.
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 http://apps.twitter.com 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")
text_file.write(data)
text_file.close()
if 'delete' in data:
# Deleted Tweets look like this
#{"delete":{"status":{"id":123456789,"user_id":99999999,"id_str":"123456789",
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")
text_file.write(data)
text_file.close()
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())
twitterStream.userstream("with=following")
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...
What links here from around this blog?