Skip to content

Instantly share code, notes, and snippets.

@reid
Created February 1, 2010 07:30
Show Gist options
  • Save reid/291514 to your computer and use it in GitHub Desktop.
Save reid/291514 to your computer and use it in GitHub Desktop.
Deletes all feed posts from a tumblelog
// Deletes all feed posts from a tumblelog
process.mixin(GLOBAL, require("sys"));
var http = require("http"),
query = require("querystring");
var hostname, email, password;
function fail (message) {
puts(message);
process.exit(1);
}
function request (method, path, headers) {
if (!headers) headers = [];
headers["Host"] = hostname;
if (method === "POST") headers["Host"] = "www.tumblr.com";
return http.createClient(
80, "tumblr.com"
).request(method, path, headers);
}
function requestForDelete (count) {
if (!count) count = 0;
request(
"GET",
"/api/read/json?" + query.stringify({
"callback" : "deleteAndRequestNext"
})
).finish(function (res) {
if (res.statusCode !== 200) {
puts("Tumblr returned " + res.statusCode + " Retrying in 1 second...");
if (count > 5) return fail("Too many tries, giving up.");
return setTimeout(function () {
requestForDelete(++count);
}, 1000);
}
var body = "";
res.addListener("body", function (chunk) {
body += chunk;
}).addListener("complete", function () {
count = 0;
eval(body);
});
});
}
function deleteAndRequestNext (tumblr) {
var bounce = 0,
wait = 0,
count = 0;
tumblr.posts.forEach(function (post) {
if (!post["from-feed-id"]) return bounce++;
setTimeout(function () {
var body = query.stringify({
"email" : email,
"password" : password,
"post-id" : post.id
});
var req = request(
"POST",
"/api/delete",
{
"Content-Type" : "application/x-www-form-urlencoded",
"Content-Length" : body.length
}
);
req.sendBody(body);
req.finish(function (res) {
if (
res.statusCode !== 200
) return puts("Unable to delete " + post.id + " because Tumblr returned " + res.statusCode);
res.addListener("complete", function () {
puts("Deleted " + post.id);
});
});
if (++count === tumblr.posts.length) requestForDelete();
}, wait);
wait += 5900;
});
if (bounce === tumblr.posts.length) fail("Couldn't find any feed posts to delete.");
}
process.stdio.addListener("data", function (data) {
data = data.replace(/\n/, "");
if (!data) return puts("This is required to proceed. Ctrl-C to abort.");
if (!hostname) {
hostname = data;
return puts("Tumblr email address?");
}
if (!email) {
email = data;
return puts("Tumblr password?");
}
if (!password) password = data;
if (email && password && hostname) {
process.stdio.close();
puts("Untumbling...");
requestForDelete();
}
});
puts("This script will delete all feed posts from your Tumblr.");
puts("Tumblr hostname?");
process.stdio.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment