-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump2jekyll.rb
54 lines (42 loc) · 1.34 KB
/
dump2jekyll.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
#!/usr/bin/env ruby -w
# encoding: UTF-8
require 'open-uri'
require 'nokogiri'
require 'htmlentities'
require 'date'
def get_post (uri, site)
response = Net::HTTP.get_response(uri)
items = JSON.parse(response.body)['items']
items.each do | post |
owner = post['owner']
author = '<a alt="' + owner['display_name']+ '" href="' + owner['link'] + '">' + owner['display_name'] + '</a>'
post_link = post['share_link'] + '/' + owner['user_id'].to_s
body = HTMLEntities.new.decode post['body_markdown']
created = DateTime.strptime(post['creation_date'].to_s, '%s').strftime('%F %T')
return <<MD
---
layout: post
title: #{ post['title'] }
tags: meta-post
license: http://creativecommons.org/licenses/by-sa/3.0/
encoding: utf-8
author: #{ author }
date: #{ created }
comments: no
---
([Originally published](#{ post_link }) on #{ site } Stack Exchange by #{ author }.)
---
#{ body }
---
Please direct comments to the [original post](#{ post_link }).
MD
end
end
abort('Usage: ' + $0 + ' url post_id') unless ARGV.length >= 2
site = ARGV.shift
ARGV.each do | id |
# http://api.stackexchange.com/docs/posts-by-ids#filter=!*7PYFiVwh*N4PkCdfxnM3de0s50u
uri = URI('http://api.stackexchange.com/2.2/posts/' + id)
uri.query = URI.encode_www_form({ :site => site, :filter => '!*7PYFiVwh*N4PkCdfxnM3de0s50u' })
puts(get_post(uri, site))
end