Skip to content

Instantly share code, notes, and snippets.

@mallipeddi
Created December 1, 2009 11:53
Show Gist options
  • Select an option

  • Save mallipeddi/246242 to your computer and use it in GitHub Desktop.

Select an option

Save mallipeddi/246242 to your computer and use it in GitHub Desktop.
Simple Tumblr.com blog backup utility
;; Tumblore - Tumblr blog backup tool
;;
;; Harish Mallipeddi
;; Dec 1 2009
(ns in.poundbang.tumblore
(:use [clojure.http.client :only [request]]
[clojure.contrib.duck-streams :only [spit]])
(:import (java.io ByteArrayInputStream)))
(def max-posts-per-request 50)
(defn- build-tumblr-url
[tumblr-id start howmany]
(format "http://%s.tumblr.com/api/read/?start=%d&num=%d" tumblr-id start howmany))
(defn- fetch-tumblr-url
[url]
(do (println (format "Fetching %s." url))
(apply str (:body-seq (request url)))))
(defn- get-total-posts
[resp]
(let [xml (xml-seq (clojure.xml/parse (ByteArrayInputStream. (.getBytes resp "utf-8"))))]
(Integer/parseInt (first (for [x xml :when (= (:tag x) :posts)] (:total (:attrs x)))))))
(defn backup
[tumblr-id output-dir]
(let [tumblr-url (partial build-tumblr-url tumblr-id)
total-posts (get-total-posts (fetch-tumblr-url (tumblr-url 0 1)))]
(do
(println (format "Found %d total posts. Downloading..." total-posts))
(doseq [start (range 0 total-posts max-posts-per-request)]
(spit (format "%s/%d.xml" output-dir start) (fetch-tumblr-url (tumblr-url start max-posts-per-request))))
(println "Done."))))
(defn run
[]
(if (= (count *command-line-args*) 2)
(apply backup *command-line-args*)
(do (println "Usage: tumblore <tumblr-id> <output-dir>"))))
(run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment