tellicoViewer/test.py

48 lines
1.4 KiB
Python
Raw Normal View History

2015-08-07 11:55:21 -04:00
#!/usr/bin/env python3
from xml.etree import ElementTree
import zipfile
from flask import Flask, render_template
app = Flask(__name__)
2015-08-08 19:26:24 -04:00
data = []
attributes= ["title",
"authors",
"isbn"]
def updateData():
out = []
2015-08-07 11:55:21 -04:00
f = zipfile.ZipFile("library.tc")
tree = ElementTree.parse(f.open("tellico.xml"))
collection = tree.getroot()[0]
ns = "{http://periapsis.org/tellico/}"
for book in collection.iter(ns + "entry"):
bookout = {}
2015-08-08 19:31:48 -04:00
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["authors"] = "; ".join(authors)
2015-08-07 11:55:21 -04:00
out.append(bookout.copy())
2015-08-08 19:26:24 -04:00
return out.copy()
@app.route("/")
def main():
data = updateData();
return render_template("main.html", books = data, attributes = attributes)
2015-08-08 19:31:48 -04:00
@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)
2015-08-07 11:55:21 -04:00
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')