Rewrote api handle
This commit is contained in:
7
api/__init__.py
Normal file
7
api/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from . import missing
|
||||
from os import listdir
|
||||
|
||||
versions = {}
|
||||
|
||||
for d in [d for d in listdir(__name__) if d not in ["__init__.py", "__pycache__", "missing.py"]]:
|
||||
versions[d] = __import__("%s.%s" % (__name__, d), fromlist=[""])
|
||||
14
api/missing.py
Normal file
14
api/missing.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from objects import glob
|
||||
|
||||
allowed_args = ["file_hash", "file_version", "timestamp"]
|
||||
|
||||
def handle(request):
|
||||
print(request)
|
||||
|
||||
return callback()
|
||||
|
||||
def callback():
|
||||
return {
|
||||
"error": 404,
|
||||
"message": "Missing api endpoint"
|
||||
}
|
||||
6
api/v1/__init__.py
Normal file
6
api/v1/__init__.py
Normal 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
36
api/v1/getFile.py
Normal 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
31
api/v1/getList.py
Normal 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
34
api/v1/getUpdate.py
Normal 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()
|
||||
Reference in New Issue
Block a user