Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 92 lines (77 sloc) 2.079 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 67 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
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
Something went wrong with that request. Please try again.