-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-posts.rb
97 lines (79 loc) · 2.34 KB
/
create-posts.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
95
96
97
require 'json'
require 'pp'
require 'time'
require 'mail'
require 'cgi'
def main
previous_topic_id = get_json('topics.json')["ygData"]["lastTopic"]
while previous_topic_id != 0 do
filename = "topic-#{previous_topic_id}.json"
json = get_json("topic-#{previous_topic_id}.json")
create_post(json)
previous_topic_id = json["ygData"]["prevTopicId"]
end
end
BAD_TOPICS = [5580, 1515]
def create_post(json)
messages = json["ygData"]["messages"].reverse.map { |m| get_post_data(m) }
initial_message = messages.first
return if BAD_TOPICS.include? initial_message["id"]
last_message = messages.last
responses = messages - [initial_message]
body = messages.map do |message|
<<-EOM
<article>
<h4>#{message["author"]}</h4>
<p><small><time>#{format_time(message["time"])}</time></small></p>
#{message["body"]}
</article>
EOM
end.join('<hr style="margin: 3em 0"/>')
post_filename = "#{initial_message["time"].strftime("%F")}-#{initial_message["id"]}.html"
post_content = <<-EOP
---
layout: post
title: >-
#{initial_message["title"]}
date: #{last_message["time"].to_s}
author: >-
#{initial_message["author"]}
slug: "#{initial_message["id"]}"
description: ""
excerpt_separator: <!--there-is-no-excerpt-separator-expected-ever-->
---
{% raw %}
#{body}
{% endraw %}
EOP
responders = responses.map do |r|
r["author"]
end.uniq - [initial_message["author"]]
puts "Creating #{post_filename} #{post_content.size} [#{initial_message["title"]}] by #{initial_message["author"]}, #{responders.join(", ")}"
IO.write("_posts/#{post_filename}", post_content)
end
def format_time(time)
time.strftime("%e %b %Y, at %l:%M%P")
end
def get_post_data(message)
{
"id" => message["topicId"],
"title" => CGI.unescapeHTML(message["subject"] || ""),
"time" => Time.at(message["postDate"].to_i - 7 * 3600),
"author" => get_author(message),
"body" => message["messageBody"]
}
end
def get_author(message)
if message["authorName"].size > 0 then
message["authorName"]
elsif message["from"].size > 0 then
Mail::Encodings.value_decode(message["from"])
elsif message["profile"].size > 0 then
message["profile"]
end
end
def get_json(filename)
path = "cache/#{filename}"
JSON.parse(IO.read(path))
end
main