From e5143a86ad615c66c7531924796b19831365f22c Mon Sep 17 00:00:00 2001 From: giuliof Date: Sat, 4 Sep 2021 00:19:06 +0200 Subject: [PATCH] Tentativo di implementazione fetching remoto .tc. Da testare MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Richiesta HTTP (o via webdav, รจ equivalente). - Uso del Last-modified nell'header HTTP per il rinnovo della cache. --- conf/conf.ini | 4 +++- main.py | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/conf/conf.ini b/conf/conf.ini index 67763ad..ca856c4 100644 --- a/conf/conf.ini +++ b/conf/conf.ini @@ -9,7 +9,9 @@ [default] # Path to Tellico .tc database to use - path = /path/to/some/tellico.tc + path = http://path/to/some/tellico.tc + user = user + pswd = pswd # Path to output directory for images and temporary data # Must be accessible by webserver diff --git a/main.py b/main.py index 9659f4d..5cbbc0a 100755 --- a/main.py +++ b/main.py @@ -20,9 +20,14 @@ import json import sys import cgitb, cgi import zipfile +from io import BytesIO import shutil as sh import os import time +# Connection to remote library file +import requests +# Parsing of HTTP RFC 1123 datetime format +from email.utils import parsedate_to_datetime # Our custom library (again no pun intended) import tcparser @@ -52,19 +57,35 @@ luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'r') lu = int(float(luh.read())) luh.close() -mtime = os.path.getmtime(glob.conf['default']['path']) +# 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)) -if int(lu) < int(mtime): - # Unzip Tellico .tc database - zipHandler = zipfile.ZipFile(glob.conf['default']['path'], 'r') - zipHandler.extractall(glob.conf['default']['outdir']) - zipHandler.close() - luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w') - luh.write(str(time.time())) - luh.close() +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(glob.conf['default']['outdir'] + "/tellico.xml", lu) +library = tcparser.getLibrary(cachefile, lu) +# TODO: properly handle missing file errors or things like this ### Get filters to search for books ### try: