2019-12-08 22:43:49 +01:00
|
|
|
import copy
|
|
|
|
import json
|
|
|
|
import traceback
|
2021-09-07 19:56:31 +02:00
|
|
|
from typing import Any, Callable, Dict, List
|
2021-05-31 20:07:03 +02:00
|
|
|
from flask import Flask, abort, request, Response
|
2019-12-08 22:43:49 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from bemani.api.exceptions import APIException
|
2022-10-15 20:56:30 +02:00
|
|
|
from bemani.api.objects import (
|
|
|
|
RecordsObject,
|
|
|
|
ProfileObject,
|
|
|
|
StatisticsObject,
|
|
|
|
CatalogObject,
|
|
|
|
)
|
2021-05-20 00:38:00 +02:00
|
|
|
from bemani.api.types import g
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.common import GameConstants, APIConstants, VersionConstants
|
2021-08-20 06:43:59 +02:00
|
|
|
from bemani.data import Config, Data
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
app = Flask(__name__)
|
2021-08-20 06:43:59 +02:00
|
|
|
config = Config()
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
SUPPORTED_VERSIONS: List[str] = ["v1"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def jsonify_response(data: Dict[str, Any], code: int = 200) -> Response:
|
2019-12-08 22:43:49 +01:00
|
|
|
return Response(
|
2022-10-15 20:56:30 +02:00
|
|
|
json.dumps(data).encode("utf8"),
|
2019-12-08 22:43:49 +01:00
|
|
|
content_type="application/json; charset=utf-8",
|
|
|
|
status=code,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def before_request() -> None:
|
|
|
|
global config
|
|
|
|
|
|
|
|
g.config = config
|
|
|
|
g.data = Data(config)
|
|
|
|
g.authorized = False
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
authkey = request.headers.get("Authorization")
|
2019-12-08 22:43:49 +01:00
|
|
|
if authkey is not None:
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
authtype, authtoken = authkey.split(" ", 1)
|
2019-12-08 22:43:49 +01:00
|
|
|
except ValueError:
|
|
|
|
authtype = None
|
|
|
|
authtoken = None
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if authtype.lower() == "token":
|
2019-12-08 22:43:49 +01:00
|
|
|
g.authorized = g.data.local.api.validate_client(authtoken)
|
|
|
|
|
|
|
|
|
2020-04-28 01:31:59 +02:00
|
|
|
@app.after_request
|
|
|
|
def after_request(response: Response) -> Response:
|
|
|
|
# Make sure our REST responses don't get cached, so that remote
|
|
|
|
# servers which respect cache headers don't get confused.
|
|
|
|
response.cache_control.no_cache = True
|
|
|
|
response.cache_control.must_revalidate = True
|
|
|
|
response.cache_control.private = True
|
2020-05-05 00:34:36 +02:00
|
|
|
return response
|
2020-04-28 01:31:59 +02:00
|
|
|
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
@app.teardown_request
|
|
|
|
def teardown_request(exception: Any) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
data = getattr(g, "data", None)
|
2019-12-08 22:43:49 +01:00
|
|
|
if data is not None:
|
|
|
|
data.close()
|
|
|
|
|
|
|
|
|
|
|
|
def authrequired(func: Callable) -> Callable:
|
|
|
|
@wraps(func)
|
|
|
|
def decoratedfunction(*args: Any, **kwargs: Any) -> Response:
|
|
|
|
if not g.authorized:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Unauthorized client!"},
|
2019-12-08 22:43:49 +01:00
|
|
|
401,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return func(*args, **kwargs)
|
2022-10-15 20:56:30 +02:00
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
return decoratedfunction
|
|
|
|
|
|
|
|
|
|
|
|
def jsonify(func: Callable) -> Callable:
|
|
|
|
@wraps(func)
|
|
|
|
def decoratedfunction(*args: Any, **kwargs: Any) -> Response:
|
|
|
|
return jsonify_response(func(*args, **kwargs))
|
2022-10-15 20:56:30 +02:00
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
return decoratedfunction
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def server_exception(exception: Any) -> Response:
|
2022-10-15 20:56:30 +02:00
|
|
|
stack = "".join(
|
|
|
|
traceback.format_exception(type(exception), exception, exception.__traceback__)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
print(stack)
|
|
|
|
try:
|
|
|
|
g.data.local.network.put_event(
|
2022-10-15 20:56:30 +02:00
|
|
|
"exception",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"service": "api",
|
|
|
|
"request": request.url,
|
|
|
|
"traceback": stack,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Exception occured while processing request."},
|
2019-12-08 22:43:49 +01:00
|
|
|
500,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(APIException)
|
|
|
|
def api_exception(exception: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": exception.message},
|
2019-12-08 22:43:49 +01:00
|
|
|
exception.code,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def server_error(error: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Exception occured while processing request."},
|
2019-12-08 22:43:49 +01:00
|
|
|
500,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(501)
|
|
|
|
def protocol_error(error: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Unsupported protocol version in request."},
|
2019-12-08 22:43:49 +01:00
|
|
|
501,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(400)
|
|
|
|
def bad_json(error: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Request JSON could not be decoded."},
|
2019-12-08 22:43:49 +01:00
|
|
|
500,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def unrecognized_object(error: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Unrecognized request game/version or object."},
|
2019-12-08 22:43:49 +01:00
|
|
|
404,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(405)
|
|
|
|
def invalid_request(error: Any) -> Response:
|
|
|
|
return jsonify_response(
|
2022-10-15 20:56:30 +02:00
|
|
|
{"error": "Invalid request URI or method."},
|
2019-12-08 22:43:49 +01:00
|
|
|
405,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@app.route("/<path:path>", methods=["GET", "POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@authrequired
|
|
|
|
def catch_all(path: str) -> Response:
|
|
|
|
abort(405)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@app.route("/", methods=["GET", "POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@authrequired
|
|
|
|
@jsonify
|
|
|
|
def info() -> Dict[str, Any]:
|
|
|
|
requestdata = request.get_json()
|
|
|
|
if requestdata is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Request JSON could not be decoded.")
|
2019-12-08 22:43:49 +01:00
|
|
|
if requestdata:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Unrecognized parameters for request.")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"versions": SUPPORTED_VERSIONS,
|
|
|
|
"name": g.config.name,
|
|
|
|
"email": g.config.email,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@app.route("/<protoversion>/<requestgame>/<requestversion>", methods=["GET", "POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@authrequired
|
|
|
|
@jsonify
|
|
|
|
def lookup(protoversion: str, requestgame: str, requestversion: str) -> Dict[str, Any]:
|
|
|
|
requestdata = request.get_json()
|
2022-10-15 20:56:30 +02:00
|
|
|
for expected in ["type", "ids", "objects"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
if expected not in requestdata:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Missing parameters for request.")
|
2019-12-08 22:43:49 +01:00
|
|
|
for param in requestdata:
|
2022-10-15 20:56:30 +02:00
|
|
|
if param not in ["type", "ids", "objects", "since", "until"]:
|
|
|
|
raise APIException("Unrecognized parameters for request.")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
args = copy.deepcopy(requestdata)
|
2022-10-15 20:56:30 +02:00
|
|
|
del args["type"]
|
|
|
|
del args["ids"]
|
|
|
|
del args["objects"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if protoversion not in SUPPORTED_VERSIONS:
|
|
|
|
# Don't know about this protocol version
|
|
|
|
abort(501)
|
|
|
|
|
|
|
|
# Figure out what games we support based on config, and map those.
|
|
|
|
gamemapping = {}
|
2023-02-17 04:02:41 +01:00
|
|
|
for gameid, constant in [
|
2022-10-15 20:56:30 +02:00
|
|
|
("ddr", GameConstants.DDR),
|
|
|
|
("iidx", GameConstants.IIDX),
|
|
|
|
("jubeat", GameConstants.JUBEAT),
|
|
|
|
("museca", GameConstants.MUSECA),
|
|
|
|
("popnmusic", GameConstants.POPN_MUSIC),
|
|
|
|
("reflecbeat", GameConstants.REFLEC_BEAT),
|
|
|
|
("soundvoltex", GameConstants.SDVX),
|
2019-12-08 22:43:49 +01:00
|
|
|
]:
|
2021-08-20 06:43:59 +02:00
|
|
|
if constant in g.config.support:
|
2019-12-08 22:43:49 +01:00
|
|
|
gamemapping[gameid] = constant
|
|
|
|
game = gamemapping.get(requestgame)
|
|
|
|
if game is None:
|
|
|
|
# Don't support this game!
|
|
|
|
abort(404)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if requestversion[0] == "o":
|
2019-12-08 22:43:49 +01:00
|
|
|
omnimix = True
|
|
|
|
requestversion = requestversion[1:]
|
|
|
|
else:
|
|
|
|
omnimix = False
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
version = (
|
|
|
|
{
|
|
|
|
GameConstants.DDR: {
|
|
|
|
"12": VersionConstants.DDR_X2,
|
|
|
|
"13": VersionConstants.DDR_X3_VS_2NDMIX,
|
|
|
|
"14": VersionConstants.DDR_2013,
|
|
|
|
"15": VersionConstants.DDR_2014,
|
|
|
|
"16": VersionConstants.DDR_ACE,
|
2022-11-01 04:39:01 +01:00
|
|
|
"17": VersionConstants.DDR_A20,
|
2022-10-15 20:56:30 +02:00
|
|
|
},
|
|
|
|
GameConstants.IIDX: {
|
|
|
|
"20": VersionConstants.IIDX_TRICORO,
|
|
|
|
"21": VersionConstants.IIDX_SPADA,
|
|
|
|
"22": VersionConstants.IIDX_PENDUAL,
|
|
|
|
"23": VersionConstants.IIDX_COPULA,
|
|
|
|
"24": VersionConstants.IIDX_SINOBUZ,
|
|
|
|
"25": VersionConstants.IIDX_CANNON_BALLERS,
|
|
|
|
"26": VersionConstants.IIDX_ROOTAGE,
|
2022-11-01 04:39:01 +01:00
|
|
|
"27": VersionConstants.IIDX_HEROIC_VERSE,
|
|
|
|
"28": VersionConstants.IIDX_BISTROVER,
|
2022-10-15 20:56:30 +02:00
|
|
|
},
|
|
|
|
GameConstants.JUBEAT: {
|
|
|
|
"5": VersionConstants.JUBEAT_SAUCER,
|
|
|
|
"5a": VersionConstants.JUBEAT_SAUCER_FULFILL,
|
|
|
|
"6": VersionConstants.JUBEAT_PROP,
|
|
|
|
"7": VersionConstants.JUBEAT_QUBELL,
|
|
|
|
"8": VersionConstants.JUBEAT_CLAN,
|
|
|
|
"9": VersionConstants.JUBEAT_FESTO,
|
2022-11-01 04:39:01 +01:00
|
|
|
"10": VersionConstants.JUBEAT_AVENUE,
|
2022-10-15 20:56:30 +02:00
|
|
|
},
|
|
|
|
GameConstants.MUSECA: {
|
|
|
|
"1": VersionConstants.MUSECA,
|
|
|
|
"1p": VersionConstants.MUSECA_1_PLUS,
|
|
|
|
},
|
|
|
|
GameConstants.POPN_MUSIC: {
|
|
|
|
"19": VersionConstants.POPN_MUSIC_TUNE_STREET,
|
|
|
|
"20": VersionConstants.POPN_MUSIC_FANTASIA,
|
|
|
|
"21": VersionConstants.POPN_MUSIC_SUNNY_PARK,
|
|
|
|
"22": VersionConstants.POPN_MUSIC_LAPISTORIA,
|
|
|
|
"23": VersionConstants.POPN_MUSIC_ECLALE,
|
|
|
|
"24": VersionConstants.POPN_MUSIC_USANEKO,
|
2022-11-01 04:39:01 +01:00
|
|
|
"25": VersionConstants.POPN_MUSIC_PEACE,
|
|
|
|
"26": VersionConstants.POPN_MUSIC_KAIMEI_RIDDLES,
|
2022-10-15 20:56:30 +02:00
|
|
|
},
|
|
|
|
GameConstants.REFLEC_BEAT: {
|
|
|
|
"1": VersionConstants.REFLEC_BEAT,
|
|
|
|
"2": VersionConstants.REFLEC_BEAT_LIMELIGHT,
|
|
|
|
# We don't support non-final COLETTE, so just return scores for
|
|
|
|
# final colette to any network that asks.
|
|
|
|
"3w": VersionConstants.REFLEC_BEAT_COLETTE,
|
|
|
|
"3sp": VersionConstants.REFLEC_BEAT_COLETTE,
|
|
|
|
"3su": VersionConstants.REFLEC_BEAT_COLETTE,
|
|
|
|
"3a": VersionConstants.REFLEC_BEAT_COLETTE,
|
|
|
|
"3as": VersionConstants.REFLEC_BEAT_COLETTE,
|
|
|
|
# We don't support groovin'!!, so just return upper scores.
|
|
|
|
"4": VersionConstants.REFLEC_BEAT_GROOVIN,
|
|
|
|
"4u": VersionConstants.REFLEC_BEAT_GROOVIN,
|
|
|
|
"5": VersionConstants.REFLEC_BEAT_VOLZZA,
|
|
|
|
"5a": VersionConstants.REFLEC_BEAT_VOLZZA_2,
|
|
|
|
"6": VersionConstants.REFLEC_BEAT_REFLESIA,
|
|
|
|
},
|
|
|
|
GameConstants.SDVX: {
|
|
|
|
"1": VersionConstants.SDVX_BOOTH,
|
|
|
|
"2": VersionConstants.SDVX_INFINITE_INFECTION,
|
|
|
|
"3": VersionConstants.SDVX_GRAVITY_WARS,
|
|
|
|
"4": VersionConstants.SDVX_HEAVENLY_HAVEN,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.get(game, {})
|
|
|
|
.get(requestversion)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
if version is None:
|
|
|
|
# Don't support this version!
|
|
|
|
abort(404)
|
|
|
|
|
2021-08-19 21:21:41 +02:00
|
|
|
# Attempt to coerce ID type. If we fail, provide the correct failure message.
|
|
|
|
idtype = None
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
idtype = APIConstants(requestdata["type"])
|
2021-08-19 21:21:41 +02:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if idtype is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Invalid ID type provided!")
|
2021-08-19 21:21:41 +02:00
|
|
|
|
|
|
|
# Validate the provided IDs given the ID type above.
|
2022-10-15 20:56:30 +02:00
|
|
|
ids = requestdata["ids"]
|
2019-12-08 22:43:49 +01:00
|
|
|
if idtype == APIConstants.ID_TYPE_CARD and len(ids) == 0:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Invalid number of IDs given!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if idtype == APIConstants.ID_TYPE_SONG and len(ids) not in [1, 2]:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Invalid number of IDs given!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if idtype == APIConstants.ID_TYPE_INSTANCE and len(ids) != 3:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Invalid number of IDs given!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if idtype == APIConstants.ID_TYPE_SERVER and len(ids) != 0:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise APIException("Invalid number of IDs given!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
responsedata = {}
|
2022-10-15 20:56:30 +02:00
|
|
|
for obj in requestdata["objects"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
handler = {
|
2022-10-15 20:56:30 +02:00
|
|
|
"records": RecordsObject,
|
|
|
|
"profile": ProfileObject,
|
|
|
|
"statistics": StatisticsObject,
|
|
|
|
"catalog": CatalogObject,
|
2019-12-08 22:43:49 +01:00
|
|
|
}.get(obj)
|
|
|
|
if handler is None:
|
|
|
|
# Don't support this object type
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
inst = handler(g.data, game, version, omnimix)
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
fetchmethod = getattr(inst, f"fetch_{protoversion}")
|
2019-12-08 22:43:49 +01:00
|
|
|
except AttributeError:
|
|
|
|
# Don't know how to handle this object for this version
|
|
|
|
abort(501)
|
|
|
|
|
|
|
|
responsedata[obj] = fetchmethod(idtype, ids, args)
|
|
|
|
|
|
|
|
return responsedata
|