nikki.rbrequire "date"
require "json"
require "open-uri"
require "uri"
arg = ARGV.shift
date = arg ? Date.parse(arg) : Date.today
next_title = (date + 1).strftime("%Y-%m-%d %a : (書きかけ)")
title = date.strftime("%Y-%m-%d %a : (書きかけ)")
year_month_day = date.strftime("%Y-%m-%d")
year = date.strftime("%Y")
year_month = date.strftime("%Y年%m月")
month_day = date.strftime("%m月%d日")
week_number = "WN" + date.strftime("%V")
june2x = date.strftime("%m%d").start_with?("062") ? "\n[june2X] 週間。\n" : nil
f13 = date.friday? && date.day == 13 ? "[Friday the 13th]\n" : nil
good_meat = date.strftime("%m%d") == "1129" ? "[いい肉の日]\n" : nil
headers =
%w[ハイライト 生活 ページ 漫画].
map { |item| "[*** #{item}]"}.
join("\n")
base_url = "https://scrapbox.io/api/pages/june29/"
prev_title =
JSON.parse(URI.open("#{base_url}#{date - 1}").read).
dig("relatedPages", "links1hop").
find { |page| page.dig("title").match(Regexp.compile((date - 1).strftime("%Y-%m-%d %a : "))) }.
dig("title")
n_years_ago =
JSON.parse(URI.open("#{base_url}#{URI.encode_www_form_component(month_day)}").read).
dig("relatedPages", "links1hop").
select { |page| page.dig("title").match(Regexp.compile(date.strftime("\\d{4}-%m-%d \\w{3} : "))) }.
reject { |page| page.dig("title").include?("書きかけ") }.
sort_by { |page| page.dig("title") }.
reverse.
map { |page| t = page.dig("title"); " [%s] / [%s]" % [t[0..9], t] }.
join("\n")
puts <<~STRING
#{title}
##{month_day} ##{year}-#{week_number} ##{year_month_day} ##{year_month}
#{june2x}#{f13}#{good_meat}
#{headers}
[*** 今日の n 年前]
#{n_years_ago}
[*** ナビ]
前日 : [#{prev_title}]
翌日 : [#{next_title}]
#日記
https://api.june29.jp/nikki/#{date}
STRING