Class: JekyllImport::Importers::Posterous
- Inherits:
-
JekyllImport::Importer
- Object
- JekyllImport::Importer
- JekyllImport::Importers::Posterous
- Defined in:
- lib/jekyll-import/importers/posterous.rb
Class Method Summary collapse
- .fetch(uri_str, limit = 10) ⇒ Object
- .fetch_images(directory, imgs) ⇒ Object
- .fetch_one(url, limit = 10) ⇒ Object
- .process(options) ⇒ Object
- .require_deps ⇒ Object
- .specify_options(c) ⇒ Object
Methods inherited from JekyllImport::Importer
inherited, run, stringify_keys, subclasses
Class Method Details
.fetch(uri_str, limit = 10) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jekyll-import/importers/posterous.rb', line 22 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 response = nil Net::HTTP.start('posterous.com') do |http| req = Net::HTTP::Get.new(uri_str) req.basic_auth @email, @pass response = http.request(req) end case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch(response['location'], limit - 1) else response.error! end end |
.fetch_images(directory, imgs) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jekyll-import/importers/posterous.rb', line 40 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 |
.fetch_one(url, limit = 10) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jekyll-import/importers/posterous.rb', line 41 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 |
.process(options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jekyll-import/importers/posterous.rb', line 68 def self.process() email = .fetch('email') pass = .fetch('password') api_token = .fetch('api_token') @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/#{opts[:blog]}/posts?api_token=#{@api_token}").body) page = 1 while posts.any? posts.each do |post| title = post["title"] slug = title.gsub(/[^[:alnum:]]+/, '-').downcase date = Date.parse(post["display_date"]) content = post["body_html"] published = !post["is_private"] basename = "%02d-%02d-%02d-%s" % [date.year, date.month, date.day, slug] name = basename + '.html' # Images: 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="' + 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) end end # Get the relevant fields as a hash, delete empty fields and convert # to YAML for the header data = { 'layout' => 'post', 'title' => title.to_s, 'published' => published }.delete_if { |k,v| v.nil? || v == ''}.to_yaml # Write out the data and content to file File.open("_posts/#{name}", "w") do |f| f.puts data f.puts "---" f.puts content end end page += 1 posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{opts[:blog]}/posts?api_token=#{@api_token}&page=#{page}").body) end end |
.require_deps ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jekyll-import/importers/posterous.rb', line 11 def self.require_deps JekyllImport.require_with_fallback(%w[ rubygems jekyll fileutils uri json net/http ]) end |
.specify_options(c) ⇒ Object
5 6 7 8 9 |
# File 'lib/jekyll-import/importers/posterous.rb', line 5 def self.(c) c.option 'email', '--email EMAIL', 'Posterous email address' c.option 'password', '--password PW', 'Posterous password' c.option 'api_token', '--token TOKEN', 'Posterous API Token' end |