|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Annotated |
| 3 | +from uuid import UUID |
| 4 | +from xml.etree import ElementTree as ET |
| 5 | + |
| 6 | +from fastapi import APIRouter, Depends, Path |
| 7 | +from fastapi.responses import Response |
| 8 | +from sqlalchemy.orm import Session as OrmSession |
| 9 | + |
| 10 | +from cms_backend.db import gen_dbsession |
| 11 | +from cms_backend.db.exceptions import RecordDoesNotExistError |
| 12 | +from cms_backend.db.library import ( |
| 13 | + get_latest_books_for_library, |
| 14 | + get_library, |
| 15 | + get_library_by_name_or_none, |
| 16 | +) |
| 17 | +from cms_backend.db.models import Book |
| 18 | + |
| 19 | +router = APIRouter(prefix="/libraries", tags=["libraries"]) |
| 20 | + |
| 21 | + |
| 22 | +def _build_library_xml(books: list[Book]) -> str: |
| 23 | + """Build XML library catalog from books.""" |
| 24 | + library_elem = ET.Element("library") |
| 25 | + library_elem.set("version", "20110515") |
| 26 | + |
| 27 | + for book in books: |
| 28 | + if not book.zim_metadata: |
| 29 | + continue |
| 30 | + |
| 31 | + book_elem = ET.SubElement(library_elem, "book") |
| 32 | + |
| 33 | + # Required attributes |
| 34 | + book_elem.set("id", str(book.id)) |
| 35 | + book_elem.set("size", str(book.size)) |
| 36 | + book_elem.set("mediaCount", str(book.media_count)) |
| 37 | + book_elem.set("articleCount", str(book.article_count)) |
| 38 | + |
| 39 | + # Metadata from zim_metadata dict |
| 40 | + zim_meta = book.zim_metadata |
| 41 | + book_elem.set("title", zim_meta.get("Title", "")) |
| 42 | + book_elem.set("description", zim_meta.get("Description", "")) |
| 43 | + book_elem.set("language", zim_meta.get("Language", "")) |
| 44 | + book_elem.set("creator", zim_meta.get("Creator", "")) |
| 45 | + book_elem.set("publisher", zim_meta.get("Publisher", "")) |
| 46 | + book_elem.set("name", zim_meta.get("Name", "")) |
| 47 | + book_elem.set("date", zim_meta.get("Date", "")) |
| 48 | + |
| 49 | + # Optional tags - combine with underscores if present |
| 50 | + tags = zim_meta.get("Tags", "") |
| 51 | + if tags: |
| 52 | + book_elem.set("tags", tags) |
| 53 | + |
| 54 | + # Favicon and faviconMimeType - these are typically extracted from the ZIM |
| 55 | + # but for now we'll use empty strings as the data structure doesn't contain them |
| 56 | + favicon = zim_meta.get("Illustration_48x48@1", "") |
| 57 | + if favicon: |
| 58 | + book_elem.set("favicon", favicon) |
| 59 | + book_elem.set("faviconMimeType", "image/png") |
| 60 | + |
| 61 | + # URL - would need to be constructed from warehouse config |
| 62 | + # For now, leaving empty as warehouse config mapping is not implemented |
| 63 | + url = zim_meta.get("URL", "") |
| 64 | + if url: |
| 65 | + book_elem.set("url", url) |
| 66 | + |
| 67 | + return ET.tostring(library_elem, encoding="unicode") |
| 68 | + |
| 69 | + |
| 70 | +@router.get("/{library_id_or_name}/catalog.xml") |
| 71 | +async def get_library_catalog_xml( |
| 72 | + library_id_or_name: Annotated[str, Path()], |
| 73 | + session: Annotated[OrmSession, Depends(gen_dbsession)], |
| 74 | +): |
| 75 | + """Get library catalog as XML. Library can be specified by ID (UUID) or name.""" |
| 76 | + # Try to parse as UUID first, otherwise treat as name |
| 77 | + library = None |
| 78 | + try: |
| 79 | + library_id = UUID(library_id_or_name) |
| 80 | + try: |
| 81 | + library = get_library(session, library_id) |
| 82 | + except RecordDoesNotExistError: |
| 83 | + pass |
| 84 | + except ValueError: |
| 85 | + # Not a valid UUID, try as name |
| 86 | + library = get_library_by_name_or_none(session, library_id_or_name) |
| 87 | + |
| 88 | + if library is None: |
| 89 | + return Response( |
| 90 | + content='<?xml version="1.0" encoding="UTF-8"?>' |
| 91 | + '<library version="20110515"></library>', |
| 92 | + status_code=HTTPStatus.NOT_FOUND, |
| 93 | + media_type="application/xml", |
| 94 | + ) |
| 95 | + |
| 96 | + books = get_latest_books_for_library(session, library.id) |
| 97 | + xml_content = _build_library_xml(books) |
| 98 | + |
| 99 | + return Response( |
| 100 | + content=xml_content, |
| 101 | + status_code=HTTPStatus.OK, |
| 102 | + media_type="application/xml", |
| 103 | + ) |
0 commit comments