Created
March 15, 2013 17:25
-
-
Save skopp/5171563 to your computer and use it in GitHub Desktop.
Posterous2Jekyll ruby script copied from https://github.com/pepijndevos/jekyll/blob/41890aa6528eded49a1fc8e1e2f092f77b8639ab/lib/jekyll/migrators/posterous.rb
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 'posterous' | |
require 'net/http' | |
require 'URI' | |
Posterous.config = { | |
'username' => ARGV[0], | |
'password' => ARGV[1], | |
'api_token' => ARGV[2] | |
} | |
include Posterous | |
puts "all set" | |
FileUtils.mkdir_p "_posts" | |
FileUtils.mkdir_p "images" | |
site = Site.primary | |
page = 1 | |
posts = site.posts(:page => page) | |
puts "made it so far" | |
def download_image(u) | |
path = 'images/%s' % u.split('/')[-1] | |
url = URI.parse(u) | |
found = false | |
until found | |
host, port = url.host, url.port if url.host && url.port | |
query = url.query ? url.query : "" | |
req = Net::HTTP::Get.new(url.path + '?' + query) | |
res = Net::HTTP.start(host, port) {|http| http.request(req) } | |
res.header['location'] ? url = URI.parse(res.header['location']) : found = true | |
end | |
open(path, "wb") do |file| | |
file.write(res.body) | |
end | |
path | |
end | |
while posts.any? | |
posts.each do |post| | |
puts post.title | |
slug = post.slug.gsub('/', '-') | |
date = Date.parse(post.display_date) | |
published = !post.is_private | |
name = "%02d-%02d-%02d-%s.html" % [date.year, date.month, date.day, slug] | |
tags = post.tags.map do |t| | |
t['name'] | |
end | |
perm = URI::parse(post.full_url) | |
# Get the relevant fields as a hash, delete empty fields and convert | |
# to YAML for the header | |
data = { | |
'permalink' => perm.path + "/index.html", | |
'layout' => 'post', | |
'title' => post.title.to_s, | |
'published' => published, | |
'categories' => tags, | |
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml | |
content = post.body_html | |
# awefull hack, do not use on vlog or podcast | |
post.media[2]['images'].each do |img| | |
path = download_image(img['full']['url']) | |
tag = "<img src=\"/%s\" alt=\"%s\" />" % [path, img['full']['caption']] | |
puts tag | |
begin | |
content[/\[\[posterous-content:[^\]]*\]\]/] = tag | |
rescue IndexError | |
puts "weird stuff happening" | |
end | |
end | |
# 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 = site.posts(:page => page) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment