import os, time, json, requests, datetime, gradio as gr # ──────────────────────────────────────────────────────────────────────────────── # 1. Vercel API 설정 ─ 환경변수에서 토큰 읽기 VERCEL_API_TOKEN = os.getenv("SVR_TOKEN") if not VERCEL_API_TOKEN: raise EnvironmentError("환경 변수 'SVR_TOKEN'이 설정되어 있지 않습니다!") VERCEL_API_URL = "https://api.vercel.com/v9" HEADERS = { "Authorization": f"Bearer {VERCEL_API_TOKEN}", "Content-Type": "application/json", } # ──────────────────────────────────────────────────────────────────────────────── # 2. 갤러리·페이지네이션 기본값 BEST_GAMES_FILE = "best_games.json" GAMES_PER_PAGE = 48 # ──────────────────────────────────────────────────────────────────────────────── # 3. BEST 탭용 샘플 게임 초기화 def initialize_best_games(): if not os.path.exists(BEST_GAMES_FILE): sample = [ { "title": "테트리스", "description": "클래식 테트리스 게임", "url": "https://tmkdop.vercel.app", "timestamp": time.time(), }, { "title": "스네이크", "description": "전통적인 스네이크 게임", "url": "https://tmkdop.vercel.app", "timestamp": time.time(), }, { "title": "팩맨", "description": "고전 아케이드 게임", "url": "https://tmkdop.vercel.app", "timestamp": time.time(), }, ] json.dump(sample, open(BEST_GAMES_FILE, "w")) def load_best_games(): try: return json.load(open(BEST_GAMES_FILE)) except Exception: return [] # ──────────────────────────────────────────────────────────────────────────────── # 4. 최신 Vercel 배포 → 게임 목록 def get_latest_deployments(): try: r = requests.get( f"{VERCEL_API_URL}/deployments", headers=HEADERS, params={"limit": 100}, timeout=30, ) r.raise_for_status() games = [] for d in r.json().get("deployments", []): if d.get("state") != "READY": continue ts = ( int(d["createdAt"] / 1000) if isinstance(d["createdAt"], (int, float)) else int(time.time()) ) games.append( { "title": d.get("name", "게임"), "description": f"배포된 게임: {d.get('name')}", "url": f"https://{d.get('url')}", "timestamp": ts, } ) return sorted(games, key=lambda x: x["timestamp"], reverse=True) except Exception as e: print("Vercel API 오류:", e) return [] # ──────────────────────────────────────────────────────────────────────────────── # 5. 페이지네이션 헬퍼 def paginate(lst, page): start = (page - 1) * GAMES_PER_PAGE end = start + GAMES_PER_PAGE total = (len(lst) + GAMES_PER_PAGE - 1) // GAMES_PER_PAGE return lst[start:end], total # ──────────────────────────────────────────────────────────────────────────────── # 6. 갤러리 HTML (사이트 미러링 스타일) def generate_gallery_html(games, page, total_pages, tab_name): if not games: return ( "
{g['title']}