def cache(gnd): """ http://d-nb.info/gnd/118514768/about/rdf """ with dbopen(DB) as cursor: query = cursor.execute("""SELECT content FROM gnd WHERE id = ?""", (gnd,)) result = query.fetchone() if not result: # download and store r = requests.get("http://d-nb.info/gnd/{gnd}/about/rdf".format(gnd=gnd)) if r.status_code == 200: cursor.execute("""INSERT INTO gnd (id, content) VALUES (?, ?)""", (gnd, r.text)) else: # pass on the d-nb.info status code abort(r.status_code) query = cursor.execute("""SELECT content FROM gnd WHERE id = ?""", (gnd,)) result = query.fetchone() if not result: abort(404) wrapped = wrap(result[0], rewrite=request.args.get('rewrite', True), header=True) return Response(response=wrapped, status=200, headers=None, mimetype='text/xml', content_type='text/xml; charset=utf-8', direct_passthrough=False)
def create_cache(): with dbopen(DB) as cursor: cursor.execute("""CREATE TABLE IF NOT EXISTS gnd (id text PRIMARY KEY, content blob, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)""") cursor.execute("""CREATE INDEX IF NOT EXISTS idx_gnd_id ON gnd (id)""") return jsonify(cache="ok")
def drop_cache(): with dbopen(DB) as cursor: cursor.execute("""DROP TABLE IF EXISTS gnd """) cursor.execute("""DROP INDEX IF EXISTS idx_gnd_id""") return jsonify(cache="dropped")