Tentativo di implementazione fetching remoto .tc. Da testare

- Richiesta HTTP (o via webdav, è equivalente).
- Uso del Last-modified nell'header HTTP per il rinnovo della cache.
This commit is contained in:
giuliof 2021-09-04 00:19:06 +02:00
parent 49f89a606d
commit e5143a86ad
2 changed files with 34 additions and 11 deletions

View File

@ -9,7 +9,9 @@
[default] [default]
# Path to Tellico .tc database to use # 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 # Path to output directory for images and temporary data
# Must be accessible by webserver # Must be accessible by webserver

41
main.py
View File

@ -20,9 +20,14 @@ import json
import sys import sys
import cgitb, cgi import cgitb, cgi
import zipfile import zipfile
from io import BytesIO
import shutil as sh import shutil as sh
import os import os
import time 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) # Our custom library (again no pun intended)
import tcparser import tcparser
@ -52,19 +57,35 @@ luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'r')
lu = int(float(luh.read())) lu = int(float(luh.read()))
luh.close() 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): cachefile = glob.conf['default']['outdir'] + "/tellico.xml"
# Unzip Tellico .tc database
zipHandler = zipfile.ZipFile(glob.conf['default']['path'], 'r') # If header fetch fails I can't update cache.
zipHandler.extractall(glob.conf['default']['outdir']) # Try with current one, if exists
zipHandler.close() if req.status_code == 200 and 'Last-modified' in req.headers:
luh = open(glob.conf['default']['outdir'] + '/lastupdate.txt', 'w') mtime = parsedate_to_datetime(req.headers['Last-modified']).timestamp()
luh.write(str(time.time()))
luh.close() # 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 # 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 ### ### Get filters to search for books ###
try: try: