-
Notifications
You must be signed in to change notification settings - Fork 0
/
esa2hatena.rb
153 lines (128 loc) · 3.66 KB
/
esa2hatena.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true
require 'atomutil'
require 'date'
require 'esa'
require 'json'
require 'pp'
# bundle exec ruby esa2hatena.rb [2019-10-19]
date_str = ARGV[0] || nil
date = Date.parse(date_str) if date_str
module Esa
# 自分の日報から "所感" を抜き出す
class RemarksExporter
attr_reader :client, :date
def self.call(*args)
new(*args).send(:call)
end
private
# @param esa_client [Esa::Client]
# @param date [Date]
def initialize(esa_client:, date: nil)
@client = esa_client
@date = date
end
# 実処理
#
# @return [Array<Hash>]
def call
posts = search_posts
result = []
posts.each do |post|
full_name = post['full_name']
body_md = post['body_md']
# タイトルを見出し用に整形
headline = full_name.match(/日報\/(.*) :sp:/)[1].gsub(/\)\//, ') ')
# 作業日報の手前までが所感
tmp = body_md.match(/(.*)## 本日の作業内容/m)[1]
remarks = tmp.gsub(/Copied from: \[.*\]\(.*\)\r\n/, '').strip
result << {
headline: headline,
remarks: remarks
}
end
result
end
# 自分の今週の日報(月〜金, 最大 5 記事)を取得
# 使うデータ(full_name, body_md) だけ返す
#
# @return [Array<Hash>] posts
def search_posts
response = client.posts(
q: "@tmd45 in:\"日報\" created:>=#{last_monday}",
sort: 'created', order: 'asc'
)
posts = response.body['posts']
posts.map do |post|
post.select { |k, _| ['full_name', 'body_md'].include?(k) }
end
end
# @return [String] Exp. "2019-10-11"
def last_monday
today = date || Date.today
last_monday = today - (today.wday - 1)
last_monday.to_s
end
end
end
module HatenaBlog
# NOTE: MonkeyPatch for atomutil
class TextContent < Atom::Content
def body=(value)
@elem.add_text value
self.type = 'text'
end
end
# はてなブログに下書き記事を作成する
class DraftMaker
attr_reader :client, :contents
def self.call(*args)
new(*args).send(:call)
end
private
# @param atompub_client [Atompub::Client]
# @param contents [Array<Hash>]
def initialize(atompub_client:, contents:)
@client = atompub_client
@contents = contents
end
# 実処理
def call
username = ENV['HATENA_USERNAME']
blog_domain = ENV['HATENA_BLOG_DOMAIN']
post_url = "https://blog.hatena.ne.jp/#{username}/#{blog_domain}/atom/entry"
# `[]` の中身がカテゴリになるはてなブログの記法
title = "[自動投稿][日記]所感週報 #{Date.today.to_s}"
entry = Atom::Entry.new(
title: title,
content: body
)
app = Atom::Control.new(draft: 'yes')
entry.add_control(app)
pp client.create_entry(post_url, entry)
end
# はてなブログ投稿用に Markdown の本文を組み立てる
#
# @return [String]
def body
body = ['今週の所感です。']
contents.each do |content|
body << "### #{content[:headline]}"
body << content[:remarks]
end
body.join("\r\n\r\n")
end
end
end
# esa
esa_client = Esa::Client.new(
access_token: ENV['ESA_ACCESS_TOKEN'],
current_team: ENV['ESA_CURRENT_TEAM']
)
contents = Esa::RemarksExporter.call(esa_client: esa_client, date: date)
# Hatena Blog
auth = Atompub::Auth::Wsse.new(
username: ENV['HATENA_USERNAME'],
password: ENV['HATENA_API_KEY']
)
atompub_client = Atompub::Client.new(auth: auth)
HatenaBlog::DraftMaker.call(atompub_client: atompub_client, contents: contents)