tellicoViewer/test.py

56 lines
1.6 KiB
Python

#!/usr/bin/env python3
from xml.etree import ElementTree
import zipfile
from flask import Flask, render_template
app = Flask(__name__)
data = []
attributes= ["title",
"author",
"isbn"]
names = {}
def updateData():
out = []
f = zipfile.ZipFile("library.tc")
tree = ElementTree.parse(f.open("tellico.xml"))
collection = tree.getroot()[0]
ns = "{http://periapsis.org/tellico/}"
for field in collection.find(ns + "fields"):
print(field.attrib)
if "title" in field.attrib:
names[field.attrib["name"]] = field.attrib["title"]
for book in collection.iter(ns + "entry"):
bookout = {}
bookout["cover"] = book.find(ns + "cover").text if book.find(ns + "cover") != None else ""
bookout["id"] = int(book.find(ns + "id").text)
bookout["title"] = book.find(ns + "title").text if (book.find(ns + "title") != None) else ""
bookout["isbn"] = book.find(ns + "isbn").text if (book.find(ns + "isbn") != None) else ""
authors = []
if book.find(ns + "authors") != None:
for a in book.find(ns + "authors"):
authors.append(a.text)
bookout["author"] = "; ".join(authors)
out.append(bookout.copy())
return out.copy()
@app.route("/")
def main():
data = updateData();
return render_template("main.html", books = data, attributes = attributes)
@app.route("/book/<int:id>")
def book(id):
data = updateData();
book = next((item for item in data if item["id"] == id), {})
return render_template("book.html", book = book, names = names)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')