Permalink
Browse files

Download and include images in posterous migrator.

  • Loading branch information...
1 parent 4499df8 commit 5890740c4bb6e8d9da498a88a342daaf8356ce01 @markhepburn committed Feb 13, 2012
Showing with 46 additions and 2 deletions.
  1. +46 −2 lib/jekyll/migrators/posterous.rb
@@ -27,7 +27,35 @@ def self.fetch(uri_str, limit = 10)
end
end
- def self.process(email, pass, api_token, blog = 'primary')
+ def self.fetch_images(directory, imgs)
+ def self.fetch_one(url, limit = 10)
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
+ response = Net::HTTP.get_response(URI.parse(url))
+ case response
+ when Net::HTTPSuccess then response.body
+ when Net::HTTPRedirection then self.fetch_one(response['location'], limit - 1)
+ else
+ response.error!
+ end
+ end
+
+ FileUtils.mkdir_p directory
+ urls = Array.new
+ imgs.each do |img|
+ fullurl = img["full"]["url"]
+ uri = URI.parse(fullurl)
+ imgname = uri.path.split("/")[-1]
+ imgdata = self.fetch_one(fullurl)
+ open(directory + "/" + imgname, "wb") do |file|
+ file.write imgdata
+ end
+ urls.push(directory + "/" + imgname)
+ end
+
+ return urls
+ end
+
+ def self.process(email, pass, api_token, blog = 'primary', base_path = '/')
@email, @pass, @api_token = email, pass, api_token
FileUtils.mkdir_p "_posts"
@@ -41,7 +69,23 @@ def self.process(email, pass, api_token, blog = 'primary')
date = Date.parse(post["display_date"])
content = post["body_html"]
published = !post["is_private"]
- name = "%02d-%02d-%02d-%s.html" % [date.year, date.month, date.day, slug]
+ basename = "%02d-%02d-%02d-%s" % [date.year, date.month, date.day, slug]
+ name = basename + '.html'
+
+ # Images:
+ post_imgs = post["media"]["images"]
+ if post_imgs.any?
+ img_dir = "imgs/%s" % basename
+ img_urls = self.fetch_images(img_dir, post_imgs)
+
+ img_urls.map! do |url|
+ '<li><img src="' + base_path + url + '"></li>'
+ end
+ imgcontent = "<ol>\n" + img_urls.join("\n") + "</ol>\n"
+
+ # filter out "posterous-content", replacing with imgs:
+ content = content.sub(/\<p\>\[\[posterous-content:[^\]]+\]\]\<\/\p\>/, imgcontent)
+ end
# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header

0 comments on commit 5890740

Please sign in to comment.