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:
parent
49f89a606d
commit
e5143a86ad
|
@ -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
|
||||
|
|
41
main.py
41
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:
|
||||
|
|
Loading…
Reference in New Issue