import os, re, time, json, datetime, requests, gradio as gr # ───────────────────── 1. Vercel API ───────────────────── TOKEN = os.getenv("SVR_TOKEN") TEAM = os.getenv("VERCEL_TEAM_ID") if not TOKEN: raise EnvironmentError("SVR_TOKEN 환경변수를 설정하세요.") API = "https://api.vercel.com" HEAD = {"Authorization": f"Bearer {TOKEN}"} # ───────────────────── 2. BEST 데이터 ──────────────────── BEST_FILE, PER_PAGE = "best_games.json", 48 def _init_best(): if not os.path.exists(BEST_FILE): json.dump([], open(BEST_FILE, "w")) def _load_best(): try: data = json.load(open(BEST_FILE)) for it in data: if "ts" not in it: it["ts"] = int(it.get("timestamp", time.time())) return data except Exception: return [] # ───────────────────── 3. NEW: 6글자 vercel.app 배포 ───── # 3. NEW: 6글자 vercel.app 배포 PAT = re.compile(r"^[a-z0-9]{6}\.vercel\.app$", re.I) def fetch_all(limit=200): """ READY 배포 중 'tmkdop.vercel.app' 처럼 6글자 도메인이 달린 것만 수집 · url 필드와 alias 배열을 모두 검색 """ try: params = {"limit": limit} if TEAM: params["teamId"] = TEAM resp = requests.get(f"{API}/v6/deployments", headers=HEAD, params=params, timeout=30) resp.raise_for_status() games = [] for d in resp.json().get("deployments", []): if d.get("state") != "READY": continue # ── url + alias 배열 통합 검사 candidates = [d.get("url", "")] candidates.extend(d.get("alias", [])) six = next((dom for dom in candidates if PAT.match(dom)), None) if not six: continue games.append({ "title": d.get("name", "(제목 없음)"), "url": f"https://{six}", "ts": int(d.get("created", time.time() * 1000) / 1000) }) return sorted(games, key=lambda x: x["ts"], reverse=True) except Exception as e: print("Vercel API 오류:", e) return [] # ───────────────────── 4. 페이지네이션 ─────────────────── def page(lst, pg): s=(pg-1)*PER_PAGE; e=s+PER_PAGE total=(len(lst)+PER_PAGE-1)//PER_PAGE return lst[s:e], total # ───────────────────── 5. HTML 3-열 그리드 ─────────────── def html(cards, pg, total): if not cards: return "
표시할 배포가 없습니다.
" css=r""" """ h=css+"
" for c in cards: date=datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d") h+=f"""

{c['title']}

{date}

""" h+="

Page "+str(pg)+" / "+str(total)+"

" return h # ───────────────────── 6. Gradio Blocks UI ─────────────── def build(): _init_best() with gr.Blocks(title="Vibe Game Craft", css="body{overflow-x:hidden;}") as demo: gr.Markdown("

🎮 Vibe Game Craft

") with gr.Row(): b_new = gr.Button("NEW", size="sm") b_best = gr.Button("BEST", size="sm") b_prev = gr.Button("⬅️ Prev", size="sm") b_next = gr.Button("Next ➡️", size="sm") b_ref = gr.Button("🔄 Reload", size="sm") tab = gr.State("new"); np = gr.State(1); bp = gr.State(1); out = gr.HTML() def show_new(p=1): d,t=page(fetch_all(),p); return html(d,p,t),"new",p def show_best(p=1): d,t=page(_load_best(),p); return html(d,p,t),"best",p def prev(t,n,b): if t=="new": n=max(1,n-1); h,_,_=show_new(n); return h,n,b b=max(1,b-1); h,_,_=show_best(b); return h,n,b def nxt(t,n,b): if t=="new": maxp=(len(fetch_all())+PER_PAGE-1)//PER_PAGE n=min(maxp,n+1); h,_,_=show_new(n); return h,n,b maxp=(len(_load_best())+PER_PAGE-1)//PER_PAGE b=min(maxp,b+1); h,_,_=show_best(b); return h,n,b b_new.click(show_new, outputs=[out,tab,np]) b_best.click(show_best,outputs=[out,tab,bp]) b_prev.click(prev, inputs=[tab,np,bp], outputs=[out,np,bp]) b_next.click(nxt, inputs=[tab,np,bp], outputs=[out,np,bp]) b_ref.click(lambda t,n,b: show_new(n)[0] if t=="new" else show_best(b)[0], inputs=[tab,np,bp], outputs=[out]) demo.load(show_new, outputs=[out,tab,np]) return demo app = build() if __name__ == "__main__": app.launch()