From 6d04ba74279e3e8011a60bde17b2ae4e2a752fa0 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Fri, 9 Sep 2022 15:17:58 -0400 Subject: add web app --- coupons_web.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 coupons_web.py (limited to 'coupons_web.py') diff --git a/coupons_web.py b/coupons_web.py new file mode 100644 index 0000000..16e89eb --- /dev/null +++ b/coupons_web.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# Startdate: 2022-09-08 +# Improve: +# confirm date logic works. + +from flask import Flask, Response, request, url_for, render_template +import json, sys, os +# Load coupons from same directory as coupons_web.py +sys.path.append(os.path.dirname(os.path.abspath(__file__))) +import coupons +from urllib import parse + +app = Flask(__name__) +config = coupons.Config() +# satisfies mod_wsgi: +application = app +@app.route("/") +def root(): + set_cache_dir_from_apache_environ(request, config) + return render_template("index.html") + +def set_cache_dir_from_apache_environ(_request, _config): + """ + Given the request and config objects, derive the SetEnv XDG_CACHE_HOME for coupons web app. + """ + try: + cache_dir = _request.environ.get("XDG_CACHE_HOME") + if cache_dir is not None and cache_dir != _config.cache_dir: + print(f"DEBUG(web set_cache_dir): setting cache dir to {cache_dir}") + _config.cache_dir = cache_dir + except: + pass + + +@app.route("/search/", methods=["GET","POST"]) +@app.route("/search/", methods=["GET"]) +def search(searchstring = ""): + """ + Pass searchstring to fetch_and_search. Returns json unless html is requested. + Accepts parameters nocache=1 and date=YYYY-MM-DD + """ + if request.method in ["POST"]: + searchstring = request.get_data().decode("utf-8") + set_cache_dir_from_apache_environ(request, config) + print(f"DEBUG(web search): {request}") + #print(f"DEBUG(web search): {request.method} searchstring={searchstring}") + force = False + nocache = request.args.get('nocache') + date = request.args.get('date') # in YYYY-MM-DD form + if nocache in ["1",1,"yes","YES","true","TRUE",True,"True","on"]: + print(f"DEBUG(web search): Caching is disabled for this request.") + force = True + results = [] + for store in coupons.store_urls: + a = coupons.fetch_and_search(store = store, force = force, searchstring = searchstring, date = date, config = config) + if len(a) > 2: # an empty a is "{}" which is a string?! + results.append({store: json.loads(a)}) + accept = request.headers.get("Accept") + from_js = True if request.headers.get("From") else False + #print(f"DEBUG(web search): Accept only type {accept}") + #print(f"DEBUG(web search): Results: {results}") + #print(f"DEBUG(web search): From_js is {from_js}") + if accept in ["text/html", "application/html"] or "text/html" in accept: + #print(f"DEBUG(web search): Must convert the json to html...") + savedlink = url_for("search") + searchstring # because url_for handles searchstring= weird + #print(f"DEBUG(web search): Preparing saved search link: {savedlink}") + return render_template("index.html", results = results, from_js = from_js, savedlink = savedlink) + else: + # json is default + return json.dumps(results),200 + +if __name__ == "__main__": + app.run() -- cgit