osu-wayback/api/v1/getFile.py

61 lines
1.3 KiB
Python
Raw Normal View History

2019-03-11 07:06:31 +01:00
import json
2019-03-08 11:12:47 +01:00
import tornado.gen
import tornado.web
2019-03-11 07:06:31 +01:00
from web import asyncTornado
from constants import argumentTypes
2018-05-29 13:38:35 +02:00
from objects import glob
2019-03-11 07:06:31 +01:00
ARGS = {
("file_hash", "file_version", "timestamp"): argumentTypes.one_required
}
SQL_STRUCT = {
"main": "SELECT * FROM updates WHERE %s LIMIT 1",
"file_hash": "%s = '%s'",
"file_version": "%s = %s",
"timestamp": "timestamp <= '%s' ORDER BY timestamp DESC"
}
class handler(asyncTornado.asyncRequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def asyncGet(self):
status_code = 400
data = {}
try:
args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS)
if False in args_filter:
raise Exception("Missing required arguments")
method = args_filter[0]
2019-03-11 07:51:05 +01:00
method_value = self.get_argument(method)
2019-03-11 07:06:31 +01:00
2019-03-11 07:51:05 +01:00
conn = glob.new_sql()
cur = conn.cursor()
2019-03-11 07:06:31 +01:00
2019-03-11 07:51:05 +01:00
sql = SQL_STRUCT["main"] % SQL_STRUCT[method]
2019-03-11 07:06:31 +01:00
if method == "timestamp":
sql = sql % method_value
else:
sql = sql % (method, method_value)
2018-05-29 13:38:35 +02:00
2019-03-11 07:06:31 +01:00
cur.execute(sql)
data = cur.fetchone()
2018-05-29 13:38:35 +02:00
2019-03-11 07:27:54 +01:00
cur.close()
2019-03-11 07:51:05 +01:00
conn.close()
2019-03-11 07:27:54 +01:00
2019-03-11 07:06:31 +01:00
status_code = 200
except Exception as e:
status_code = 400
data["status"] = status_code
2019-03-11 07:28:53 +01:00
data["message"] = str(e)
2019-03-11 07:51:05 +01:00
print("Err: %s" % e)
2019-03-11 07:06:31 +01:00
finally:
self.write( json.dumps(data) )
self.set_header("Content-Type", "application/json")
self.set_status(status_code)