Skip to content

Instantly share code, notes, and snippets.

@roman
Created February 20, 2012 00:35
Show Gist options
  • Save roman/1866788 to your computer and use it in GitHub Desktop.
Save roman/1866788 to your computer and use it in GitHub Desktop.
Tumblr API using river
(ns river.tumblr
(:use [clojure.pprint :only (pprint)])
(:use [river.core]
[zetta.core]
[river.http :only (produce-http-get)]
[zetta.river :only (parse*)]
[zetta.json :only (json)])
(:require [river.seq :as rs]
[zetta.parser.seq :as zs]))
(defrecord TumblrInfo [title
posts
name
url
updated
description
ask
likes])
(defrecord TumblrPost
[blog_name id post_url type timestamp date
format reblog_key tags bookmarklet mobile source_url
source_title total_posts
; text posts
title body
; photo posts
photos caption width height
; quote posts
text source
; link posts
url description ; title
; chat posts
dialogue ; title body
; audio posts
player plays album_art artist album
track_name track_number year ; caption
; video posts
; caption player
; answer posts
asking_name asking_url question answer])
(defn- build-tumblr-url [blog-hostname blog-method]
(str "http://api.tumblr.com/v2/blog/"
blog-hostname "/" blog-method))
(defn produce-info [blog-hostname query-params]
"Produces a river stream of TumblrInfo records."
(let [url (build-tumblr-url blog-hostname
"info")]
(p* (produce-http-get url {:query-params query-params})
(parse* json)
(rs/map* #(-> %
(get-in [:response :blog])
map->TumblrInfo)))))
(defn produce-posts
"Produces a river stream of TumblrPost records."
([blog-hostname]
(produce-posts blog-hostname nil {}))
([blog-hostname query-params]
(produce-posts blog-hostname nil query-params))
([blog-hostname type query-params]
(let [url (build-tumblr-url blog-hostname
"posts")
complete-url (cond
(nil? type) url
:else (str url "/" type))]
(println complete-url)
(p* (produce-http-get url {:query-params query-params})
(parse* json)
(rs/mapcat* #(get-in % [:response :posts]))
(rs/map* map->TumblrPost)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment