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",
|
2015-08-13 10:29:18 -04:00
|
|
|
"authors",
|
2015-08-08 19:26:24 -04:00
|
|
|
"isbn"]
|
|
|
|
|
2015-08-13 10:29:18 -04:00
|
|
|
fields = {}
|
2015-08-09 20:13:44 -04:00
|
|
|
|
2015-08-08 19:26:24 -04:00
|
|
|
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/}"
|
|
|
|
|
2015-08-09 20:13:44 -04:00
|
|
|
for field in collection.find(ns + "fields"):
|
2015-08-13 10:29:18 -04:00
|
|
|
#check if multiple entries are allowed
|
|
|
|
if (int(field.attrib["flags"]) & 0x01 & 0xff) != 0:
|
|
|
|
#change name to match
|
|
|
|
name = field.attrib["name"] + "s"
|
|
|
|
else:
|
|
|
|
name = field.attrib["name"]
|
|
|
|
fields[name] = field.attrib
|
2015-08-09 20:13:44 -04:00
|
|
|
|
2015-08-07 11:55:21 -04:00
|
|
|
for book in collection.iter(ns + "entry"):
|
|
|
|
bookout = {}
|
2015-08-13 10:29:18 -04:00
|
|
|
for datum in fields.keys():
|
|
|
|
if book.find(ns + datum) != None:
|
|
|
|
t = int(fields[datum]["type"])
|
|
|
|
#simple text, paragraph, multiple choice, image
|
|
|
|
if t == 1 or t == 2 or t == 3 or t == 10:
|
|
|
|
#check if multiple entries are allowed
|
|
|
|
#if so, put entries in a list
|
|
|
|
if (int(fields[datum]["flags"]) & 0x01 & 0xff) != 0:
|
|
|
|
bookout[datum] = []
|
|
|
|
for sub in book.find(ns + datum):
|
|
|
|
bookout[datum].append(sub.text)
|
|
|
|
else:
|
|
|
|
bookout[datum] = book.find(ns + datum).text
|
2015-08-08 19:24:14 -04:00
|
|
|
|
2015-08-13 10:29:18 -04:00
|
|
|
#date
|
|
|
|
elif t == 12:
|
|
|
|
bookout[datum] = ""
|
|
|
|
if book.find(ns + "year") != None:
|
|
|
|
bookout[datum] += book.find(ns + "year").text
|
|
|
|
if book.find(ns + "month") != None:
|
|
|
|
bookout[datum] += "-" + book.find(ns + "month").text
|
|
|
|
if book.find(ns + "day") != None:
|
|
|
|
bookout[datum] += "-" + book.find(ns + "day").text
|
|
|
|
|
|
|
|
elif t == 6: #Number
|
|
|
|
bookout[datum] = book.find(ns + datum).text
|
|
|
|
else:
|
|
|
|
print(str(t) + ": " + datum)
|
|
|
|
bookout[datum] = ""
|
|
|
|
else:
|
|
|
|
bookout[datum] = ""
|
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();
|
2015-08-13 10:29:18 -04:00
|
|
|
book = next((item for item in data if int(item["id"]) == id), {})
|
|
|
|
return render_template("book.html", book = book, fields = fields)
|
2015-08-07 11:55:21 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True, host='0.0.0.0')
|