Created
March 21, 2012 01:17
-
-
Save justinkelly/2143368 to your computer and use it in GitHub Desktop.
Update posterous migrator for jekyll/octopress that maintains url slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To maintain your posterous links use the below permalink config | |
# ----------------------- # | |
# Jekyll & Plugins # | |
# ----------------------- # | |
permalink: /:title/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'jekyll' | |
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)' | |
module Jekyll | |
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 | |
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) | |
when Net::HTTPForbidden then | |
retry_after = response.to_hash['retry-after'][0] | |
puts "We have been told to try again after #{retry_after} seconds" | |
sleep(retry_after.to_i + 1) | |
fetch(uri_str, limit - 1) | |
else response.error! | |
end | |
end | |
def self.process(email, pass, api_token, blog = 'primary', tags_key = 'categories') | |
@email, @pass, @api_token = email, pass, api_token | |
FileUtils.mkdir_p "_posts" | |
posts = JSON.parse(self.fetch("/api/2/sites/#{blog}/posts?api_token=#{@api_token}").body) | |
page = 1 | |
while posts.any? | |
posts.each do |post| | |
title = post["title"] | |
# slug = title.gsub(/[^[:alnum:]]+/, '-').gsub(/^-+|-+$/, '').downcase | |
posterous_slug = post["slug"] | |
slug = posterous_slug[0..44] | |
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] | |
#name = "%s.html" % [slug] | |
tags = [] | |
post["tags"].each do |tag| | |
tags.push(tag["name"]) | |
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, | |
'date' => date, | |
tags_key => tags, | |
'posterous_url' => post["full_url"], | |
'posterous_slug' => posterous_slug | |
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml | |
# Write out the data and content to file | |
File.open("_posts/#{name}", "w") do |f| | |
puts name | |
f.puts data | |
f.puts "---" | |
f.puts content | |
end | |
end | |
page += 1 | |
posts = JSON.parse(self.fetch("/api/2/sites/#{blog}/posts?api_token=#{@api_token}&page=#{page}").body) | |
end | |
end | |
end | |
end |
Hi, thanks for this, how do you run this file in an octopress install ?
refer: https://github.com/mojombo/jekyll/wiki/blog-migrations for how to run this - just replace the default jekyll.octpress posterous file with this one
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this migrator is based on https://github.com/mojombo/jekyll/pull/477/files