forked from horaextra/horaextra.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posterous.rb
94 lines (79 loc) · 2.12 KB
/
posterous.rb
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
93
94
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]
}
puts "#{ARGV[0]}, #{ARGV[1]}, #{ARGV[2]}"
include Posterous
puts "all set"
FileUtils.mkdir_p "_posts"
site = Site.primary
page = 1
posts = site.posts(:page => page)
puts "made it so far"
def download_image(u)
path = 'static/img/blog-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.title.gsub(/[^[:alnum:]]+/, '-').downcase
author = post.author_display_name
date = Date.parse(post.display_date)
published = !post.is_private
post_name = "%02d-%02d-%02d-%s" % [date.year, date.month, date.day, slug]
name = "#{post_name}.html"
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 = {
'layout' => 'post',
'title' => post.title.to_s,
'published' => published,
'categories' => tags,
'author' => author
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml
content = post.body_html
# awefull hack, do not use on vlog or podcast
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