aboutsummaryrefslogtreecommitdiff
path: root/coupons_web.py
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2022-09-09 15:17:58 -0400
committerB. Stack <bgstack15@gmail.com>2022-09-09 15:17:58 -0400
commit6d04ba74279e3e8011a60bde17b2ae4e2a752fa0 (patch)
treece9aa02264b7fc98e507f34b1694e7794e8f9095 /coupons_web.py
parentuse XDG_CACHE_HOME correctly (diff)
downloadcoupons-6d04ba74279e3e8011a60bde17b2ae4e2a752fa0.tar.gz
coupons-6d04ba74279e3e8011a60bde17b2ae4e2a752fa0.tar.bz2
coupons-6d04ba74279e3e8011a60bde17b2ae4e2a752fa0.zip
add web app
Diffstat (limited to 'coupons_web.py')
-rw-r--r--coupons_web.py73
1 files changed, 73 insertions, 0 deletions
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/<searchstring>", 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()
bgstack15