From 8752f2a431847ebc44a0ecbbddfdbce98fabeb62 Mon Sep 17 00:00:00 2001 From: Sunpy Date: Tue, 29 May 2018 13:28:42 +0200 Subject: [PATCH] Added getUpdate endpoint --- handlers/getupdate.py | 23 ++++++++++++++++++++--- objects/glob.py | 1 + web.py | 8 ++++---- 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 objects/glob.py diff --git a/handlers/getupdate.py b/handlers/getupdate.py index 330d2af..5520e24 100644 --- a/handlers/getupdate.py +++ b/handlers/getupdate.py @@ -1,3 +1,5 @@ +from objects import glob + allowed_args = ["file_hash", "file_version", "timestamp"] def handle(request): @@ -11,7 +13,22 @@ def handle(request): break output = { - "search_method": method_name + "method": method_name, + "response": callback(method_name, request.args.get(method_name)) } - output["endpoint"] = request.endpoint - return output \ No newline at end of file + return output + +def callback(method, data): + cur = glob.sql.cursor() + + if method is "file_hash": + cur.execute("SELECT * FROM updates WHERE file_hash = '{}'".format( + data + )) + else: + cur.execute("SELECT a.* FROM updates a INNER JOIN ( SELECT MAX(file_version) file_version, filename FROM updates WHERE {} < {} GROUP BY filename) b ON a.file_version = b.file_version;".format( + method, + data + )) + + return cur.fetchall() \ No newline at end of file diff --git a/objects/glob.py b/objects/glob.py new file mode 100644 index 0000000..1d87e23 --- /dev/null +++ b/objects/glob.py @@ -0,0 +1 @@ +sql = None \ No newline at end of file diff --git a/web.py b/web.py index f5041a3..1da4abe 100644 --- a/web.py +++ b/web.py @@ -3,12 +3,16 @@ import MySQLdb import MySQLdb.cursors from flask import Flask, make_response, request, render_template, jsonify from handlers import update +from objects import glob app = Flask(__name__) with open("config.json", "r") as f: config = json.load(f) +# Setup sql +glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) + @app.route("/") @app.route("/home") def home_index(): @@ -26,9 +30,5 @@ def api_index(): def api_update(): return update.handle(request) -@app.route("/api") -def api_index(): - return render_template("api.html") - if __name__ == "__main__": app.run(**config["web"]) \ No newline at end of file