Skip to content

Instantly share code, notes, and snippets.

@thinrhino
Last active August 29, 2015 14:15
Show Gist options
  • Save thinrhino/e86a3542dd9144bac93f to your computer and use it in GitHub Desktop.
Save thinrhino/e86a3542dd9144bac93f to your computer and use it in GitHub Desktop.
A small script to delete all tweets from your twitter stream
# First download the twitter archive
# Get API_KEY and API_SECRET from developer.twitter.com
import os
import json
import glob
import base64
import requests
from requests_oauthlib import OAuth1Session
API_KEY = 'XXX'
API_SECRET = 'XXX'
# For some reason, twitter only allowed me to do a PIN based oAuth
# Hence callback_uri='oob'
twitter = OAuth1Session(client_key=API_KEY,
client_secret=API_SECRET,
callback_uri='oob')
request_token_url = 'https://api.twitter.com/oauth/request_token'
authorization_url = 'https://api.twitter.com/oauth/authorize'
access_token_url = 'https://api.twitter.com/oauth/access_token'
twitter.fetch_request_token(request_token_url)
twitter.authorization_url(authorization_url)
# Open the resulting URL in the browser and copy-paste the token displayed, in the call below
resp = twitter.post('https://api.twitter.com/oauth/access_token', data={'oauth_verifier':'<TOKEN>'}).text
twitter.parse_authorization_response('https://127.0.0.1/callback?%s' % resp)
# Verify the oAuth worked
twitter.get('https://api.twitter.com/1.1/statuses/user_timeline.json', params={'count':100, 'include_rts': True}).json()
# Download your twitter archive from Twitter
all_ids = []
files = glob.glob('~/t_archive/tweets/*.js')
for name in files:
data = json.loads(open(name).read().split('= \n')[1])
for t in data:
all_ids.append(t['id'])
all_ids.sort()
success_deleted = 0
already_delted = 0
errors = []
for id in all_ids:
url = 'https://api.twitter.com/1.1/statuses/destroy/%s.json' % id
resp = twitter.post(url)
if resp.status_code == 200:
success_deleted =+ 1
elif resp.status_code == 404:
already_deleted =+ 1
else:
errors.append(resp.text)
print success_deleted
print already_delted
print errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment