Last active
December 14, 2015 01:08
-
-
Save aterreno/5003450 to your computer and use it in GitHub Desktop.
Migration script from http://posterous.com/ to github pages, forked from https://github.com/AlexeyMK/alexeymk.github.com/blob/master/posterous_import.rb with a monkey patch on Posterous Connection.
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' | |
puts ARGV, "IS ARGV" | |
module Posterous | |
module Connection | |
def default_options | |
@default_options ||= { | |
#:username => Posterous.config['username'], | |
#:password => Posterous.config['password'] | |
:userpwd => Posterous.config['username']+":"+Posterous.config['password'] | |
} | |
end | |
end | |
end | |
Posterous.config = { | |
'username' => ARGV[0], | |
'password' => ARGV[1], | |
'api_token' => ARGV[2] # https://posterous.com/api | |
} | |
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| | |
sleep 1 # second | |
puts post.title | |
slug = post.slug.gsub('/', '-') | |
date = DateTime.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, | |
'date' => 'QUOTEHACK%02d-%02d-%02d %02d:%02d:%02d' % [date.year, date.month, date.day, date.hour, date.minute, date.second], | |
'tags' => tags, | |
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml | |
# force YAML to not wrap strings around the date. | |
data.gsub!("QUOTEHACK", "") | |
content = post.body_html | |
# awefull hack, do not use on vlog or podcast | |
# may not have any images | |
post.media['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