Rewrote api handle

This commit is contained in:
2019-03-07 14:04:54 +01:00
parent 7149885c9d
commit e491530689
8 changed files with 65 additions and 45 deletions

6
api/v1/__init__.py Normal file
View File

@@ -0,0 +1,6 @@
from os import listdir
handlers = {}
for d in [d.rstrip(".py") for d in listdir(__name__.replace(".", "/")) if d not in ["__init__.py", "__pycache__"]]:
handlers[d] = __import__("%s.%s" % (__name__, d), fromlist=[""])

36
api/v1/getFile.py Normal file
View File

@@ -0,0 +1,36 @@
from objects import glob
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
}
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
return callback(method_name, request.args.get(method_name))
def callback(method, data):
cur = glob.sql.cursor()
if method is "timestamp":
cur.execute("SELECT * FROM updates WHERE timestamp <= '{}' ORDER BY timestamp DESC LIMIT 1".format(
data
))
else:
query = "SELECT * FROM updates WHERE {} = {} LIMIT 1"
if method is "file_hash":
query = "SELECT * FROM updates WHERE {} = '{}' LIMIT 1"
cur.execute(query.format(
method,
data
))
return cur.fetchone()

31
api/v1/getList.py Normal file
View File

@@ -0,0 +1,31 @@
from objects import glob
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
}
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
return callback(method_name)
def callback(method):
cur = glob.sql.cursor()
cur.execute("SELECT {} FROM updates WHERE filename = 'osu!.exe' ORDER BY file_version".format(
method
))
data = []
for row in cur.fetchall():
data.append(row[method])
return data

34
api/v1/getUpdate.py Normal file
View File

@@ -0,0 +1,34 @@
from objects import glob
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
}
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
return callback(method_name, request.args.get(method_name))
def callback(method, data):
cur = glob.sql.cursor()
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
))
return cur.fetchall()