summaryrefslogtreecommitdiff
path: root/scripts/scrape.ts
blob: 62e9172504f6cb42133c16b7707129b3c47c4ee7 (plain)
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
#!/usr/bin/env node
/**
 * CLI スクレイパー
 *
 * 使い方:
 *   npm run scrape              # 全会場
 *   npm run scrape liquid-room  # 特定会場
 *   npm run scrape -- --list    # 登録済み会場を表示
 */
import { styleText } from "node:util";
import { runAllScrapers, runScraper } from "../app/lib/scraper-runner.server.js";
import { ALL_SCRAPERS } from "../app/scrapers/index.js";

const args = process.argv.slice(2);
const venueId = args.find((a) => !a.startsWith("--"));
const flagList = args.includes("--list");

if (flagList) {
  console.log(styleText("bold", "\n登録済みスクレイパー一覧:\n"));
  for (const s of ALL_SCRAPERS) {
    console.log(`  ${styleText("cyan", s.venue.id.padEnd(25))} ${s.venue.name} (${s.venue.area})`);
  }
  console.log();
  process.exit(0);
}

const startTime = Date.now();

console.log(
  styleText("bold", "\n🎸 東京ライブハウス スクレイパー\n") +
    (venueId
      ? `  対象: ${styleText("cyan", venueId)}\n`
      : `  対象: ${styleText("cyan", `全 ${ALL_SCRAPERS.length} 会場`)}\n`)
);

const results = venueId
  ? [await runScraper(venueId)]
  : await runAllScrapers();

const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
let totalSaved = 0;
let hasError = false;

for (const r of results) {
  const icon = r.status === "ok" ? styleText("green", "✔") : styleText("red", "✖");
  const name = r.venue_name.padEnd(24);

  if (r.status === "ok") {
    const count = styleText("green", `${r.events_saved} 件`);
    console.log(`  ${icon} ${name} ${count}`);
    totalSaved += r.events_saved;
  } else {
    hasError = true;
    console.log(`  ${icon} ${styleText("red", name)}`);
    if (r.error) {
      const lines = r.error.split("\n");
      for (const line of lines) {
        console.log(`     ${styleText("dim", line)}`);
      }
    }
  }
}

const summary = `\n  合計 ${styleText("bold", `${totalSaved} 件`)} 保存  (${elapsed}s)`;
console.log(summary + "\n");

process.exit(hasError ? 1 : 0);