#!/usr/bin/env node // 小平市立図書館のイベントカレンダーから開催日を取得し、 // 行事予定ページの図書館イベント行(日付セル)を更新する。 // // 使い方: // node scripts/update-library-dates.mjs … 変更内容を表示(書き換えない) // node scripts/update-library-dates.mjs --apply … 実際に書き換える // node scripts/update-library-dates.mjs --months 4 … 当月から4か月分を取得(既定3) // // 行の追加・削除は自動では行わない(内容を確認して手で編集する)。 // 表にない新しいイベント、カレンダーから消えたイベントは一覧で表示する。 import fs from 'node:fs'; import path from 'node:path'; const BASE = 'https://library.kodaira.ed.jp/viewer/'; 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 apply = args.includes('--apply'); const monthsArg = args.indexOf('--months'); const months = monthsArg >= 0 ? Number(args[monthsArg + 1]) || 3 : 3; const targetIdx = args.indexOf('--target'); const targetPath = targetIdx >= 0 ? args[targetIdx + 1] : path.join('src', 'data', 'schedule', 'events.json'); async function fetchText(url) { const res = await fetch(url, { headers: { 'User-Agent': UA } }); if (!res.ok) throw new Error(`${res.status} ${url}`); return res.text(); } function normalize(html) { return html.replace(/\s+/g, ' '); } // カレンダーから { id: { title, lib, dates:Set } } を作る async function collectEvents() { const now = new Date(); const events = new Map(); for (let i = 0; i < months; i += 1) { const d = new Date(now.getFullYear(), now.getMonth() + i, 1); const url = `${BASE}calendar-monthly.html?date=${d.getFullYear()}/${d.getMonth() + 1}/1`; const html = normalize(await fetchText(url)); const rows = html.match(/