|
# frozen_string_literal: true |
|
|
|
require 'bundler/setup' |
|
require 'esa' |
|
require 'time' |
|
|
|
# pattern 1: "日報" が名前より先 |
|
search_query = 'in:"(unsorted)/all" title:日報' |
|
daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})(/|-)(?<month>[0-9]+)(/|-)(?<day>[0-9]+)[\s ]*([((].+[))])*[\s ]*日報[\s ]*(?<name>.+)} |
|
|
|
# pattern 2: "日報" が名前より先で (1) などの数字入り |
|
# search_query = 'in:"(unsorted)/all" title:日報 title:"(1)"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]+)/(?<day>[0-9]+)[\s ]*([((].+[))])*[\s ]*日報[\s ]*(?<name>.+)[\s ]([(()]\d+[))])*\Z} |
|
|
|
# pattern 3: 週報 |
|
# search_query = 'in:"(unsorted)/all" title:週報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})(/|-)(?<month>[0-9]+)(/|-)(?<day>[0-9]+)[\s ]*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)*[\s ](?<name>(ほぼ|隔)*週報.+)} |
|
|
|
# pattern 4: 週報その2 |
|
# search_query = 'in:"(unsorted)/all" title:週報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})(/|-)(?<month>[0-9]+)(/|-)(?<day>[0-9]+)(?<name>(ほぼ|隔)*週報.+)} |
|
|
|
# 特殊フォーマットその1 |
|
# search_query = 'in:"(unsorted)/all" title:"日報 201"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/日報[\s ](?<year>[0-9]{4})/(?<month>[0-9]+)/(?<day>[0-9]+)[\s ]*([((].+[))])[\s ]*-[\s ]*(?<name>.+)} |
|
|
|
# 特殊フォーマットその2 |
|
# search_query = 'in:"(unsorted)/all" title:"日報 201"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/日報[\s ](?<year>[0-9]{4})/(?<month>[0-9]+)/(?<day>[0-9]+)[\s ]*([((].+[))])[\s ]*(?<name>.+)} |
|
|
|
# 特殊フォーマットその3 |
|
# search_query = 'in:"(unsorted)/all" title:"日報 201"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/日報[\s ](?<year>[0-9]{4})(/|\s| )(?<month>[0-9]+)(/|\s| )(?<day>[0-9]+)[\s ](?<name>.+)} |
|
|
|
# 特殊フォーマットその4 |
|
# search_query = 'in:"(unsorted)/all" title:"日報 201"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/日報[\s ](?<year>[0-9]{4})(?<month>[0-9]{2})(?<day>[0-9]{2})[\s ](?<name>.+)} |
|
|
|
# 特殊フォーマットその5 |
|
# search_query = 'in:"(unsorted)/all" title:日報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})([((].+[))])[\s ](?<name>.+)[\s ]日報(?<additional_title>[\s ].+)*} |
|
|
|
# 特殊フォーマットその6 |
|
# search_query = 'in:"(unsorted)/all" title:日報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})[\s ](Mon|Tue|Wed|Thu|Fri|Sat|Sun)[\s ]日報[\s ](?<name>.+)} |
|
|
|
# 特殊フォーマットその7 |
|
# search_query = 'in:"(unsorted)/all" title:"\'s 日報"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})[\s ](?<name>.+)} |
|
|
|
# 特殊フォーマットその8 |
|
# search_query = 'in:"(unsorted)/all" title:日報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})[\s ](?<name>.+)[\s ]日報\Z} |
|
|
|
# 特殊フォーマットその9 |
|
# search_query = 'in:"(unsorted)/all" title:日報' |
|
# daily_report_regexp = %r{\A\(unsorted\)/all/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})[\s ]*[<<][\s ]*日報[\s ]*[>>][\s ]*(?<name>.+)} |
|
|
|
# 後から修正用 |
|
# search_query = 'in:"(unsorted)/日報" title:"(1)"' |
|
# daily_report_regexp = %r{\A\(unsorted\)/日報/(?<year>[0-9]{4})/(?<month>[0-9]+)/(?<day>[0-9]+)(\s\(.\))/(?<name>.+)[\s ]([(()]\d+[))])*\Z} |
|
|
|
client = Esa::Client.new(access_token: ENV.fetch('ESA_ACCESS_TOKEN'), current_team: ENV.fetch('ESA_TEAM')) |
|
|
|
update_posts = [] |
|
page = 1 |
|
|
|
loop do |
|
posts_response = client.posts(q: search_query, page: page, per_page: 100) |
|
|
|
posts_response.body['posts'].each do |post| |
|
number = post['number'] |
|
full_name = post['full_name'] |
|
|
|
matches = daily_report_regexp.match(full_name)&.named_captures |
|
next unless matches |
|
|
|
begin |
|
date = Date.parse("#{matches['year']}/#{matches['month']}/#{matches['day']}") |
|
rescue ArgumentError |
|
puts matches |
|
next |
|
end |
|
|
|
wday = %w[日 月 火 水 木 金 土][date.wday] |
|
renamed_category = "(unsorted)/日報/#{date.strftime('%Y/%2m/%2d')} (#{wday})" |
|
name = matches['additional_title'] ? "#{matches['name']}#{matches['additional_title']}" : matches['name'] |
|
|
|
update_post = { number: number, name: name, category: renamed_category } |
|
puts "before: '#{full_name}', after: '#{update_post[:category]}/#{update_post[:name]}'" |
|
update_posts << update_post |
|
end |
|
|
|
page = posts_response.body['next_page'] |
|
puts "page: #{page}" |
|
break if page.nil? |
|
break if page > 100 # 10000 アイテム以上はページネーションで取得できないため |
|
end |
|
|
|
puts "update_posts: #{update_posts.count}" |
|
|
|
if ARGV[0] == 'run' |
|
update_posts.each do |update_post| |
|
client.update_post(update_post[:number], name: update_post[:name], category: update_post[:category]) |
|
end |
|
end |