luoxu-api-pub/main.py

112 lines
3.0 KiB
Python
Raw Normal View History

2023-09-14 15:19:44 +00:00
from typing import Union
2023-08-05 13:40:44 +00:00
from fastapi import FastAPI
2024-03-30 14:02:27 +00:00
from httpx import AsyncClient, ReadTimeout
2023-08-05 13:40:44 +00:00
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import StreamingResponse, RedirectResponse
from starlette.staticfiles import StaticFiles
app = FastAPI()
local_api = "http://127.0.0.1:9008/luoxu/"
2023-09-14 15:09:42 +00:00
local_stats_api = "http://127.0.0.1:9010/api/"
2023-09-14 15:19:44 +00:00
ghost_url = "https://search-pub.xtaolabs.com/luoxu/avatar/ghost.jpg"
2023-08-05 13:40:44 +00:00
allowed_origins = [
"search-pub.xtaolabs.com",
"127.0.0.1:9009",
]
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2023-08-08 10:06:08 +00:00
groups_id = [1366383997, 1797471403, 1936247070]
2023-09-14 15:09:42 +00:00
groups_stats_id = [-1001797471403]
2023-08-05 13:40:44 +00:00
groups = {
2023-08-08 10:06:08 +00:00
"groups": [
{
"group_id": "1366383997",
"name": "原神 · 提瓦特大陆",
"pub_id": "GenshinImpact"
},
{
"group_id": "1797471403",
"name": "崩坏:星穹铁道 · 仙舟:罗浮",
"pub_id": "HSRCN_Group"
},
{
"group_id": "1936247070",
"name": "绝区零 · 新艾利都",
"pub_id": "ZZZGroups"
}
]
2023-08-05 13:40:44 +00:00
}
@app.get("/luoxu/groups")
async def get_groups():
return groups
@app.get("/luoxu/search")
async def get_search(request: Request):
try:
if int(request.query_params.get("g", 0)) in groups_id:
async with AsyncClient(timeout=60) as client:
r = await client.get(f"{local_api}search", params=request.query_params)
return r.json()
except:
pass
return {}
@app.get("/luoxu/names")
async def get_names(request: Request):
try:
if int(request.query_params.get("g", 0)) in groups_id:
async with AsyncClient(timeout=60) as client:
r = await client.get(f"{local_api}names", params=request.query_params)
return r.json()
except:
pass
return {}
@app.get("/luoxu/avatar/{avatar}")
2023-09-14 15:19:44 +00:00
async def read_avatar(avatar: str):
2023-08-05 13:40:44 +00:00
async with AsyncClient(timeout=60) as client:
2024-03-30 14:02:27 +00:00
try:
r = await client.get(f"{local_api}avatar/{avatar}")
except ReadTimeout:
return RedirectResponse(url=ghost_url)
2023-09-14 15:27:04 +00:00
if r.status_code == 307:
2023-09-14 15:19:44 +00:00
return RedirectResponse(url=ghost_url)
2023-08-05 13:40:44 +00:00
return StreamingResponse(r.aiter_bytes(), media_type="image/png")
2023-09-14 15:09:42 +00:00
@app.get("/luoxu/stats/{gid}")
async def read_avatar(gid: str):
try:
if int(gid) not in groups_stats_id:
return []
except Exception:
return []
async with AsyncClient(timeout=60) as client:
r = await client.get(f"{local_stats_api}group_analytics?cid={gid}")
return r.json()
2023-08-05 13:40:44 +00:00
@app.get("/")
async def redirect_to_html():
return RedirectResponse(url="/index.html")
app.mount("/", StaticFiles(directory="html"), name="html")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, port=9009)