Buckets:
| import os | |
| import json | |
| import yaml | |
| import subprocess | |
| import base64 | |
| from typing import List, Dict, Any, Optional | |
| from mcp.server.fastmcp import FastMCP | |
| from pydantic import BaseModel | |
| # Initialize FastMCP server | |
| mcp = FastMCP("Ebook Builder") | |
| def get_workspace_path(path: str = ""): | |
| base = os.path.join(os.getcwd(), "ebook_workspace") | |
| if not os.path.exists(base): os.makedirs(base) | |
| return os.path.join(base, path) | |
| def init_book_workspace(title: str, author: str) -> str: | |
| """Initializes the eBook directory structure.""" | |
| ws = get_workspace_path() | |
| for d in ["chapters", "assets", "output", "front_matter"]: os.makedirs(os.path.join(ws, d), exist_ok=True) | |
| metadata = {"title": title, "author": author, "rights": f"Copyright 2026 {author}"} | |
| with open(os.path.join(ws, "metadata.yaml"), "w") as f: yaml.dump(metadata, f) | |
| return f"Workspace for '{title}' ready." | |
| def save_chapter(number: int, title: str, content: str) -> str: | |
| """Saves a chapter as a Markdown file.""" | |
| path = get_workspace_path(f"chapters/{number:02d}_{title.replace(' ', '_').lower()}.md") | |
| with open(path, "w") as f: f.write(f"# {title}\n\n{content}") | |
| return f"Chapter {number} saved." | |
| def add_front_matter(type: str, content: str) -> str: | |
| """Adds Preface, Foreword, or Acknowledgments.""" | |
| path = get_workspace_path(f"front_matter/{type.lower()}.md") | |
| with open(path, "w") as f: f.write(f"# {type}\n\n{content}") | |
| return f"{type} added." | |
| def get_book_status() -> Dict[str, Any]: | |
| """Returns progress report: chapter count and assets.""" | |
| ws = get_workspace_path() | |
| chapters = [f for f in os.listdir(os.path.join(ws, "chapters")) if f.endswith(".md")] | |
| assets = os.listdir(os.path.join(ws, "assets")) | |
| return {"chapters_count": len(chapters), "chapters": sorted(chapters), "assets": assets} | |
| def generate_cover_prompt(style: str = "cinematic") -> str: | |
| """Drafts an AI image prompt for the cover.""" | |
| ws = get_workspace_path() | |
| try: | |
| with open(os.path.join(ws, "metadata.yaml"), "r") as f: meta = yaml.safe_load(f) | |
| return f"A {style} book cover for '{meta['title']}' by {meta['author']}, highly detailed, masterpiece." | |
| except: return "Metadata not found. Init workspace first." | |
| def package_ebook(formats: List[str]) -> Dict[str, str]: | |
| """Compiles the book using Pandoc.""" | |
| ws = get_workspace_path() | |
| chapters = sorted([os.path.join(ws, "chapters", f) for f in os.listdir(os.path.join(ws, "chapters"))]) | |
| front = sorted([os.path.join(ws, "front_matter", f) for f in os.listdir(os.path.join(ws, "front_matter"))]) | |
| all_files = front + chapters | |
| results = {} | |
| for fmt in formats: | |
| out = os.path.join(ws, "output", f"book.{fmt}") | |
| cmd = ["pandoc"] + all_files + ["--metadata-file", os.path.join(ws, "metadata.yaml"), "-o", out] | |
| try: | |
| subprocess.run(cmd, check=True) | |
| results[fmt] = f"Success: {out}" | |
| except Exception as e: results[fmt] = f"Error: {e}" | |
| return results | |
| if __name__ == "__main__": | |
| mcp.run() | |
Xet Storage Details
- Size:
- 3.13 kB
- Xet hash:
- 1b6d235200816c8391e99aac3daace2f1c17c324051739d0ed0f8d2df95ecd7c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.