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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#!/usr/bin/env npx tsx
/**
* tokyo-livehouse-events の events.db から全出演者を取得し、
* whois-band に自動登録するスクリプト。
*
* Usage:
* npx tsx scripts/import-livehouse-events.ts
*
* Optional env vars:
* DB_PATH whois.db のパス (default: /app/whois-band/data/whois.db)
* EVENTS_DB_PATH events.db のパス (default: /app/tokyo-livehouse-events/data/events.db)
* DRY_RUN=1 登録せず結果だけ表示
*/
import Database from "better-sqlite3";
import { randomUUID } from "crypto";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const WHOIS_DB_PATH = process.env.DB_PATH ?? "/app/whois-band/data/whois.db";
const EVENTS_DB_PATH = process.env.EVENTS_DB_PATH ?? "/app/tokyo-livehouse-events/data/events.db";
const DRY_RUN = process.env.DRY_RUN === "1";
// ── Artist name parser ────────────────────────────────────────────────────────
const NOISE_WORDS = new Set([
"tour", "presents", "present", "and", "feat", "feat.", "with", "guest",
"vs", "vs.", "×", "starring",
]);
const NOISE_PATTERNS = [
/\d{1,2}:\d{2}/, // time HH:MM
/^[\d\s\/\-\.]+$/, // numbers/dates/slashes only
/\.(com|net|jp|org)\b/i, // domain names
/^(OPEN|START|ADV|DOOR)/i, // ticket info
/^(出演|単独公演|二日間|開催|開場|ゲスト)/,
];
function isNoise(name: string): boolean {
const trimmed = name.trim();
if (!trimmed || trimmed.length < 2 || trimmed.length > 60) return true;
if (NOISE_WORDS.has(trimmed.toLowerCase())) return true;
return NOISE_PATTERNS.some((re) => re.test(trimmed));
}
/**
* Remove all balanced parenthetical content from a string.
* e.g. "KENNY DOPE (MASTERS AT WORK)" → "KENNY DOPE"
* e.g. "MyM (Miyuki・Yoshiko・Mahiru)" → "MyM"
* Keeps the content if there's nothing else left (entire string is in parens).
*/
function stripParens(s: string): string {
let result = s;
// Strip standard round parens and their Japanese variants
result = result.replace(/[\((][^)))]*[\))]/g, "");
// Strip any leftover unmatched parens
result = result.replace(/[\((()))]/g, "");
const stripped = result.trim();
// Fall back to original if nothing remains
return stripped.length >= 2 ? stripped : s.trim();
}
/**
* Find indices of top-level "/" separators (not inside parens).
*/
function findTopLevelSlashes(s: string): number[] {
const positions: number[] = [];
let depth = 0;
for (let i = 0; i < s.length; i++) {
const ch = s[i];
if (ch === "(" || ch === "(") depth++;
else if (ch === ")" || ch === ")") depth = Math.max(0, depth - 1);
else if (ch === "/" && depth === 0) positions.push(i);
}
return positions;
}
function splitOnTopLevelSlash(s: string): string[] {
const slashes = findTopLevelSlashes(s);
if (slashes.length === 0) return [s];
const parts: string[] = [];
let last = 0;
// Handle "//" (double slash) as a single separator
const merged: number[] = [];
for (let i = 0; i < slashes.length; i++) {
if (i > 0 && slashes[i] === slashes[i - 1] + 1) continue; // skip second of "//"
merged.push(slashes[i]);
// If next slash is consecutive, advance last by 2 after processing
}
for (const pos of merged) {
const part = s.slice(last, pos).trim();
if (part) parts.push(part);
// Skip consecutive slash (for "//")
last = (s[pos + 1] === "/") ? pos + 2 : pos + 1;
}
const tail = s.slice(last).trim();
if (tail) parts.push(tail);
return parts;
}
function extractNames(raw: string): string[] {
let s = raw;
// 1. Replace venue markers like =O-EAST= =AZUMAYA= with "/" so they act as separators
s = s.replace(/=([^=]+)=\s*/g, " / ");
// 2. Strip bracket blocks [members] <detail> 【...】 [...]
s = s.replace(/[\[【<<[][^\]】>>]]*[\]】>>]]/g, "");
// 3. Strip "ARTIST Presents EVENT" → keep only ARTIST
// (match " presents " followed by text until "/" or end)
s = s.replace(/\s+[Pp]resents\s+"?[^/"、/]+/g, "");
s = s.replace(/\s+[Pp]resent\s+"?[^/"、/]+/g, "");
// 4. Split on top-level "/" (not inside parens), then on 「、」
const slashParts = splitOnTopLevelSlash(s);
const allParts: string[] = [];
for (const part of slashParts) {
// Further split on 「、」 (Japanese comma) — this is always a top-level separator
const jpParts = part.split(/[、]/).map((p) => p.trim()).filter(Boolean);
allParts.push(...jpParts);
}
// 5. Clean each part
return allParts
.map((name) => {
// Remove trailing parenthetical content (label names, member lists, roles)
let cleaned = stripParens(name);
// Remove leading/trailing punctuation noise
cleaned = cleaned
.replace(/^[「」『』【】〈〉《》\[\]"'・\s]+/, "")
.replace(/[「」『』【】〈〉《》\[\]"'・\s]+$/, "")
.trim();
return cleaned;
})
.filter((name) => !isNoise(name));
}
// ── DB helpers ────────────────────────────────────────────────────────────────
function toSlug(name: string): string {
return name
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w-ヿ一-鿿--]/g, "")
.replace(/^-+|-+$/g, "");
}
interface Band { id: string; name: string; slug: string; }
function insertBand(db: Database.Database, name: string): string {
const id = randomUUID();
const baseSlug = toSlug(name) || id.slice(0, 8);
for (let attempt = 0; attempt < 3; attempt++) {
const slug = attempt === 0 ? baseSlug : `${baseSlug}-${id.slice(0, 6)}`;
try {
db.transaction(() => {
db.prepare(
"INSERT INTO bands (id, slug, name, area, description, status) VALUES (?, ?, ?, ?, ?, ?)"
).run(id, slug, name, null, null, "active");
const band = db.prepare("SELECT * FROM bands WHERE id = ?").get(id);
db.prepare(
"INSERT INTO band_revisions (id, band_id, snapshot, message, ip_address) VALUES (?, ?, ?, ?, ?)"
).run(
randomUUID(),
id,
JSON.stringify({ band, links: [], members: [] }),
"tokyo-livehouse-events から自動インポート",
"cli-import"
);
})();
return id;
} catch (e) {
if (e instanceof Error && e.message.includes("UNIQUE constraint failed")) continue;
throw e;
}
}
throw new Error(`スラッグ競合で登録失敗: ${name}`);
}
// ── Main ──────────────────────────────────────────────────────────────────────
async function main() {
console.log("=== tokyo-livehouse-events → whois-band インポーター ===");
console.log(`events DB: ${EVENTS_DB_PATH}`);
console.log(`whois DB: ${WHOIS_DB_PATH}`);
if (DRY_RUN) console.log("⚠️ DRY_RUN モード(登録しません)");
console.log();
// 1. events.db から全出演者文字列を取得
const eventsDb = new Database(EVENTS_DB_PATH, { readonly: true });
const artistRows = eventsDb
.prepare("SELECT DISTINCT artist FROM events WHERE artist IS NOT NULL AND artist != ''")
.all() as { artist: string }[];
eventsDb.close();
console.log(`出演者フィールド数: ${artistRows.length}`);
// 2. 全名前を抽出・重複排除(スラッグで判定)
const extracted = new Map<string, string>(); // slug → name (最初に出現したものを採用)
for (const { artist } of artistRows) {
for (const name of extractNames(artist)) {
const slug = toSlug(name);
if (slug && !extracted.has(slug)) {
extracted.set(slug, name);
}
}
}
console.log(`抽出したユニーク名(スラッグ重複排除後): ${extracted.size}`);
// 3. whois.db を開いて既存バンドと照合
const whoisDb = new Database(WHOIS_DB_PATH);
whoisDb.pragma("journal_mode = WAL");
whoisDb.pragma("foreign_keys = ON");
const existingBands = whoisDb
.prepare("SELECT id, name, slug FROM bands")
.all() as Band[];
const existingSlugs = new Set(existingBands.map((b) => b.slug));
const existingNames = new Set(existingBands.map((b) => b.name.toLowerCase()));
console.log(`既存バンド数: ${existingBands.length}\n`);
const toRegister: string[] = [];
const skipped: string[] = [];
for (const [slug, name] of extracted) {
if (existingSlugs.has(slug) || existingNames.has(name.toLowerCase())) {
skipped.push(name);
} else {
toRegister.push(name);
}
}
console.log(`新規登録対象: ${toRegister.length}件`);
console.log(`既存スキップ: ${skipped.length}件\n`);
if (DRY_RUN) {
console.log("=== ドライランサマリ ===");
console.log("登録予定バンド(先頭100件):");
toRegister.slice(0, 100).forEach((n) => console.log(` + ${n}`));
if (toRegister.length > 100) console.log(` ... 他 ${toRegister.length - 100} 件`);
return;
}
// 4. 登録
let registered = 0;
let errors = 0;
for (const name of toRegister) {
try {
insertBand(whoisDb, name);
registered++;
if (registered % 100 === 0) {
process.stdout.write(` ... ${registered}件登録済\n`);
}
} catch (e) {
console.error(` ❌ "${name}": ${e instanceof Error ? e.message : e}`);
errors++;
}
}
whoisDb.close();
console.log("\n=== 結果サマリ ===");
console.log(`登録完了: ${registered}件`);
console.log(`スキップ(既存): ${skipped.length}件`);
if (errors > 0) console.log(`エラー: ${errors}件`);
const finalCount = whoisDb
? 0
: (() => {
const db2 = new Database(WHOIS_DB_PATH, { readonly: true });
const c = (db2.prepare("SELECT COUNT(*) as c FROM bands").get() as { c: number }).c;
db2.close();
return c;
})();
const db2 = new Database(WHOIS_DB_PATH, { readonly: true });
const finalTotal = (db2.prepare("SELECT COUNT(*) as c FROM bands").get() as { c: number }).c;
db2.close();
console.log(`DBのバンド総数: ${finalTotal}件`);
}
main().catch((e) => { console.error(e); process.exit(1); });
|