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-13 11:18:51 -04:00
|
|
|
@app.route("/r")
|
2015-08-08 19:26:24 -04:00
|
|
|
def updateData():
|
2015-08-13 11:18:51 -04:00
|
|
|
global data
|
2015-08-08 19:26:24 -04:00
|
|
|
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 11:21:53 -04:00
|
|
|
for dName in fields.keys():
|
|
|
|
datum = book.find(ns + dName)
|
|
|
|
if datum != None:
|
|
|
|
t = int(fields[dName]["type"])
|
2015-08-13 10:29:18 -04:00
|
|
|
#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
|
2015-08-13 11:21:53 -04:00
|
|
|
if (int(fields[dName]["flags"]) & 0x01 & 0xff) != 0:
|
|
|
|
bookout[dName] = []
|
|
|
|
for sub in datum:
|
|
|
|
bookout[dName].append(sub.text)
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-13 11:21:53 -04:00
|
|
|
bookout[dName] = datum.text
|
2015-08-08 19:24:14 -04:00
|
|
|
|
2015-08-13 10:29:18 -04:00
|
|
|
#date
|
|
|
|
elif t == 12:
|
2015-08-13 11:21:53 -04:00
|
|
|
bookout[dName] = ""
|
|
|
|
if datum.find(ns + "year") != None:
|
|
|
|
bookout[dName] += datum.find(ns + "year").text
|
|
|
|
if datum.find(ns + "month") != None:
|
|
|
|
bookout[dName] += "-" + datum.find(ns + "month").text
|
|
|
|
if datum.find(ns + "day") != None:
|
|
|
|
bookout[dName] += "-" + datum.find(ns + "day").text
|
2015-08-13 10:29:18 -04:00
|
|
|
|
|
|
|
elif t == 6: #Number
|
2015-08-13 11:21:53 -04:00
|
|
|
bookout[dName] = datum.text
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-13 11:21:53 -04:00
|
|
|
print("Did not parse:" + dName + ", type:" + str(t))
|
|
|
|
bookout[dName] = ""
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-13 11:21:53 -04:00
|
|
|
bookout[dName] = ""
|
2015-08-07 11:55:21 -04:00
|
|
|
out.append(bookout.copy())
|
2015-08-13 11:18:51 -04:00
|
|
|
data = out.copy()
|
|
|
|
return "success"
|
2015-08-08 19:26:24 -04:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def main():
|
2015-08-13 11:18:51 -04:00
|
|
|
if data == []:
|
|
|
|
updateData()
|
2015-08-08 19:26:24 -04:00
|
|
|
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):
|
2015-08-13 11:18:51 -04:00
|
|
|
if 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')
|