feature: download svgs instead of building them

This commit is contained in:
Matthias Jung
2025-11-09 08:42:29 +01:00
parent 169c81c5e0
commit 9f0d0ffc3a
2 changed files with 11 additions and 41 deletions
+1 -1
View File
@@ -14,8 +14,8 @@ dl.download_edition("N")
# Download additional files: # Download additional files:
dl.download_question_metadata() dl.download_question_metadata()
dl.download_pictures()
dl.download_photos() dl.download_photos()
dl.download_pictures()
dl.download_includes() dl.download_includes()
dl.download_snippets() dl.download_snippets()
dl.download_content() dl.download_content()
+9 -39
View File
@@ -1,9 +1,5 @@
import json import json
import multiprocessing
import os
import shutil
from joblib import Memory, Parallel, delayed
from tqdm import tqdm from tqdm import tqdm
from api.directus import DirectusAPI from api.directus import DirectusAPI
@@ -97,50 +93,24 @@ class Download:
for photo in tqdm(photos, desc="Downloading photos"): for photo in tqdm(photos, desc="Downloading photos"):
data = self.api.get_file("assets/" + photo["photo"]) data = self.api.get_file("assets/" + photo["photo"])
with (self.config.p_data_photos / f"{photo['id']}.jpg").open("wb") as file: with (self.config.p_data_photos / f"{photo["id"]}.jpg").open("wb") as file:
file.write(data) file.write(data)
def download_pictures(self): def download_pictures(self):
self.config.p_data_pictures.mkdir(parents=True, exist_ok=True) self.config.p_data_pictures.mkdir(parents=True, exist_ok=True)
# Symlink photos, so LaTeX can access these assets to render into graphics.
foto_link = self.config.p_data_pictures / "foto"
if not foto_link.is_symlink():
foto_link.symlink_to(self.config.p_data_photos)
memory = Memory("./cache", verbose=0) pictures = self.api.get("items/pictures", { "limit": -1 })
@memory.cache for picture in tqdm(pictures, desc="Downloading pictures") :
def build_picture(picture):
pid = picture["id"]
if ( # Download LaTeX Sources:
"\\begin{document}" in picture["latex"] or "\\begin{table}" in picture["latex"] with (self.config.p_data_pictures / f"{picture["id"]}include.tex").open("w") as file:
): # Workaround, if the picture contains a document class it should be ignored!
return
if self.config.no_latex:
shutil.copyfile("./assets/images/50ohm_gray.svg", self.config.p_data_pictures / f"{pid}.svg")
else:
with (self.config.p_data_pictures / f"img/{pid}include.tex").open("w") as file:
file.write(picture["latex"]) file.write(picture["latex"])
aux_file = self.config.p_data_photos / f"{pid}.tex" # Download SVGs:
with aux_file.open("w", encoding="utf-8") as file: data = self.api.get_file(f"assets/{picture["picture"]}")
# Write the auxilary LaTeX file: with (self.config.p_data_pictures / f"{picture["id"]}.svg").open("wb") as file:
file.write(f"\\documentclass{{BNetzA-Fragenkatalog}}\\DARCimageOnly{{9cm}}{{{pid}include}}") file.write(data)
# Build the picture:
os.system(f"latexmk -lualatex -f -interaction=nonstopmode {aux_file} > /dev/null 2>&1")
# Convert the PDF to SVG:
os.system(
"pdftocairo -svg"
f"{self.config.p_data_pictures / f'{pid}.pdf'} - > {self.config.p_data_pictures / f'{pid}.svg'}"
)
pictures = self.api.get("items/pictures", params={"limit": -1})
Parallel(n_jobs=multiprocessing.cpu_count())(
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
)
def download_includes(self): def download_includes(self):
includes = self.api.get("items/include", params={"limit": -1}) includes = self.api.get("items/include", params={"limit": -1})