osu-wayback/api/v1/getUpdate.py
2019-03-11 14:41:55 +01:00

65 lines
1.6 KiB
Python

import traceback
import json
import tornado.gen
import tornado.web
from helpers import validatorHelper
from web import asyncTornado
from constants import argumentTypes
from objects import glob
class handler(asyncTornado.asyncRequestHandler):
ARGS = {
("file_hash", "file_version", "timestamp"): argumentTypes.one_required
}
ARGS_VALIDATION = {
validatorHelper.HEX: ["file_hash"],
validatorHelper.INT: ["file_version", "timestamp"]
}
SQL_STRUCT = {
"main": "SELECT max(search.file_version) as file_version, search.* FROM updates search %s GROUP BY filename;",
"file_hash": "WHERE %s <= %s",
"file_version": "WHERE %s <= %s",
"timestamp": "INNER JOIN ( SELECT file_version, file_version FROM updates WHERE %s = '%s' ) find ON search.file_version <= find.file_version"
}
@tornado.web.asynchronous
@tornado.gen.engine
def asyncGet(self):
status_code = 400
data = {}
try:
args_filter = asyncTornado.check_arguments(self)
if not args_filter:
raise Exception("Missing required arguments")
method = args_filter[0]
method_value = self.get_argument(method)
conn = glob.new_sql()
cur = conn.cursor()
sql = self.SQL_STRUCT["main"] % self.SQL_STRUCT[method]
cur.execute(sql % (method, method_value))
data = cur.fetchall()
cur.close()
conn.close()
status_code = 200
except Exception as e:
status_code = 400
data["status"] = status_code
data["message"] = str(e)
print("Err: %s" % e)
traceback.print_exc()
finally:
self.write( json.dumps(data) )
self.set_header("Content-Type", "application/json")
self.set_status(status_code)