Game-Gallery / app.py
ginipick's picture
Update app.py
b16d98a verified
raw
history blame
11 kB
import os, re, time, json, datetime, requests, gradio as gr
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 1. ๊ธฐ๋ณธ ์„ค์ • โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
BEST_FILE, PER_PAGE = "best_games.json", 9
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 2. BEST ๋ฐ์ดํ„ฐ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
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 as e:
print(f"BEST ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์˜ค๋ฅ˜: {e}")
return []
def _save_best(data):
try:
json.dump(data, open(BEST_FILE, "w"))
return True
except Exception as e:
print(f"BEST ๋ฐ์ดํ„ฐ ์ €์žฅ ์˜ค๋ฅ˜: {e}")
return False
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 3. URL ์ถ”๊ฐ€ ๊ธฐ๋Šฅ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def add_url_to_best(title, url):
"""์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•œ URL์„ BEST ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค."""
try:
# ํ˜„์žฌ BEST ๋ฐ์ดํ„ฐ ๋กœ๋“œ
data = _load_best()
# URL์ด ์ด๋ฏธ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
for item in data:
if item.get("url") == url:
print(f"URL์ด ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค: {url}")
return False
# ์ƒˆ ํ•ญ๋ชฉ ์ถ”๊ฐ€
new_item = {
"title": title,
"url": url,
"ts": int(time.time()),
"projectId": "", # ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๋ฏ€๋กœ projectId ์—†์Œ
"deploymentId": "" # ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๋ฏ€๋กœ deploymentId ์—†์Œ
}
data.append(new_item)
# ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ •๋ ฌ
data = sorted(data, key=lambda x: x["ts"], reverse=True)
# ์ €์žฅ
if _save_best(data):
print(f"URL์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {url}")
return True
return False
except Exception as e:
print(f"URL ์ถ”๊ฐ€ ์˜ค๋ฅ˜: {str(e)}")
return False
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 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 ๊ทธ๋ฆฌ๋“œ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def html(cards, pg, total):
if not cards:
return "<div style='text-align:center;padding:70px;color:#555;'>ํ‘œ์‹œํ•  ๋ฐฐํฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</div>"
# ์ˆœ์ˆ˜ HTML๊ณผ CSS๋กœ ๊ตฌํ˜„
h = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}
body, html {{
width: 100%;
height: 100%;
overflow: hidden;
font-family: Arial, sans-serif;
background: #f0f0f0;
}}
#app {{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}}
#grid-container {{
flex: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10px;
padding: 10px;
width: 100%;
height: 100%;
}}
.card {{
background: white;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
height: 100%;
}}
.card-header {{
padding: 8px 12px;
font-size: 0.9rem;
font-weight: bold;
border-bottom: 1px solid #f0f0f0;
z-index: 2;
background: rgba(255,255,255,0.9);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}}
.card-date {{
font-size: 0.75rem;
color: #888;
margin-top: 2px;
}}
.card-frame {{
flex: 1;
position: relative;
overflow: hidden;
}}
.card-frame iframe {{
position: absolute;
top: 0;
left: 0;
width: 142.857%;
height: 142.857%;
transform: scale(0.7);
transform-origin: top left;
border: 0;
}}
.card-footer {{
padding: 6px 12px;
text-align: right;
font-size: 0.8rem;
border-top: 1px solid #f0f0f0;
background: rgba(255,255,255,0.9);
z-index: 2;
}}
.card-footer a {{
color: #4a6dd8;
text-decoration: none;
font-weight: bold;
}}
#nav-container {{
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 15px 0;
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
z-index: 10;
}}
.nav-button {{
padding: 10px 25px;
background: rgba(255,255,255,0.9);
border: 1px solid #ddd;
border-radius: 50px;
font-size: 1.1rem;
font-weight: bold;
cursor: pointer;
box-shadow: 0 4px 10px rgba(0,0,0,0.15);
transition: all 0.2s ease;
color: #333;
}}
.nav-button:hover {{
transform: translateY(-2px);
box-shadow: 0 6px 15px rgba(0,0,0,0.18);
}}
.page-info {{
position: absolute;
bottom: 5px;
left: 0;
width: 100%;
text-align: center;
font-size: 0.8rem;
color: #888;
z-index: 9;
}}
/* ๋ฐฐ๊ฒฝ ๊ทธ๋ž˜๋””์–ธํŠธ ํšจ๊ณผ */
#bottom-gradient {{
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 80px;
background: linear-gradient(to bottom, rgba(240,240,240,0) 0%, rgba(240,240,240,0.9) 70%);
z-index: 5;
pointer-events: none;
}}
</style>
</head>
<body>
<div id="app">
<div id="grid-container">
"""
# ์นด๋“œ ์ถ”๊ฐ€
for c in cards:
date = datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d")
h += f"""
<div class="card">
<div class="card-header">
{c['title']}
<div class="card-date">{date}</div>
</div>
<div class="card-frame">
<iframe src="{c['url']}" loading="lazy" allow="accelerometer; camera; encrypted-media; gyroscope;"></iframe>
</div>
<div class="card-footer">
<a href="{c['url']}" target="_blank">์›๋ณธโ†—</a>
</div>
</div>
"""
# ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ ํŽ˜์ด์ง€ ์ •๋ณด
h += f"""
</div>
<div id="bottom-gradient"></div>
<div id="nav-container">
<button class="nav-button" onclick="prevPage()">โ—€ ์ด์ „</button>
<button class="nav-button" onclick="nextPage()">๋‹ค์Œ โ–ถ</button>
</div>
<div class="page-info">Page {pg} / {total}</div>
</div>
<script>
function prevPage() {{
if ({pg} > 1) {{
document.getElementById('prev-button').click();
}}
}}
function nextPage() {{
if ({pg} < {total}) {{
document.getElementById('next-button').click();
}}
}}
// ์Šคํฌ๋กค ๋ฐฉ์ง€
document.addEventListener('wheel', function(e) {{
e.preventDefault();
}}, {{ passive: false }});
document.addEventListener('touchmove', function(e) {{
e.preventDefault();
}}, {{ passive: false }});
</script>
</body>
</html>
"""
return h
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 6. Gradio Blocks UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def build():
_init_best()
with gr.Blocks(title="Vibe Game Craft", css="footer{display:none !important;}") as demo:
# ์ƒํƒœ ๋ฐ ์ถœ๋ ฅ
bp = gr.State(1)
out = gr.HTML(elem_id="display-area", visible=True)
# ์ˆจ๊ฒจ์ง„ ๋ฒ„ํŠผ (JavaScript์—์„œ ํ˜ธ์ถœ์šฉ)
b_prev = gr.Button("์ด์ „", elem_id="prev-button", visible=False)
b_next = gr.Button("๋‹ค์Œ", elem_id="next-button", visible=False)
def show_best(p=1):
d, t = page(_load_best(), p)
return html(d, p, t), p
def prev(b):
b = max(1, b-1)
h, _ = show_best(b)
return h, b
def nxt(b):
maxp = (len(_load_best()) + PER_PAGE - 1) // PER_PAGE
b = min(maxp, b+1)
h, _ = show_best(b)
return h, b
# ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
b_prev.click(prev, inputs=[bp], outputs=[out, bp])
b_next.click(nxt, inputs=[bp], outputs=[out, bp])
# ์ดˆ๊ธฐ ๋กœ๋“œ
demo.load(show_best, outputs=[out, bp])
return demo
app = build()
if __name__ == "__main__":
app.launch()