Test con la libreria di logging
This commit is contained in:
parent
e5143a86ad
commit
45752d2903
151
main.py
151
main.py
|
@ -24,82 +24,103 @@ from io import BytesIO
|
||||||
import shutil as sh
|
import shutil as sh
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
# Connection to remote library file
|
# Connection to remote library file
|
||||||
import requests
|
import requests
|
||||||
# Parsing of HTTP RFC 1123 datetime format
|
# Parsing of HTTP RFC 1123 datetime format
|
||||||
from email.utils import parsedate_to_datetime
|
from email.utils import parsedate_to_datetime
|
||||||
|
|
||||||
# Our custom library (again no pun intended)
|
# Our custom library (again no pun intended)
|
||||||
import tcparser
|
import tcparser
|
||||||
|
# Global variables and configurations
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
# Start CGI handling for webserver
|
try:
|
||||||
cgitb.enable()
|
|
||||||
inputvars = cgi.FieldStorage()
|
|
||||||
|
|
||||||
print('Content-Type: text/json; charset=utf-8')
|
# Start the logging library (to avoid printing on stdout)
|
||||||
print('Access-Control-Allow-Origin: *')
|
# TODO
|
||||||
print()
|
logging.basicConfig(filename='conf/tpdf.log', encoding='utf-8', level=logging.DEBUG)
|
||||||
### End of HTTP headers: it is now safe to output things
|
|
||||||
##########################################################
|
|
||||||
|
|
||||||
# Create output directory and temporary files if they do not exist
|
# Start CGI handling for webserver
|
||||||
if not os.path.exists(glob.conf['default']['outdir']):
|
cgitb.enable()
|
||||||
os.mkdir(glob.conf['default']['outdir'])
|
inputvars = cgi.FieldStorage()
|
||||||
if not os.path.exists(glob.conf['default']['outdir'] + '/lastupdate.txt'):
|
|
||||||
luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w')
|
logging.debug("Started CGI")
|
||||||
luh.write('0')
|
|
||||||
|
print('Content-Type: text/json; charset=utf-8')
|
||||||
|
print('Access-Control-Allow-Origin: *')
|
||||||
|
print()
|
||||||
|
### End of HTTP headers: it is now safe to output things
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
# Create output directory and temporary files if they do not exist
|
||||||
|
if not os.path.exists(glob.conf['default']['outdir']):
|
||||||
|
logging.debug("missing outdir, creating...")
|
||||||
|
os.mkdir(glob.conf['default']['outdir'])
|
||||||
|
if not os.path.exists(glob.conf['default']['outdir'] + '/lastupdate.txt'):
|
||||||
|
logging.debug("missing lastupdate.txt, creating...")
|
||||||
|
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()
|
luh.close()
|
||||||
|
logging.info("last database update timestamp is %d" % lu)
|
||||||
# Retrieve last database update timestamp
|
|
||||||
luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'r')
|
|
||||||
lu = int(float(luh.read()))
|
|
||||||
luh.close()
|
|
||||||
|
|
||||||
# Fetch last modified from HTTP header
|
|
||||||
path = glob.conf['default']['path']
|
|
||||||
user = glob.conf['default']['user']
|
|
||||||
pswd = glob.conf['default']['pswd']
|
|
||||||
req = requests.head(path, auth=(user, pswd))
|
|
||||||
|
|
||||||
cachefile = glob.conf['default']['outdir'] + "/tellico.xml"
|
|
||||||
|
|
||||||
# If header fetch fails I can't update cache.
|
|
||||||
# Try with current one, if exists
|
|
||||||
if req.status_code == 200 and 'Last-modified' in req.headers:
|
|
||||||
mtime = parsedate_to_datetime(req.headers['Last-modified']).timestamp()
|
|
||||||
|
|
||||||
# If local xml is out-of-date or missing, try download it
|
|
||||||
if int(lu) < int(mtime) or not os.path.isfile(cachefile):
|
|
||||||
# Download Tellico .tc database
|
|
||||||
req = requests.get(path, auth=(user, pswd))
|
|
||||||
if req.status_code == 200 and req.content != None:
|
|
||||||
# Unzip Tellico .tc database and "cache it" locally
|
|
||||||
zipHandler = zipfile.ZipFile(BytesIO(req.content), 'r')
|
|
||||||
zipHandler.extractall(glob.conf['default']['outdir'])
|
|
||||||
zipHandler.close()
|
|
||||||
luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w')
|
|
||||||
luh.write(int(mtime))
|
|
||||||
luh.close()
|
|
||||||
|
|
||||||
# Get a Python-friendly library struct from XML file
|
|
||||||
library = tcparser.getLibrary(cachefile, lu)
|
|
||||||
# TODO: properly handle missing file errors or things like this
|
|
||||||
|
|
||||||
### Get filters to search for books ###
|
|
||||||
try:
|
|
||||||
title = inputvars['title'].value
|
|
||||||
except KeyError:
|
|
||||||
title = ''
|
|
||||||
|
|
||||||
try:
|
|
||||||
author = inputvars['author'].value
|
|
||||||
except KeyError:
|
|
||||||
author = ''
|
|
||||||
|
|
||||||
|
|
||||||
result = tcparser.filter(library, title=title, author=author)
|
# Fetch last modified from HTTP header
|
||||||
|
path = glob.conf['default']['path']
|
||||||
|
user = glob.conf['default']['user']
|
||||||
|
pswd = glob.conf['default']['pswd']
|
||||||
|
req = requests.head(path, auth=(user, pswd))
|
||||||
|
|
||||||
# Wanna get a pretty JSON encoded library to do your nasty things offline at home? ;-)
|
logging.debug("fetched header from %s, returned code %d" % (path, req.status_code))
|
||||||
print(json.dumps(result, indent=4))
|
|
||||||
|
cachefile = glob.conf['default']['outdir'] + "/tellico.xml"
|
||||||
|
|
||||||
|
# If header fetch fails I can't update cache.
|
||||||
|
# Try with current one, if exists
|
||||||
|
if req.status_code == 200 and 'Last-modified' in req.headers:
|
||||||
|
mtime = int(parsedate_to_datetime(req.headers['Last-modified']).timestamp())
|
||||||
|
logging.info("Tellico last modified timestamp is %d" % mtime)
|
||||||
|
|
||||||
|
# If local xml is out-of-date or missing, try download it
|
||||||
|
if int(lu) < int(mtime) or not os.path.isfile(cachefile):
|
||||||
|
logging.info("Out-of-date, updating")
|
||||||
|
# Download Tellico .tc database
|
||||||
|
req = requests.get(path, auth=(user, pswd))
|
||||||
|
if req.status_code == 200 and req.content != None:
|
||||||
|
# Unzip Tellico .tc database and "cache it" locally
|
||||||
|
zipHandler = zipfile.ZipFile(BytesIO(req.content), 'r')
|
||||||
|
zipHandler.extractall(glob.conf['default']['outdir'])
|
||||||
|
zipHandler.close()
|
||||||
|
luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w')
|
||||||
|
luh.write(mtime)
|
||||||
|
luh.close()
|
||||||
|
else:
|
||||||
|
logging.error("Update failed")
|
||||||
|
|
||||||
|
# Get a Python-friendly library struct from XML file
|
||||||
|
library = tcparser.getLibrary(cachefile, lu)
|
||||||
|
# TODO: properly handle missing file errors or things like this
|
||||||
|
|
||||||
|
### Get filters to search for books ###
|
||||||
|
try:
|
||||||
|
title = inputvars['title'].value
|
||||||
|
except KeyError:
|
||||||
|
title = ''
|
||||||
|
|
||||||
|
try:
|
||||||
|
author = inputvars['author'].value
|
||||||
|
except KeyError:
|
||||||
|
author = ''
|
||||||
|
|
||||||
|
|
||||||
|
result = tcparser.filter(library, title=title, author=author)
|
||||||
|
|
||||||
|
# Wanna get a pretty JSON encoded library to do your nasty things offline at home? ;-)
|
||||||
|
print(json.dumps(result, indent=4))
|
||||||
|
|
||||||
|
except:
|
||||||
|
logging.fatal("Unexpected error:", sys.exc_info()[0])
|
||||||
|
|
Loading…
Reference in New Issue