// 小平市ホームページのイベント一覧(/event/)から、これからのイベントを取り出す。
//
// 使い方:
// node scripts/fetch-city-events.mjs # 今日以降のイベントを一覧
// node scripts/fetch-city-events.mjs --missing # 予定データに無いものだけ表示
// node scripts/fetch-city-events.mjs --days 30 # 何日先まで見るか
//
// ページは各イベントが
//
…
// …
// の形で並んでいる。日付は data-date_start(開始日時)を使う。
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
const UA =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
const args = process.argv.slice(2);
const onlyMissing = args.includes('--missing');
const daysIndex = args.indexOf('--days');
const days = daysIndex >= 0 ? Number(args[daysIndex + 1]) : 120;
const html = execFileSync(
'curl',
['-s', '--max-time', '60', '-A', UA, 'https://www.city.kodaira.tokyo.jp/event/'],
{ encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 },
);
const pad = (n) => String(n).padStart(2, '0');
const toKey = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
const now = new Date();
const todayKey = toKey(now);
const limit = new Date(now);
limit.setDate(limit.getDate() + days);
const limitKey = toKey(limit);
const items = [];
for (const block of html.split('class="event_info"').slice(1)) {
const link = block.match(/