Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,175 +1,154 @@
|
|
1 |
-
import os, time, json, datetime, requests, gradio as gr
|
2 |
|
3 |
# ──────────────────────────────────────────────────────────
|
|
|
|
|
4 |
# 1. Vercel API 설정
|
5 |
-
|
6 |
-
|
7 |
-
if not
|
8 |
raise EnvironmentError("SVR_TOKEN 환경변수를 설정하세요.")
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# 2. BEST 탭 파일 / 상수
|
15 |
-
BEST_FILE = "best_games.json"
|
16 |
-
CARDS_PER_PG = 48
|
17 |
-
|
18 |
-
def init_best():
|
19 |
if not os.path.exists(BEST_FILE):
|
20 |
json.dump([], open(BEST_FILE, "w"))
|
21 |
-
|
22 |
-
def load_best():
|
23 |
try:
|
24 |
return json.load(open(BEST_FILE))
|
25 |
except Exception:
|
26 |
return []
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
def fetch_deployments(limit=100):
|
31 |
try:
|
32 |
params = {"limit": limit}
|
33 |
-
if
|
34 |
-
params["teamId"] =
|
35 |
-
r = requests.get(f"{
|
36 |
-
headers=
|
37 |
r.raise_for_status()
|
38 |
|
39 |
-
|
40 |
for d in r.json().get("deployments", []):
|
41 |
if d.get("state") != "READY":
|
42 |
continue
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
"
|
48 |
-
"
|
|
|
49 |
})
|
50 |
-
return sorted(
|
51 |
except Exception as e:
|
52 |
print("Vercel API 오류:", e)
|
53 |
return []
|
54 |
|
55 |
-
#
|
56 |
-
def
|
57 |
-
s = (
|
58 |
-
e = s +
|
59 |
-
total = (len(lst)
|
60 |
return lst[s:e], total
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
def make_html(cards, page, total):
|
65 |
if not cards:
|
66 |
-
return "<div style='text-align:center;padding:
|
67 |
|
68 |
-
css = """
|
69 |
<style>
|
70 |
-
body{margin:0;
|
71 |
-
.grid{display:grid;grid-template-columns:1fr;gap:
|
72 |
-
.
|
73 |
-
|
74 |
-
.
|
75 |
-
.
|
76 |
-
.
|
77 |
-
.
|
78 |
-
.
|
79 |
-
.
|
80 |
-
.
|
81 |
-
|
82 |
-
.
|
83 |
-
|
84 |
-
.
|
85 |
-
.btn:disabled{opacity:.4;cursor:default}
|
86 |
-
.count{font-size:.85rem;color:#555;text-align:center;margin-top:-4px}
|
87 |
</style>
|
88 |
"""
|
89 |
-
|
90 |
-
html = css + "<div class='grid'>"
|
91 |
for c in cards:
|
92 |
-
date = datetime.datetime.fromtimestamp(c["
|
93 |
-
|
94 |
<div class='card'>
|
95 |
-
<div class='
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
</div>
|
107 |
-
"""
|
108 |
-
html += "</div>"
|
109 |
-
html += f"<p class='count'>Page {page} / {total}</p>"
|
110 |
-
return html
|
111 |
-
|
112 |
-
# ──────────────────────────────────────────────────────────
|
113 |
-
def build_app():
|
114 |
-
init_best()
|
115 |
-
|
116 |
with gr.Blocks(title="Vibe Game Craft", css="body{overflow-x:hidden;}") as demo:
|
117 |
-
gr.Markdown("<h1 style='text-align:center;padding:
|
118 |
|
119 |
with gr.Row():
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
tab
|
127 |
-
np
|
128 |
-
bp
|
129 |
-
out
|
130 |
-
|
131 |
-
#
|
132 |
def show_new(p=1):
|
133 |
-
|
134 |
-
return
|
135 |
-
|
136 |
def show_best(p=1):
|
137 |
-
|
138 |
-
return
|
139 |
|
140 |
-
|
|
|
141 |
if t=="new":
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
return html, cp_n, cp_b
|
146 |
-
|
147 |
-
def nxt(t, cp_n, cp_b):
|
148 |
if t=="new":
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
# 초기 로드
|
167 |
demo.load(show_new, outputs=[out, tab, np])
|
168 |
|
169 |
return demo
|
170 |
|
171 |
-
#
|
172 |
-
app =
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
app.launch()
|
|
|
|
|
1 |
|
2 |
# ──────────────────────────────────────────────────────────
|
3 |
+
import os, time, json, datetime, requests, gradio as gr
|
4 |
+
|
5 |
# 1. Vercel API 설정
|
6 |
+
TOKEN = os.getenv("SVR_TOKEN") # 필수
|
7 |
+
TEAM = os.getenv("VERCEL_TEAM_ID") # 팀 계정이면 지정
|
8 |
+
if not TOKEN:
|
9 |
raise EnvironmentError("SVR_TOKEN 환경변수를 설정하세요.")
|
10 |
+
API = "https://api.vercel.com"
|
11 |
+
HEAD = {"Authorization": f"Bearer {TOKEN}"}
|
12 |
|
13 |
+
# 2. BEST 탭 데이터
|
14 |
+
BEST_FILE = "best_games.json"
|
15 |
+
PER_PAGE = 48
|
16 |
+
def _init_best():
|
|
|
|
|
|
|
|
|
|
|
17 |
if not os.path.exists(BEST_FILE):
|
18 |
json.dump([], open(BEST_FILE, "w"))
|
19 |
+
def _load_best():
|
|
|
20 |
try:
|
21 |
return json.load(open(BEST_FILE))
|
22 |
except Exception:
|
23 |
return []
|
24 |
|
25 |
+
# 3. 모든 배포 가져오기 (v6)
|
26 |
+
def fetch_all(limit=200):
|
|
|
27 |
try:
|
28 |
params = {"limit": limit}
|
29 |
+
if TEAM:
|
30 |
+
params["teamId"] = TEAM
|
31 |
+
r = requests.get(f"{API}/v6/deployments",
|
32 |
+
headers=HEAD, params=params, timeout=30)
|
33 |
r.raise_for_status()
|
34 |
|
35 |
+
out = []
|
36 |
for d in r.json().get("deployments", []):
|
37 |
if d.get("state") != "READY":
|
38 |
continue
|
39 |
+
created_ms = d.get("created", time.time()*1000)
|
40 |
+
url_host = d.get("url", "")
|
41 |
+
url_full = url_host if url_host.startswith("http") else f"https://{url_host}"
|
42 |
+
out.append({
|
43 |
+
"title": d.get("name", "(제목 없음)"),
|
44 |
+
"url" : url_full,
|
45 |
+
"ts" : int(created_ms / 1000)
|
46 |
})
|
47 |
+
return sorted(out, key=lambda x: x["ts"], reverse=True)
|
48 |
except Exception as e:
|
49 |
print("Vercel API 오류:", e)
|
50 |
return []
|
51 |
|
52 |
+
# 4. 페이지네이션
|
53 |
+
def page(lst, pg):
|
54 |
+
s = (pg-1)*PER_PAGE
|
55 |
+
e = s + PER_PAGE
|
56 |
+
total = (len(lst)+PER_PAGE-1)//PER_PAGE
|
57 |
return lst[s:e], total
|
58 |
|
59 |
+
# 5. HTML (3-열 그리드, 반응형)
|
60 |
+
def html(cards, pg, total):
|
|
|
61 |
if not cards:
|
62 |
+
return "<div style='text-align:center;padding:70px;color:#555;'>표시할 배포가 없습니다.</div>"
|
63 |
|
64 |
+
css = r"""
|
65 |
<style>
|
66 |
+
body{margin:0;font-family:Poppins,sans-serif;background:linear-gradient(135deg,#C5E8FF 0%,#FFD6E0 100%);}
|
67 |
+
.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:28px 24px;margin:0 20px 60px;}
|
68 |
+
@media(max-width:1024px){.grid{grid-template-columns:repeat(2,1fr);} }
|
69 |
+
@media(max-width:640px){ .grid{grid-template-columns:1fr;} }
|
70 |
+
.card{background:#fff;border-radius:18px;overflow:hidden;box-shadow:0 10px 25px rgba(0,0,0,.08);transition:.3s}
|
71 |
+
.card:hover{transform:translateY(-6px);box-shadow:0 16px 40px rgba(0,0,0,.12)}
|
72 |
+
.hdr{padding:20px 24px;background:rgba(255,255,255,.75);backdrop-filter:blur(8px);border-bottom:1px solid #eee;}
|
73 |
+
.ttl{margin:0;font-size:1.15rem;font-weight:700;color:#333;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
|
74 |
+
.date{margin-top:4px;font-size:.85rem;color:#777;}
|
75 |
+
.frame{position:relative;width:100%;padding-top:60%;overflow:hidden;}
|
76 |
+
.frame iframe{position:absolute;top:0;left:0;width:142.857%;height:142.857%;
|
77 |
+
transform:scale(.7);transform-origin:top left;border:0;}
|
78 |
+
.foot{padding:14px 24px;background:rgba(255,255,255,.85);backdrop-filter:blur(8px);text-align:right;}
|
79 |
+
.link{font-size:.85rem;font-weight:600;color:#4a6dd8;text-decoration:none;}
|
80 |
+
.cnt{text-align:center;font-size:.85rem;color:#555;margin:10px 0 40px;}
|
|
|
|
|
81 |
</style>
|
82 |
"""
|
83 |
+
h = css + "<div class='grid'>"
|
|
|
84 |
for c in cards:
|
85 |
+
date = datetime.datetime.fromtimestamp(c["ts"]).strftime("%Y-%m-%d")
|
86 |
+
h += f"""
|
87 |
<div class='card'>
|
88 |
+
<div class='hdr'><p class='ttl'>{c['title']}</p><p class='date'>{date}</p></div>
|
89 |
+
<div class='frame'><iframe src="{c['url']}" loading="lazy"
|
90 |
+
allow="accelerometer; camera; encrypted-media; gyroscope;"></iframe></div>
|
91 |
+
<div class='foot'><a class='link' href="{c['url']}" target="_blank">원본↗</a></div>
|
92 |
+
</div>"""
|
93 |
+
h += "</div><p class='cnt'>Page "+str(pg)+" / "+str(total)+"</p>"
|
94 |
+
return h
|
95 |
+
|
96 |
+
# 6. Gradio UI
|
97 |
+
def build():
|
98 |
+
_init_best()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
with gr.Blocks(title="Vibe Game Craft", css="body{overflow-x:hidden;}") as demo:
|
100 |
+
gr.Markdown("<h1 style='text-align:center;padding:32px 0 0;color:#333;'>🎮 Vibe Game Craft</h1>")
|
101 |
|
102 |
with gr.Row():
|
103 |
+
b_new = gr.Button("NEW", size="sm")
|
104 |
+
b_best = gr.Button("BEST", size="sm")
|
105 |
+
b_prev = gr.Button("⬅️ Prev", size="sm")
|
106 |
+
b_next = gr.Button("Next ➡️", size="sm")
|
107 |
+
b_ref = gr.Button("🔄 Reload", size="sm")
|
108 |
+
|
109 |
+
tab = gr.State("new") # "new" / "best"
|
110 |
+
np = gr.State(1) # NEW 페이지
|
111 |
+
bp = gr.State(1) # BEST 페이지
|
112 |
+
out = gr.HTML()
|
113 |
+
|
114 |
+
# NEW / BEST 렌더
|
115 |
def show_new(p=1):
|
116 |
+
data, tot = page(fetch_all(), p)
|
117 |
+
return html(data, p, tot), "new", p
|
|
|
118 |
def show_best(p=1):
|
119 |
+
data, tot = page(_load_best(), p)
|
120 |
+
return html(data, p, tot), "best", p
|
121 |
|
122 |
+
# Prev / Next
|
123 |
+
def prev(t, n, b):
|
124 |
if t=="new":
|
125 |
+
n=max(1,n-1); h,_,_ = show_new(n); return h, n, b
|
126 |
+
b=max(1,b-1); h,_,_ = show_best(b); return h, n, b
|
127 |
+
def nxt(t, n, b):
|
|
|
|
|
|
|
128 |
if t=="new":
|
129 |
+
maxp=(len(fetch_all())+PER_PAGE-1)//PER_PAGE
|
130 |
+
n=min(maxp,n+1); h,_,_ = show_new(n); return h, n, b
|
131 |
+
maxp=(len(_load_best())+PER_PAGE-1)//PER_PAGE
|
132 |
+
b=min(maxp,b+1); h,_,_ = show_best(b); return h, n, b
|
133 |
+
|
134 |
+
# Reload
|
135 |
+
def reload(t, n, b):
|
136 |
+
return show_new(n)[0] if t=="new" else show_best(b)[0]
|
137 |
+
|
138 |
+
# 버튼 연결
|
139 |
+
b_new.click(show_new, outputs=[out, tab, np])
|
140 |
+
b_best.click(show_best, outputs=[out, tab, bp])
|
141 |
+
b_prev.click(prev, inputs=[tab, np, bp], outputs=[out, np, bp])
|
142 |
+
b_next.click(nxt, inputs=[tab, np, bp], outputs=[out, np, bp])
|
143 |
+
b_ref.click(reload, inputs=[tab, np, bp], outputs=[out])
|
144 |
+
|
145 |
+
# 초기 로드 — NEW 1페이지
|
|
|
146 |
demo.load(show_new, outputs=[out, tab, np])
|
147 |
|
148 |
return demo
|
149 |
|
150 |
+
# 7. 실행
|
151 |
+
app = build()
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
app.launch()
|