Skip to content

Instantly share code, notes, and snippets.

@zpao
Created May 27, 2012 00:55
Show Gist options
  • Save zpao/2795768 to your computer and use it in GitHub Desktop.
Save zpao/2795768 to your computer and use it in GitHub Desktop.
Script I wrote to import my Tumblr to Jekyll
#!/usr/bin/env coffee
request = require "request"
fs = require "fs"
TARGET_DIR = "./_posts"
PHOTO_SIZE = 1280
url = "http://blog.zpao.com/api/read?format=json&filter=none&num=50"
getPosts = (page) ->
start = page * 50
request.get "#{url}&start=#{start}", (err, res, response) ->
# because I want to parse it, not eval it
response = response.replace('var tumblr_api_read = ', '').replace('};','}').replace(/\n/g,'')
tumblr = JSON.parse response
for post in tumblr.posts
# I'm going to downcase all tags because I was inconsistent on tumblr
tags = post.tags.map (t) -> t.toLowerCase()
dateString = formatDate(post.date)
tumblr_permalink = "post/#{post.id}/#{post.slug}"
filename = "#{TARGET_DIR}/#{dateString}-#{post.slug}.markdown"
postExtra = ""
switch post.type
when "regular"
title = post["regular-title"]
content = post["regular-body"]
when "link"
title = post["link-text"]
content = post["link-description"]
#XXX a bit ghetto to make this format nicely
postExtra = "type: link\nurl: \"#{ post["link-url"] }\"\n"
when "photo"
#TODO grab the image files, write to disk
title = ""
content = "![](#{ post["photo-url-#{ PHOTO_SIZE }"] })\n\n#{ post["photo-caption"] }"
postExtra = "type: photo\n"
console.log post
when "video"
title = ""
content = "#{ post["video-player"] }\n\n#{ post["video-caption"] }"
postExtra = "type: video\n"
when "quote"
title = ""
content = ">#{ post["quote-text"] }\n\n—#{ post["quote-source"] }"
postExtra = "type: quote\n"
else
console.log "UNKNOWN POST TYPE"
console.log post
# coffeescript's heredocs don't eval #{var} like normal strings :(
#TODO make YAML.stringify instead of this frankenstein
tagsStr = " - " + tags.join("\n - ")
file_content =
"---\n" +
"title: \"#{title}\"\n" +
"tags:\n#{tagsStr}\n" +
"layout: post\n" +
"#{postExtra}" +
"tumblr_permalink: #{tumblr_permalink}\n" +
"---\n\n#{content}\n"
fs.writeFileSync(filename, file_content)
# recurse! recurse! until we have all the posts
if start + tumblr.posts.length < tumblr["posts-total"]
getPosts(page + 1)
fs.rmdir TARGET_DIR, (err) ->
fs.mkdir TARGET_DIR, "0777", (err) ->
getPosts(0)
formatDate = (date) ->
date = new Date(date)
year = date.getYear() + 1900
month = "0#{date.getMonth() + 1}".slice(-2)
day = "0#{date.getDate()}".slice(-2)
return "#{year}-#{month}-#{day}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment