#!/usr/bin/env python3 """Build curriculum.json from the workbook and unified SSS asset root.""" from __future__ import annotations import argparse import json import re from pathlib import Path from openpyxl import load_workbook SONG_PATTERN = re.compile(r"^(\d+)-(\d+)\s+(.+)$") REVIEW_PATTERN = re.compile(r"复习(\d+)-(\d+)周") ASSET_SPECS = { "video_file": ("视频", ".mp4"), "audio_file": ("音频", ".mp3"), "lyrics_file": ("歌词", ".jpg"), } def files_by_song_id(folder: Path, extension: str) -> dict[str, str]: result = {} for path in sorted(folder.glob(f"*{extension}")): match = re.match(r"^(\d+-\d+)\s+", path.stem) if match: result[match.group(1)] = path.name return result def build_curriculum(workbook_path: Path, asset_root: Path) -> dict: workbook = load_workbook(workbook_path, data_only=True, read_only=True) worksheet = workbook["50周打卡"] assets = { field: files_by_song_id(asset_root / folder, extension) for field, (folder, extension) in ASSET_SPECS.items() } weeks: dict[int, dict] = {} current_week = None for week_value, category_value, song_value in worksheet.iter_rows( min_row=3, max_col=3, values_only=True ): if isinstance(week_value, int): current_week = week_value category = str(category_value or "").strip() review_match = REVIEW_PATTERN.search(category) weeks[current_week] = { "number": current_week, "kind": "review" if review_match else "lesson", "category": category, "songs": [], } if review_match: weeks[current_week]["review_from"] = int(review_match.group(1)) weeks[current_week]["review_to"] = int(review_match.group(2)) if current_week is None or not isinstance(song_value, str): continue match = SONG_PATTERN.match(song_value.strip()) if not match: continue song_id = f"{match.group(1)}-{match.group(2)}" song = { "id": song_id, "slot": int(match.group(2)), "title": match.group(3), } for field, mapping in assets.items(): if song_id not in mapping: raise RuntimeError(f"Missing {field} for {song_id}") song[field] = mapping[song_id] weeks[current_week]["songs"].append(song) if sorted(weeks) != list(range(1, 51)): raise RuntimeError("Workbook must contain weeks 1 through 50") songs = [song for week in weeks.values() for song in week["songs"]] song_ids = {song["id"] for song in songs} for field, mapping in assets.items(): unused = sorted(set(mapping) - song_ids) if unused: raise RuntimeError(f"Unused {field}: {unused}") return { "meta": { "title": "SSS 50周学习计划", "source_workbook": workbook_path.name, "asset_root_name": asset_root.name, "song_count": len(songs), "week_count": 50, }, "weeks": [weeks[number] for number in sorted(weeks)], } def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("workbook", type=Path) parser.add_argument("asset_root", type=Path) parser.add_argument("output", type=Path) args = parser.parse_args() result = build_curriculum(args.workbook, args.asset_root) args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text( json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8" ) print( f"Wrote {result['meta']['week_count']} weeks and " f"{result['meta']['song_count']} songs to {args.output}" ) if __name__ == "__main__": main()