Skip to content

Instantly share code, notes, and snippets.

@codebutler
Created September 23, 2012 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebutler/3772538 to your computer and use it in GitHub Desktop.
Save codebutler/3772538 to your computer and use it in GitHub Desktop.
Import Markdown posts from Posterous into Jeykll
source :rubygems
gem 'rest-client'
gem 'nokogiri'
require 'rest-client'
require 'json'
require 'date'
require 'nokogiri'
require 'yaml'
require 'uri'
require 'fileutils'
# Copy the value from your browser's session cookie here
SESSION_ID = ''
options = {
cookies: { '_sharebymail_session_id' => SESSION_ID }
}
sites = JSON.parse(RestClient.get('http://posterous.com/api/2/sites', options))
site_id = sites[0]['id']
response = RestClient.get("https://posterous.com/api/2/sites/#{site_id}/posts", options)
File.open('posterous-posts.json', 'w') {|f| f.write(response) }
posts = JSON.parse(response)
posts.each do |post|
post_id = post['id']
post_slug = post['slug']
post_title = post['title']
post_date = Date.parse(post['display_date'])
body_text = post['body_html']
extension = 'html'
puts "Importing #{post_id}: #{post_title}"
# Check to see if this is markdown
response = RestClient.get "http://posterous.com/posts/edit/#{post_id}", options
doc = Nokogiri::HTML(response)
original_body_text = doc.css('#editor_main #post_body').text
original_body = Nokogiri::HTML(original_body_text)
if original_body.css('markdown').any?
extension = 'markdown'
body_text = original_body.css('markdown').text
end
header = {
'layout' => 'post',
'title' => post_title,
'date' => post_date.strftime('%b %d, %Y'),
'redirects' => [
URI.parse(post['full_url']).path
]
}
lines = []
lines << header.to_yaml.strip
lines << '---'
lines << ''
lines << body_text
unless post['is_private']
filename = "_posts/#{post_date.strftime('%Y-%m-%d')}-#{post_slug}.#{extension}"
else
filename = "_posts_private/#{post_date.strftime('%Y-%m-%d')}-#{post_slug}.#{extension}"
end
FileUtils.mkdir_p(File.dirname(filename))
File.open(filename, 'w') { |f| f.write(lines.join("\n")) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment