summaryrefslogtreecommitdiff
path: root/app/lib/markdown-writer.server.ts
blob: cfef3153716e4f3acd48af3ff431f97a0ea24f3e (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
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * Generates a Markdown summary file per venue after scraping.
 * Files are written to events/<venue-id>.md in the project root.
 */
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { queryEvents } from "./db.server";
import type { Event } from "./db.server";

const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), "../../");
const EVENTS_DIR = path.join(ROOT, "events");

export function generateVenueMarkdown(venueId: string): void {
  const events = queryEvents({ venue_id: venueId, limit: 200 });
  if (events.length === 0) return;

  fs.mkdirSync(EVENTS_DIR, { recursive: true });

  const venueName = events[0].venue_name;
  const venueArea = events[0].venue_area ?? "";
  const now = new Date().toISOString().slice(0, 10);

  const lines: string[] = [
    `# ${venueName}(${venueArea})イベント情報`,
    ``,
    `> 最終更新: ${now}  `,
    `> データソース: スクレイパー自動取得`,
    ``,
    `| 日付 | 出演者 | タイトル | 時間 | 料金 | URL |`,
    `| ---- | ------ | -------- | ---- | ---- | --- |`,
  ];

  for (const ev of events) {
    const date = formatDate(ev.date);
    const artist = escape(ev.artist ?? "未定");
    const title = escape(ev.title);
    const time = buildTime(ev.open_time, ev.start_time);
    const fee = escape(ev.price ?? "");
    const url = ev.ticket_url
      ? `[チケット](${ev.ticket_url})`
      : ev.source_url
      ? `[詳細](${ev.source_url})`
      : "";

    lines.push(`| ${date} | ${artist} | ${title} | ${time} | ${fee} | ${url} |`);
  }

  lines.push(``);
  lines.push(`---`);
  lines.push(`*このファイルは自動生成されます。手動編集は次回更新時に上書きされます。*`);
  lines.push(``);

  const filePath = path.join(EVENTS_DIR, `${venueId}.md`);
  fs.writeFileSync(filePath, lines.join("\n"), "utf-8");
}

export function generateAllVenueMarkdown(venueIds: string[]): void {
  for (const id of venueIds) {
    generateVenueMarkdown(id);
  }
}

function formatDate(iso: string): string {
  const [y, m, d] = iso.split("-");
  const days = ["日", "月", "火", "水", "木", "金", "土"];
  const dayIdx = new Date(`${iso}T00:00:00`).getDay();
  return `${y}/${m}/${d}(${days[dayIdx]})`;
}

function buildTime(open: string | null, start: string | null): string {
  const parts: string[] = [];
  if (open) parts.push(`OPEN ${open}`);
  if (start) parts.push(`START ${start}`);
  return parts.join(" / ") || "";
}

function escape(s: string): string {
  return s.replace(/\|/g, "\\|").replace(/\n/g, " ");
}