MonkeyBusiness/modules/__init__.py

67 lines
2.2 KiB
Python
Raw Normal View History

2022-08-26 12:39:11 +02:00
from importlib import util
from os import path
from glob import glob
2023-09-23 11:25:52 +02:00
from fastapi import APIRouter, Request
from typing import Optional
2022-08-26 12:39:11 +02:00
routers = []
2022-11-15 15:03:37 +01:00
for module_path in [
f
for f in glob(path.join(path.dirname(__file__), "**/*.py"), recursive=True)
if path.basename(f) != "__init__.py"
]:
spec = util.spec_from_file_location("", module_path)
2022-08-26 12:39:11 +02:00
module = util.module_from_spec(spec)
spec.loader.exec_module(module)
2022-11-15 15:03:37 +01:00
router = getattr(module, "router", None)
2022-08-26 12:39:11 +02:00
if router is not None:
routers.append(router)
2023-09-23 11:25:52 +02:00
if path.basename(module_path) != "api.py":
for obj in dir(module):
globals()[obj] = module.__dict__[obj]
router = APIRouter(tags=["slashless_forwarder"])
@router.post("/fwdr")
2023-10-07 14:06:24 +02:00
async def forward_slashless(
2023-09-23 11:25:52 +02:00
request: Request,
model: Optional[str] = None,
f: Optional[str] = None,
module: Optional[str] = None,
method: Optional[str] = None,
):
if f != None:
module, method = f.split(".")
try:
2023-10-07 14:06:24 +02:00
find_response = globals()[f"{module}_{method}".lower()]
return await find_response(request)
2023-09-23 11:25:52 +02:00
except KeyError:
2023-10-07 14:06:24 +02:00
try:
game_code = model.split(":")[0]
# TODO: check for more edge cases
if game_code == "MDX" and module == "eventlog" or module == "eventlog_2":
find_response = globals()[f"ddr_{module}_{method}"]
elif game_code == "REC":
find_response = globals()[f"drs_{module}_{method}"]
elif game_code == "KFC" and module == "eventlog":
find_response = globals()[f"sdvx_{module}_{method}"]
elif game_code == "M32":
if module == "lobby":
find_response = globals()[f"gitadora_{module}_{method}"]
else:
gd_module = module.split("_")
find_response = globals()[f"gitadora_{gd_module[-1]}_{method}"]
return await find_response(gd_module[0], request)
return await find_response(request)
except (KeyError, UnboundLocalError):
print("Try URL Slash 1 (On) if this game is supported.")
return Response(status_code=404)
2023-09-23 11:25:52 +02:00
routers.append(router)