Download api/checkin.py from zlcy/abc: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/spaces/zlcy/abc/resolve/main/api/checkin.py
- Command line
-
hf download hf://spaces/zlcy/abc/api/checkin.py
-
curl -L -o checkin.py https://huggingface.co/spaces/zlcy/abc/resolve/main/api/checkin.py
2.75 kB
| from __future__ import annotations | |
| import random | |
| from fastapi import APIRouter, Header, HTTPException | |
| from pydantic import BaseModel | |
| from api.support import require_admin, require_identity | |
| from services.auth_service import auth_service | |
| from services.config import config | |
| class CheckinRequest(BaseModel): | |
| mode: str = "checkin" | |
| def create_router() -> APIRouter: | |
| router = APIRouter() | |
| async def get_admin_checkin(authorization: str | None = Header(default=None)): | |
| require_admin(authorization) | |
| return {**auth_service.checkin_admin_summary(), "ads": config.get().get("checkin_ads", [])} | |
| async def get_checkin(authorization: str | None = Header(default=None)): | |
| identity = require_identity(authorization) | |
| if identity.get("role") != "normal": | |
| raise HTTPException(status_code=403, detail={"error": "仅普通用户可以签到"}) | |
| try: | |
| summary = auth_service.checkin_summary(str(identity.get("id") or "")) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=404, detail={"error": str(exc)}) from exc | |
| return { | |
| **summary, | |
| "credits": auth_service.credit_summary(identity), | |
| "ads": config.get().get("checkin_ads", []), | |
| "content": config.checkin_content, | |
| "credit_valid_days": config.checkin_credit_valid_days, | |
| } | |
| async def perform_checkin(body: CheckinRequest, authorization: str | None = Header(default=None)): | |
| identity = require_identity(authorization) | |
| if identity.get("role") != "normal": | |
| raise HTTPException(status_code=403, detail={"error": "仅普通用户可以签到"}) | |
| mode = "random" if body.mode == "random" else "checkin" | |
| reward = random.randint(config.checkin_random_min, config.checkin_random_max) if mode == "random" else max(1, config.checkin_daily_credits) | |
| try: | |
| result = auth_service.perform_checkin(str(identity.get("id") or ""), mode=mode, reward=reward) | |
| summary = auth_service.checkin_summary(str(identity.get("id") or "")) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=409, detail={"error": str(exc)}) from exc | |
| return { | |
| **summary, | |
| "result": result, | |
| "random_range": {"min": config.checkin_random_min, "max": config.checkin_random_max}, | |
| "credits": auth_service.credit_summary(identity), | |
| "ads": config.get().get("checkin_ads", []), | |
| "content": config.checkin_content, | |
| "credit_valid_days": config.checkin_credit_valid_days, | |
| } | |
| return router | |