lib(RCurl)
lib(rjson)

N <- 100


urls <- sprintf("http://today-was-great.tumblr.com/page/%i/json", seq(N)+1)

raw.ll <- vector("list", length=N)
for (i in seq(N))
  raw.ll[[i]] <- fromJSON(gsub("^var tumblr_api_read \\= ", "", getURL(urls[[i]])))

stopifnot(areEqual(lapply(raw.ll, names)))
stopifnot(names(raw.ll[[1]]) == c("tumblelog", "posts"))

tumblelogs <- lapply(raw.ll, "[[", "tumblelog")
areEqual(tumblelogs)

posts <- lapply(raw.ll, "[[", "posts")
posts <- posts[sapply(posts, length) != 0]

posts.u <- unlist(posts, recursive=FALSE)
quotes <- sapply(posts.u, "[[", 'quote-text')

quotes <- gsub("\\&\\#8217\\;", "'", quotes)
quotes <- gsub("\\&\\#8230\\;", "...", quotes)
quotes <- gsub("\\&\\#822[01]\\;", '"', quotes)
quotes <- gsub("<br/>\\\n", "\n", quotes)

print(quotes[grepl("\\&\\#", quotes)])

description <- sprintf("{description: \"%s\"}", quotes)
cat("\n--------\n\n\n", description, "\n", sep="\n")
