diff --git a/export_db.sh b/export_db.sh index d9d35c4..d7fa7c5 100755 --- a/export_db.sh +++ b/export_db.sh @@ -1,4 +1,4 @@ #!/bin/bash rm html/database.db -sqlite3 database.db '.dump pbs_item pbs_mp pbs_mp_brand_name pbs_mpp pbs_item_restriction pbs_restriction pbs_restriction_criteria pbs_criteria pbs_criteria_parameter' | sqlite3 html/database.db +sqlite3 database.db '.dump meta pbs_item pbs_mp pbs_mp_brand_name pbs_mpp pbs_item_restriction pbs_restriction pbs_restriction_criteria pbs_criteria pbs_criteria_parameter' | sqlite3 html/database.db diff --git a/full_build_db.sh b/full_build_db.sh index 988bf9c..47f5d9b 100755 --- a/full_build_db.sh +++ b/full_build_db.sh @@ -1,6 +1,6 @@ #!/bin/bash -./import_pbs_xml.py -./render_pbs_criteria.py -./find_pbs_brand_names.py +./import_pbs_xml.py || exit 1 +./render_pbs_criteria.py || exit 1 +./find_pbs_brand_names.py || exit 1 ./export_db.sh diff --git a/html/index.html b/html/index.html index 6b731ba..93f2a38 100644 --- a/html/index.html +++ b/html/index.html @@ -69,10 +69,10 @@ + - @@ -92,6 +92,10 @@ const [SQL, buf] = await Promise.all([sqlPromise, dataPromise]) db = new SQL.Database(new Uint8Array(buf)); + // Display date + const pbs_date_bits = execAsScalars(db.prepare('SELECT value FROM meta WHERE key = "pbs_date"'))[0].split('-'); + document.getElementById('pbs-date').innerText = {'01': 'January', '02': 'February', '03': 'March', '04': 'April', '05': 'May', '06': 'June', '07': 'July', '08': 'August', '09': 'September', '10': 'October', '11': 'November', '12': 'December'}[pbs_date_bits[1]] + ' ' + pbs_date_bits[0]; + // Initialise search bar const mp_preferred_terms = execAsScalars(db.prepare('SELECT preferred_term FROM pbs_mp ORDER BY LOWER(preferred_term)')); let data = mp_preferred_terms.map(mp_preferred_term => ({'label': mp_preferred_term, 'preview': mp_preferred_term, 'value': mp_preferred_term})); diff --git a/import_pbs_xml.py b/import_pbs_xml.py index 2dea9e3..492b794 100755 --- a/import_pbs_xml.py +++ b/import_pbs_xml.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import os import sqlite3 import zipfile from xml.etree import ElementTree as ET @@ -23,6 +24,9 @@ con = sqlite3.connect('database.db') cur = con.cursor() # Init schema +cur.execute('DROP TABLE IF EXISTS meta') +cur.execute('CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT)') + cur.execute('DROP TABLE IF EXISTS pbs_item') cur.execute('CREATE TABLE pbs_item (code TEXT PRIMARY KEY, mpp_code TEXT, maximum_prescribable_units INTEGER, number_repeats INTEGER, benefit_type TEXT, program TEXT)') @@ -51,14 +55,20 @@ cur.execute('DROP TABLE IF EXISTS pbs_criteria_parameter') cur.execute('CREATE TABLE pbs_criteria_parameter (id INTEGER PRIMARY KEY AUTOINCREMENT, criteria_code INTEGER, text TEXT)') # Parse XML -with zipfile.ZipFile('data/2023-01-01-xml-V3.zip', 'r') as zipf: - with zipf.open('sch-2023-01-01-r1.xml', 'r') as f: +pbs_zip_file = sorted([f for f in os.listdir('data') if f.endswith('-xml-V3.zip')])[-1] + +with zipfile.ZipFile('data/' + pbs_zip_file, 'r') as zipf: + pbs_xml_file = next(f for f in zipf.namelist() if f.endswith('.xml')) + with zipf.open(pbs_xml_file, 'r') as f: tree = ET.parse(f) print('Parsed XML') root = tree.getroot() -ns = {'pbs': 'http://schema.pbs.gov.au/', 'xlink': 'http://www.w3.org/1999/xlink', 'xml': 'http://www.w3.org/XML/1998/namespace', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'dbk': 'http://docbook.org/ns/docbook'} +ns = {'pbs': 'http://schema.pbs.gov.au/', 'xlink': 'http://www.w3.org/1999/xlink', 'xml': 'http://www.w3.org/XML/1998/namespace', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'dbk': 'http://docbook.org/ns/docbook', 'dct': 'http://purl.org/dc/terms/'} + +# Write meta +cur.execute('INSERT INTO meta (key, value) VALUES (?, ?)', ('pbs_date', root.find('pbs:info', ns).find('dct:valid', ns).text)) # ----------------------------------------- # Parse items from each desired PBS program