1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 ? '/予定データに無いもの' : ''})`);
|