diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-11-17 23:28:34 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-11-17 23:28:34 +0900 |
| commit | f7240a325c5647e2f3fb46a81ed51bef51b2730b (patch) | |
| tree | 6c37343f4e24be077457c4648369c5850136d258 /scripts | |
| parent | 69e32f1f4a171b6575e2cf2565c87664fce583c8 (diff) | |
fetch-city-events.mjs: 市ホームページのイベント一覧を走査するスクリプトを追加
「/event/」の各イベントから開催日を取り出し、今日以降のものを一覧表示する。--missing を付けると予定データに無いものを抽出できる(題名の先頭6文字で照合するため、表記ゆれによる誤検出はある)。
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/fetch-city-events.mjs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/fetch-city-events.mjs b/scripts/fetch-city-events.mjs new file mode 100644 index 0000000..50ad72d --- /dev/null +++ b/scripts/fetch-city-events.mjs @@ -0,0 +1,76 @@ +// 小平市ホームページのイベント一覧(/event/)から、これからのイベントを取り出す。 +// +// 使い方: +// node scripts/fetch-city-events.mjs # 今日以降のイベントを一覧 +// node scripts/fetch-city-events.mjs --missing # 予定データに無いものだけ表示 +// node scripts/fetch-city-events.mjs --days 30 # 何日先まで見るか +// +// ページは各イベントが +// <div class="event_info" data-honbun="..."> … <h3><a href="/event/…">題名</a></h3> +// … <li data-date_start="UNIX秒" …> +// の形で並んでいる。日付は 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(/<h3><a href="([^"]+)">([\s\S]*?)<\/a>/); + if (!link) continue; + const title = link[2].replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim(); + const starts = [...block.matchAll(/data-date_start="(\d+)"/g)].map((m) => Number(m[1])); + if (!starts.length) continue; + const start = new Date(Math.min(...starts) * 1000 + 9 * 3600 * 1000); + const key = start.toISOString().slice(0, 10); + if (key < todayKey || key > limitKey) continue; + items.push({ date: key, title, url: `https://www.city.kodaira.tokyo.jp${link[1]}` }); +} + +items.sort((a, b) => a.date.localeCompare(b.date)); + +const rows = onlyMissing + ? [ + ...JSON.parse(readFileSync('src/data/schedule/events.json', 'utf8')).rows, + ...JSON.parse(readFileSync('src/data/schedule/meetings.json', 'utf8')).rows, + ] + : []; + +// 予定データに入っているかの判定(括弧書きや年度・数字を除いた題名の先頭6文字を手がかりにする) +const normalize = (s) => + String(s) + .replace(/[【((][^】))]*[】))]/g, '') // 括弧書き(日付・補足)を除く + .replace(/令和\d+年度|\d{4}/g, '') + .replace(/[\s【】()()「」・]/g, ''); +const haystack = rows.map((r) => normalize(r.title) + normalize(r.note)); +const isListed = (title) => { + const key = normalize(title).slice(0, 6); + return key.length >= 4 && haystack.some((t) => t.includes(key)); +}; + +const output = onlyMissing ? items.filter((i) => !isListed(i.title)) : items; + +for (const item of output) { + console.log(`${item.date} | ${item.title} | ${item.url}`); +} +console.log(`\n${output.length}件(${todayKey}〜${limitKey}${onlyMissing ? '/予定データに無いもの' : ''})`); |
