lib/jekyll/jekyll-import/posterous.rb in jekyll-import-0.1.0.beta1 vs lib/jekyll/jekyll-import/posterous.rb in jekyll-import-0.1.0.beta2
- old
+ new
@@ -3,12 +3,19 @@
require 'fileutils'
require 'net/http'
require 'uri'
require "json"
-# ruby -r './lib/jekyll/migrators/posterous.rb' -e 'Jekyll::Posterous.process(email, pass, api_key, blog)'
+# ruby -r './lib/jekyll/migrators/posterous.rb' -e 'Jekyll::Posterous.process(email, pass, api_key)'
+# Other arguments are optional; the default values are:
+# * :include_imgs => false # should images be downloaded as well?
+# * :blog => 'primary' # blog, if you have more than one.
+# * :base_path => '/' # for image, if they will be served from a different host for eg.
+# For example, to download images as well as your posts, use the above command with
+# ....process(email, pass, api_key, :include_imgs => true)
+
module JekyllImport
module Posterous
def self.fetch(uri_str, limit = 10)
# You should choose better exception.
raise ArgumentError, 'Stuck in a redirect loop. Please double check your email and password' if limit == 0
@@ -53,15 +60,17 @@
end
return urls
end
- def self.process(email, pass, api_token, blog = 'primary', base_path = '/')
+ def self.process(email, pass, api_token, opts={})
@email, @pass, @api_token = email, pass, api_token
+ defaults = { :include_imgs => false, :blog => 'primary', :base_path => '/' }
+ opts = defaults.merge(opts)
FileUtils.mkdir_p "_posts"
- posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}").body)
+ posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{opts[:blog]}/posts?api_token=#{@api_token}").body)
page = 1
while posts.any?
posts.each do |post|
title = post["title"]
@@ -71,22 +80,24 @@
published = !post["is_private"]
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)
+ if opts[:include_imgs]
+ 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"
+ img_urls.map! do |url|
+ '<li><img src="' + opts[: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)
+ # filter out "posterous-content", replacing with imgs:
+ content = content.sub(/\<p\>\[\[posterous-content:[^\]]+\]\]\<\/p\>/, imgcontent)
+ end
end
# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header
data = {
@@ -102,10 +113,10 @@
f.puts content
end
end
page += 1
- posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}&page=#{page}").body)
+ posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{opts[:blog]}/posts?api_token=#{@api_token}&page=#{page}").body)
end
end
end
end