Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Twitter API application for remove the tweets from the last one to the specified by id.
Dangerous tool because it deletes the tweets!
Uncomment, correct and use it carefully.
import sys
import json
import oauth2 as oauth
import urllib2 as urllib
access_token_key = "14408321-Ol6zBLXFWCIxBTWc20PUi96T82y5SFjVneVTQ8fRk"
access_token_secret = "HacXa7Nfca4FrGTuZtiCZbqpxcIbFiFfkq5pW1A2Cw"
consumer_key = "56CLNBdYe9RsNU8soQwWg"
consumer_secret = "Phi0K9FsFXJUDrJ6riN4BuptbyFL1s4IdcouCHKHg"
_debug = 0
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_handler = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)
Construct, sign, and open a twitter request
using the hard-coded credentials above.
def twitterreq(url, method, parameters):
req = oauth.Request.from_consumer_and_token(oauth_consumer,
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if method == "POST":
encoded_post_data = req.to_postdata()
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
response =, encoded_post_data)
return response
def fetchTweets(page):
url = "" + str(page)
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
return json.loads(line)
def removeTweet(id):
url = ""+id+".json"
parameters = []#{'id': id}
response = twitterreq(url, "POST", parameters)
# get first several pages and remove all tweets till 316103416294297600
if __name__ == '__main__':
for page in xrange(1, 50):
for tweet in fetchTweets(page):
print("%s: %s" % (tweet['id_str'], tweet['text'].encode('utf-8')))
# this is dangerous place. here the process should be stopped.
#if '316103416294297600' == tweet['id_str']:
# sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.