Created
August 18, 2013 05:12
-
-
Save xstevens/6259999 to your computer and use it in GitHub Desktop.
A python script to erase all posts from a Tumblr blog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyblr | |
import oauth2 as oauth | |
import urllib | |
import urlparse | |
import time | |
''' | |
!WITH PROPER CREDENTIALS THIS WILL ERASE ALL POSTS FROM THE GIVEN TUMBLR BLOG! | |
Use with caution and make sure you know what you're doing. That said this is | |
probably the easiest way to delete all of your posts if you have thousands of | |
posts. If you have less than that it might be easier for you to just use the | |
mass editor in the Tumblr UI. | |
This requires you to get an API key from Tumblr and request xAuth access to | |
be enabled. | |
''' | |
consumer_key = "YOUR_CONSUMER_KEY" | |
secret_key = "YOUR_SECRET_KEY" | |
username = "YOUR_EMAIL" | |
password = "YOUR_PASSWORD" | |
blog_name = "YOUR_BLOG.tumblr.com" | |
xauth_access_token_url = "https://www.tumblr.com/oauth/access_token" | |
request_token_url = "http://www.tumblr.com/oauth/request_token" | |
authorize_url = "http://www.tumblr.com/oauth/authorize" | |
access_token_url = "http://www.tumblr.com/oauth/access_token" | |
consumer = oauth.Consumer(consumer_key,secret_key) | |
client = oauth.Client(consumer) | |
client.add_credentials(username,password) | |
client.authorizations | |
params = {} | |
params["x_auth_username"] = username | |
params["x_auth_password"] = password | |
params["x_auth_mode"] = 'client_auth' | |
client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1() | |
resp, content = client.request(xauth_access_token_url, method="POST", body=urllib.urlencode(params)) | |
if resp['status'] != '200': | |
raise Exception("Invalid response %s. => %s" % (resp['status'], content)) | |
token_dict = dict(urlparse.parse_qsl(content)) | |
access_token = oauth.Token(token_dict["oauth_token"], token_dict["oauth_token_secret"]) | |
pyblr_client = pyblr.Pyblr(oauth.Client(consumer, access_token)) | |
counter = 0 | |
while True: | |
try: | |
posts = pyblr_client.posts(blog_name) | |
if len(posts) == 0: break | |
for p in posts["posts"]: | |
pyblr_client.delete_post(blog_name, params={ "id" : p["id"] }) | |
counter += 1 | |
except ValueError, err: | |
print "ERROR: %s" % (str(err)) | |
print "sleeping for 5 seconds before retrying" | |
time.sleep(5) | |
print "Finished clearing blog: %s" % (blog_name) | |
print "Deleted %d posts" % (counter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment