Instantly share code, notes, and snippets.

Embed
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,
token=oauth_token,
http_method=method,
http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
return response
def fetchTweets(page):
url = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=kikudjiro&page=" + str(page)
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
return json.loads(line)
def removeTweet(id):
url = "http://api.twitter.com/1.1/statuses/destroy/"+id+".json"
parameters = []#{'id': id}
response = twitterreq(url, "POST", parameters)
# get first several pages and remove all tweets till 316103416294297600
if __name__ == '__main__':
#removeTweet('332443311262547969')
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()
#removeTweet(tweet['id_str'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment