File: examples/companions/nested-catalog/make_nested_catalog.py (124 lines)
#!/usr/bin/env python3
"""Write an index.xml into every folder of a book tree, for the nested-catalog example.
usage:
make_nested_catalog.py LIBRARY_DIR
LIBRARY_DIR the root folder of your books, with any number of subfolders:
library/
+-- Fiction/
| +-- Science Fiction/
| | +-- Dune.epub
| | +-- Hyperion.epub
| +-- Fantasy/The Hobbit.epub
+-- Non-Fiction/Cosmos.epub
Afterwards every folder that contains books (at any depth) has an index.xml.
Upload the whole tree to a static host (GitHub, Pages, S3, a NAS...) and point
device.json "browse.url" at <public URL of LIBRARY_DIR>/index.xml.
All links in the XML are RELATIVE (a subfolder's "Fantasy/index.xml", a book's
"Dune.epub"), and the reader resolves them against the folder of the index.xml
it is browsing. That's why the same files work on any host, with no base URL
baked in. The reader's limits per folder: 200 entries, about 64 KB of
titles/subtitles/links.
"""
import pathlib
import re
import sys
import urllib.parse
import xml.etree.ElementTree as ET
import zipfile
from xml.sax.saxutils import escape, quoteattr
BOOK_TYPES = {".epub", ".xtc", ".txt", ".md"} # keep in sync with device.json "extensions"
INDEX = "index.xml"
MAX_ENTRIES = 200
FIELD_BUDGET = 60 * 1024
NS = {"c": "urn:oasis:names:tc:opendocument:xmlns:container",
"dc": "http://purl.org/dc/elements/1.1/"}