commit
72c8ceba4e
|
@ -1,11 +1,14 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'jekyll'
|
require 'jekyll'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'net/http'
|
require 'net/https'
|
||||||
|
require 'open-uri'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
# ruby -r './lib/jekyll/migrators/posterous.rb' -e 'Jekyll::Posterous.process(email, pass, api_key, blog)'
|
# ruby -r './lib/jekyll/migrators/posterous.rb' -e 'Jekyll::Posterous.process(email, pass, api_token, blog, tags_key)'
|
||||||
|
# You can find your api token in posterous api page - http://posterous.com/api . Click on any of the 'view token' links to see your token.
|
||||||
|
# blog is optional, by default it is the primary one
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module Posterous
|
module Posterous
|
||||||
|
@ -14,6 +17,9 @@ module Jekyll
|
||||||
raise ArgumentError, 'Stuck in a redirect loop. Please double check your email and password' if limit == 0
|
raise ArgumentError, 'Stuck in a redirect loop. Please double check your email and password' if limit == 0
|
||||||
|
|
||||||
response = nil
|
response = nil
|
||||||
|
|
||||||
|
puts uri_str
|
||||||
|
puts '-------'
|
||||||
Net::HTTP.start('posterous.com') do |http|
|
Net::HTTP.start('posterous.com') do |http|
|
||||||
req = Net::HTTP::Get.new(uri_str)
|
req = Net::HTTP::Get.new(uri_str)
|
||||||
req.basic_auth @email, @pass
|
req.basic_auth @email, @pass
|
||||||
|
@ -23,36 +29,50 @@ module Jekyll
|
||||||
case response
|
case response
|
||||||
when Net::HTTPSuccess then response
|
when Net::HTTPSuccess then response
|
||||||
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
|
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!
|
else response.error!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.process(email, pass, api_token, blog = 'primary')
|
def self.process(email, pass, api_token, blog = 'primary', tags_key = 'categories')
|
||||||
@email, @pass, @api_token = email, pass, api_token
|
@email, @pass, @api_token = email, pass, api_token
|
||||||
FileUtils.mkdir_p "_posts"
|
FileUtils.mkdir_p "_posts"
|
||||||
|
|
||||||
posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}").body)
|
posts = JSON.parse(self.fetch("/api/2/sites/#{blog}/posts?api_token=#{@api_token}").body)
|
||||||
page = 1
|
page = 1
|
||||||
|
|
||||||
while posts.any?
|
while posts.any?
|
||||||
posts.each do |post|
|
posts.each do |post|
|
||||||
title = post["title"]
|
title = post["title"]
|
||||||
slug = title.gsub(/[^[:alnum:]]+/, '-').downcase
|
slug = title.gsub(/[^[:alnum:]]+/, '-').gsub(/^-+|-+$/, '').downcase
|
||||||
|
posterous_slug = post["slug"]
|
||||||
date = Date.parse(post["display_date"])
|
date = Date.parse(post["display_date"])
|
||||||
content = post["body_html"]
|
content = post["body_html"]
|
||||||
published = !post["is_private"]
|
published = !post["is_private"]
|
||||||
name = "%02d-%02d-%02d-%s.html" % [date.year, date.month, date.day, slug]
|
name = "%02d-%02d-%02d-%s.html" % [date.year, date.month, date.day, slug]
|
||||||
|
tags = []
|
||||||
|
post["tags"].each do |tag|
|
||||||
|
tags.push(tag["name"])
|
||||||
|
end
|
||||||
|
|
||||||
# Get the relevant fields as a hash, delete empty fields and convert
|
# Get the relevant fields as a hash, delete empty fields and convert
|
||||||
# to YAML for the header
|
# to YAML for the header
|
||||||
data = {
|
data = {
|
||||||
'layout' => 'post',
|
'layout' => 'post',
|
||||||
'title' => title.to_s,
|
'title' => title.to_s,
|
||||||
'published' => published
|
'published' => published,
|
||||||
|
tags_key => tags,
|
||||||
|
'posterous_url' => post["full_url"],
|
||||||
|
'posterous_slug' => posterous_slug
|
||||||
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml
|
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml
|
||||||
|
|
||||||
# Write out the data and content to file
|
# Write out the data and content to file
|
||||||
File.open("_posts/#{name}", "w") do |f|
|
File.open("_posts/#{name}", "w") do |f|
|
||||||
|
puts name
|
||||||
f.puts data
|
f.puts data
|
||||||
f.puts "---"
|
f.puts "---"
|
||||||
f.puts content
|
f.puts content
|
||||||
|
@ -60,7 +80,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
page += 1
|
page += 1
|
||||||
posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}&page=#{page}").body)
|
posts = JSON.parse(self.fetch("/api/2/sites/#{blog}/posts?api_token=#{@api_token}&page=#{page}").body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue