diff --git a/conf/conf.ini b/conf/conf.ini index 905fdeb..67763ad 100644 --- a/conf/conf.ini +++ b/conf/conf.ini @@ -7,5 +7,10 @@ ################################################################################ [default] -# Path to Tellico .tc database to use -path = /some/path/to/tellico.tc + + # Path to Tellico .tc database to use + path = /path/to/some/tellico.tc + + # Path to output directory for images and temporary data + # Must be accessible by webserver + outdir = output diff --git a/glob.py b/glob.py new file mode 100644 index 0000000..52232ca --- /dev/null +++ b/glob.py @@ -0,0 +1,5 @@ +import configparser + +# Read configuration files (latest files in list override previous settings) +conf = configparser.ConfigParser() +conf.read(['conf/conf.ini', 'conf/conf.custom.ini']) diff --git a/main.py b/main.py index 64c09b8..946d322 100755 --- a/main.py +++ b/main.py @@ -25,11 +25,12 @@ import zipfile import shutil as sh import os import time -import configparser # Our custom library (again no pun intended) import tcparser +import glob + # Start CGI handling for webserver cgitb.enable() inputvars = cgi.FieldStorage() @@ -49,29 +50,33 @@ print() ### End of HTTP headers: it is now safe to output things ########################################################## -# Read configuration files (latest files in list override previous settings) -conf = configparser.ConfigParser() -conf.read(['conf/conf.ini', 'conf/conf.custom.ini']) -luh = open('./output/lastupdate.txt', 'r') +# Create output directory and temporary files if they do not exist +if not os.path.exists(glob.conf['default']['outdir']): + os.mkdir(glob.conf['default']['outdir']) +if not os.path.exists(glob.conf['default']['outdir'] + '/lastupdate.txt'): + luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w') + luh.write('0') + luh.close() + +# Retrieve last database update timestamp +luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'r') lu = int(float(luh.read())) luh.close() -mtime = os.path.getmtime(conf['default']['path']) - -outdir = './output' +mtime = os.path.getmtime(glob.conf['default']['path']) if int(lu) < int(mtime): # Unzip Tellico .tc database - zipHandler = zipfile.ZipFile(conf['default']['path'], 'r') - zipHandler.extractall(outdir) + zipHandler = zipfile.ZipFile(glob.conf['default']['path'], 'r') + zipHandler.extractall(glob.conf['default']['outdir']) zipHandler.close() - luh = open('./output/lastupdate.txt', 'w') + luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w') luh.write(str(time.time())) luh.close() # Get a Python-friendly library struct from XML file -library = tcparser.getLibrary(outdir + "/tellico.xml", lu) +library = tcparser.getLibrary(glob.conf['default']['outdir'] + "/tellico.xml", lu) ### Get filters to search for books ### try: diff --git a/tcparser.py b/tcparser.py index 09ac145..d77c89e 100644 --- a/tcparser.py +++ b/tcparser.py @@ -3,6 +3,8 @@ import json import datetime import sys +import glob + # Parse Tellico's XML and get a library struct, # a stripped version of our library in a Python-friendly format def getLibrary(path, lastUpdate): @@ -112,7 +114,7 @@ def getHTML(library): cover = ET.Element('td') if i.get('cover'): - img = ET.Element('img', attrib={'alt': 'Book "' + i.get('title') + '" cover', 'src': 'output/images/' + i.get('cover')}) + img = ET.Element('img', attrib={'alt': 'Book "' + i.get('title') + '" cover', 'src': glob.conf['default']['outdir'] + '/images/' + i.get('cover')}) cover.append(img) tr.append(cover)