Spaces:
Running
Running
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>" | |
css = r""" | |
<style> | |
html, body { | |
height: 100%; | |
overflow: hidden !important; | |
margin: 0; | |
padding: 0; | |
font-family: Poppins, sans-serif; | |
background: #f0f0f0; | |
} | |
#gradio-app, #component-0, #component-1, .gradio-container { | |
height: 100vh !important; | |
overflow: hidden !important; | |
position: fixed !important; | |
top: 0 !important; | |
left: 0 !important; | |
right: 0 !important; | |
bottom: 0 !important; | |
} | |
.main-container { | |
position: fixed; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
height: 100vh; | |
width: 100vw; | |
overflow: hidden; | |
display: flex; | |
flex-direction: column; | |
} | |
.grid-container { | |
flex: 1; | |
overflow: hidden; | |
padding: 10px; | |
box-sizing: border-box; | |
} | |
.grid { | |
display: grid; | |
grid-template-columns: repeat(3, 1fr); | |
grid-template-rows: repeat(3, 1fr); | |
gap: 10px; | |
width: 100%; | |
height: 100%; | |
overflow: hidden; | |
box-sizing: border-box; | |
} | |
.card { | |
background: #fff; | |
border-radius: 10px; | |
overflow: hidden; | |
box-shadow: 0 4px 8px rgba(0,0,0,0.1); | |
display: flex; | |
flex-direction: column; | |
height: 100%; | |
position: relative; | |
} | |
.hdr { | |
padding: 8px; | |
background: rgba(255,255,255,.9); | |
border-bottom: 1px solid #eee; | |
flex-shrink: 0; | |
height: 40px; | |
box-sizing: border-box; | |
z-index: 2; | |
} | |
.ttl { | |
margin: 0; | |
font-size: 0.9rem; | |
font-weight: 600; | |
color: #333; | |
white-space: nowrap; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
} | |
.date { | |
margin-top: 0; | |
font-size: 0.7rem; | |
color: #777; | |
} | |
.frame { | |
position: absolute; | |
top: 40px; | |
left: 0; | |
right: 0; | |
bottom: 30px; | |
overflow: hidden; | |
} | |
.frame iframe { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 142.857%; | |
height: 142.857%; | |
transform: scale(0.7); | |
transform-origin: top left; | |
border: 0; | |
} | |
.foot { | |
position: absolute; | |
bottom: 0; | |
left: 0; | |
right: 0; | |
padding: 6px; | |
background: rgba(255,255,255,.9); | |
text-align: right; | |
height: 30px; | |
box-sizing: border-box; | |
z-index: 2; | |
} | |
.link { | |
font-size: 0.8rem; | |
font-weight: 600; | |
color: #4a6dd8; | |
text-decoration: none; | |
} | |
.navigation-buttons { | |
position: fixed; | |
bottom: 15px; | |
left: 0; | |
right: 0; | |
display: flex; | |
justify-content: center; | |
gap: 30px; | |
padding: 10px; | |
z-index: 1000; | |
background: transparent; | |
} | |
.navigation-buttons button { | |
background: rgba(255, 255, 255, 0.9); | |
border: 1px solid #ddd; | |
box-shadow: 0 4px 12px rgba(0,0,0,0.2); | |
padding: 10px 30px; | |
border-radius: 30px; | |
font-size: 1.2rem; | |
font-weight: bold; | |
cursor: pointer; | |
transition: all 0.2s; | |
} | |
.navigation-buttons button:hover { | |
background: #f0f0f0; | |
transform: translateY(-2px); | |
box-shadow: 0 6px 15px rgba(0,0,0,0.2); | |
} | |
footer, .footer { | |
display: none !important; | |
} | |
.page-info { | |
position: fixed; | |
bottom: 5px; | |
left: 0; | |
right: 0; | |
text-align: center; | |
font-size: 0.8rem; | |
color: #777; | |
z-index: 999; | |
} | |
/* ๋ชจ๋ฐ์ผ ์ต์ ํ */ | |
@media (max-width: 768px) { | |
.grid { | |
grid-template-columns: repeat(2, 1fr); | |
grid-template-rows: repeat(5, 1fr); | |
} | |
} | |
@media (max-width: 480px) { | |
.grid { | |
grid-template-columns: 1fr; | |
} | |
} | |
</style>""" | |
h = css + "<div class='main-container'><div class='grid-container'><div class='grid'>" | |
for c in cards: | |
date = datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d") | |
h += f""" | |
<div class='card'> | |
<div class='hdr'><p class='ttl'>{c['title']}</p><p class='date'>{date}</p></div> | |
<div class='frame'><iframe src="{c['url']}" loading="lazy" | |
allow="accelerometer; camera; encrypted-media; gyroscope;"></iframe></div> | |
<div class='foot'><a class='link' href="{c['url']}" target="_blank">์๋ณธโ</a></div> | |
</div>""" | |
h += "</div></div>" | |
# ํ์ด์ง ์ ๋ณด | |
h += f"<div class='page-info'>Page {pg} / {total}</div></div>" | |
# JavaScript๋ก ์คํฌ๋กค ๋ฐฉ์ง ์ถ๊ฐ | |
h += """ | |
<script> | |
// ์คํฌ๋กค ๋ฐฉ์ง | |
document.addEventListener('DOMContentLoaded', function() { | |
document.body.style.overflow = 'hidden'; | |
document.documentElement.style.overflow = 'hidden'; | |
// ํฐ์น ์ด๋ฒคํธ ๋ฐฉ์ง (๋ชจ๋ฐ์ผ) | |
document.addEventListener('touchmove', function(e) { | |
e.preventDefault(); | |
}, { passive: false }); | |
// ์คํฌ๋กค ์ด๋ฒคํธ ๋ฐฉ์ง | |
document.addEventListener('wheel', function(e) { | |
e.preventDefault(); | |
}, { passive: false }); | |
}); | |
</script> | |
""" | |
return h | |
# โโโโโโโโโโโโโโโโโโโโโ 6. Gradio Blocks UI โโโโโโโโโโโโโโโโโโโ | |
def build(): | |
_init_best() | |
with gr.Blocks(title="Vibe Game Craft", css="html,body{margin:0;padding:0;overflow:hidden !important;} footer{display:none !important;} .gradio-container{overflow:hidden !important; height:100vh; position:fixed; top:0; left:0; right:0; bottom:0;}") as demo: | |
# ์ํ ๋ฐ ์ถ๋ ฅ | |
bp = gr.State(1) | |
out = gr.HTML(elem_id="main-output") | |
# ํ์ด์ง ๋ค๋น๊ฒ์ด์ | |
with gr.Row(elem_classes="navigation-buttons"): | |
b_prev = gr.Button("โ ์ด์ ", size="lg") | |
b_next = gr.Button("๋ค์ โถ", size="lg") | |
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(debug=True, height=100) |