feature: added code to provide first data download
This commit is contained in:
+67
-17
@@ -1,5 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
|
import multiprocessing
|
||||||
|
import os
|
||||||
|
|
||||||
|
from joblib import Memory, Parallel, delayed
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
|
||||||
@@ -7,24 +10,23 @@ class Download:
|
|||||||
def __init__(self, api):
|
def __init__(self, api):
|
||||||
self.api = api
|
self.api = api
|
||||||
|
|
||||||
def download_edition(cls, edition) :
|
def download_edition(self, edition) :
|
||||||
print("Downloading edition " + edition)
|
|
||||||
|
|
||||||
in_edition = cls.api.get_one(
|
in_edition = self.api.get_one(
|
||||||
"items/edition",
|
"items/edition",
|
||||||
{"filter": {"ident": {"_eq": edition}}}
|
{"filter": {"ident": {"_eq": edition}}}
|
||||||
)
|
)
|
||||||
|
|
||||||
in_chapters = cls.api.get(
|
in_chapters = self.api.get(
|
||||||
"items/chapter",
|
"items/chapter",
|
||||||
{"filter": {"edition": {"edition_id": {"_eq": in_edition["id"]}}}},
|
{"filter": {"edition": {"edition_id": {"_eq": in_edition["id"]}}}},
|
||||||
)
|
)
|
||||||
|
|
||||||
out_chapters = []
|
out_chapters = []
|
||||||
|
|
||||||
for in_chapter in tqdm(in_chapters):
|
for in_chapter in tqdm(in_chapters, desc="Downloading edition"):
|
||||||
|
|
||||||
in_sections = cls.api.get(
|
in_sections = self.api.get(
|
||||||
"items/section",
|
"items/section",
|
||||||
{"filter": {"chapter": {"chapter_id": {"_eq": in_chapter["id"]}}}},
|
{"filter": {"chapter": {"chapter_id": {"_eq": in_chapter["id"]}}}},
|
||||||
)
|
)
|
||||||
@@ -53,17 +55,16 @@ class Download:
|
|||||||
with open("data/book_"+edition+".json", "w", encoding="utf-8") as file:
|
with open("data/book_"+edition+".json", "w", encoding="utf-8") as file:
|
||||||
json.dump(out_chapters, file, ensure_ascii=False, indent=4)
|
json.dump(out_chapters, file, ensure_ascii=False, indent=4)
|
||||||
|
|
||||||
def download_question_metadata(cls) :
|
def download_question_metadata(self) :
|
||||||
print("Downloading question metadata")
|
|
||||||
|
|
||||||
questions = cls.api.get(
|
questions = self.api.get(
|
||||||
"items/questions",
|
"items/questions",
|
||||||
params={"filter": {"class_3": {"_in": [1,2,3]}}, "sort": "position"}
|
params={"filter": {"class_3": {"_in": [1,2,3]}}, "sort": "position"}
|
||||||
)
|
)
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
for question in questions:
|
for question in tqdm(questions, desc="Downloading question metadata"):
|
||||||
result[question["id"]] = {
|
result[question["id"]] = {
|
||||||
"number": question["number"],
|
"number": question["number"],
|
||||||
"picture_question": question["picture_question"] if question["picture_question"] is not None else "",
|
"picture_question": question["picture_question"] if question["picture_question"] is not None else "",
|
||||||
@@ -79,16 +80,65 @@ class Download:
|
|||||||
json.dump(result, f, indent=4)
|
json.dump(result, f, indent=4)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def download_photos(cls) :
|
def download_photos(self) :
|
||||||
print("Downloading photos")
|
|
||||||
|
|
||||||
photos = cls.api.get(
|
photos = self.api.get(
|
||||||
"items/Fotos",
|
"items/Fotos",
|
||||||
params=None
|
params={ "limit": -1 }
|
||||||
)
|
)
|
||||||
|
|
||||||
for photo in tqdm(photos):
|
for photo in tqdm(photos, desc="Downloading photos"):
|
||||||
data = cls.api.get_file("assets/" + photo["photo"])
|
data = self.api.get_file("assets/" + photo["photo"])
|
||||||
file = open("./data/photo/" + str(photo["id"]) + ".jpg", "wb")
|
file = open("./data/photos/" + str(photo["id"]) + ".jpg", "wb")
|
||||||
file.write(data)
|
file.write(data)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
def __symlink_pictures(self) :
|
||||||
|
os.chdir("./data/pictures")
|
||||||
|
if not os.path.islink("foto"):
|
||||||
|
os.system("ln -s ../photos foto")
|
||||||
|
os.chdir("../..")
|
||||||
|
|
||||||
|
def download_pictures(self) :
|
||||||
|
self.__symlink_pictures()
|
||||||
|
|
||||||
|
memory=Memory("./cache", verbose=0)
|
||||||
|
|
||||||
|
@memory.cache
|
||||||
|
def build_picture(picture) :
|
||||||
|
pid = picture["id"]
|
||||||
|
|
||||||
|
if "\\begin{document}" in picture["latex"] or "\\begin{table}" in picture["latex"]: # Workaround, if the picture contains a document class it should be ignored!
|
||||||
|
return
|
||||||
|
|
||||||
|
with open(f"./data/pictures/{pid}.tex", 'w', encoding='utf-8') as file:
|
||||||
|
# Write the auxilary LaTeX file:
|
||||||
|
file.write(f"\\documentclass{{BNetzA-Fragenkatalog}}\\DARCimageOnly{{9cm}}{{{picture['id']}include}}")
|
||||||
|
file.close()
|
||||||
|
# Build the picture:
|
||||||
|
os.system(f"latexmk -lualatex -f -interaction=nonstopmode ./data/pictures/{pid}.tex > /dev/null 2>&1")
|
||||||
|
# Convert the PDF to SVG:
|
||||||
|
os.system(f"pdftocairo -svg data/pictures/{picture['id']}.pdf - > data/pictures/{picture['id']}.svg > /dev/null 2>&1")
|
||||||
|
# Remove all build files:
|
||||||
|
#os.system(f"latexmk -C ./data/pictures/{pid}.tex > /dev/null 2>&1")
|
||||||
|
#os.system(f"rm ./data/pictures/{pid}.tex > /dev/null 2>&1")
|
||||||
|
|
||||||
|
pictures = self.api.get(
|
||||||
|
"items/pictures",
|
||||||
|
params={ "limit": -1 }
|
||||||
|
)
|
||||||
|
|
||||||
|
for picture in tqdm(pictures, desc="Download all pictures"):
|
||||||
|
latex = picture["latex"]
|
||||||
|
with open("./data/pictures/img/" + str(picture["id"]) + "include.tex", "w") as file:
|
||||||
|
file.write(latex)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
#for picture in tqdm(pictures, desc="Build all pictures"):
|
||||||
|
# build_picture(picture)
|
||||||
|
Parallel(n_jobs=multiprocessing.cpu_count())(
|
||||||
|
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -7,6 +7,10 @@ conf = config.Config()
|
|||||||
api = directus.DirectusAPI(conf)
|
api = directus.DirectusAPI(conf)
|
||||||
dl = download.Download(api)
|
dl = download.Download(api)
|
||||||
|
|
||||||
|
# Download the text files:
|
||||||
dl.download_edition("N")
|
dl.download_edition("N")
|
||||||
|
|
||||||
|
# Download additional files:
|
||||||
dl.download_question_metadata()
|
dl.download_question_metadata()
|
||||||
dl.download_photos()
|
dl.download_photos()
|
||||||
|
dl.download_pictures()
|
||||||
|
|||||||
Reference in New Issue
Block a user