osu-wayback/api/v1/getUpdate.py

34 lines
1.2 KiB
Python
Raw Normal View History

2018-05-29 13:28:42 +02:00
from objects import glob
2018-05-24 12:22:16 +02:00
allowed_args = ["file_hash", "file_version", "timestamp"]
def handle(request):
if len([x for x in request.args if x in allowed_args]) == 0:
return {
"error": "Missing valid args",
"allowed": allowed_args
}
2018-05-24 12:22:16 +02:00
for i in range(len(allowed_args)): # Gets the first valid argument and sets it as the method handler
method = request.args.get(allowed_args[i])
method_name = allowed_args[i]
if method is not None:
break
2018-05-31 11:23:04 +02:00
return callback(method_name, request.args.get(method_name))
2018-05-29 13:28:42 +02:00
def callback(method, data):
cur = glob.sql.cursor()
2018-05-29 13:41:21 +02:00
query = "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"
if method is "timestamp":
query += " ORDER BY a.timestamp DESC"
elif method is "file_hash":
query = "SELECT * FROM updates WHERE {} = '{}'"
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
))
2018-05-29 13:28:42 +02:00
return cur.fetchall()