Spaces:
Paused

Youtube / app.py
Demo555's picture
Rename keep_alive.py to app.py
c0768e6 verified
Raw History Blame Contribute Delete
11 kB
from flask import Flask, request, jsonify
import asyncio
import time
import os
from motor.motor_asyncio import AsyncIOMotorClient
app = Flask(__name__)
# Hugging Face Secrets se securely URI fetch karega
MONGO_URI = os.environ.get("MONGO_URI")
@app.route('/')
def home():
return "<h1>Bot Ad Server is running perfectly! ✅</h1>"
@app.route('/watch')
def watch_ad_page():
bot_id = request.args.get('bot_id', type=int)
user_id = request.args.get('user_id', type=int)
payload = request.args.get('payload', type=str)
async def get_clone_data():
client = AsyncIOMotorClient(MONGO_URI)
db = client['FileStoreBot']
clone_data = await db['clones'].find_one({"bot_id": bot_id})
client.close()
return clone_data
clone = asyncio.run(get_clone_data())
if not clone:
return "<h1>Error: Bot Not Found or Deactivated!</h1>"
settings = clone.get("settings", {})
ad_data = settings.get("watch_ad", {})
ad_link = ad_data.get("ad_link", "#")
bot_username = clone.get("username", "")
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Unlock Your File</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
:root {{
--bg-color: var(--tg-theme-secondary-bg-color, #f4f4f5);
--card-bg: var(--tg-theme-bg-color, #ffffff);
--text-main: var(--tg-theme-text-color, #222222);
--text-muted: var(--tg-theme-hint-color, #888888);
--accent-color: var(--tg-theme-button-color, #3390ec);
--accent-text: var(--tg-theme-button-text-color, #ffffff);
--success-color: #34c759;
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg-color);
color: var(--text-main);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
padding: 20px;
box-sizing: border-box;
}}
.card {{
background-color: var(--card-bg);
width: 100%;
max-width: 400px;
border-radius: 20px;
padding: 40px 25px;
box-sizing: border-box;
box-shadow: 0 10px 30px rgba(0,0,0,0.05);
text-align: center;
animation: slideUp 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}}
@keyframes slideUp {{
from {{ transform: translateY(20px); opacity: 0; }}
to {{ transform: translateY(0); opacity: 1; }}
}}
.icon-container {{
width: 70px;
height: 70px;
background: rgba(51, 144, 236, 0.1);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
color: var(--accent-color);
font-size: 32px;
}}
h2 {{ margin: 0 0 10px; font-size: 24px; font-weight: 700; }}
p.desc {{ color: var(--text-muted); font-size: 15px; line-height: 1.5; margin-bottom: 30px; }}
.btn {{
background-color: var(--accent-color);
color: var(--accent-text);
border: none;
padding: 16px 20px;
border-radius: 14px;
font-size: 17px;
font-weight: 600;
width: 100%;
box-sizing: border-box;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
text-decoration: none;
margin: 0 auto;
box-shadow: 0 4px 15px rgba(51, 144, 236, 0.3);
}}
.loading-state {{ display: none; animation: fadeIn 0.4s ease; }}
@keyframes fadeIn {{ from {{ opacity: 0; }} to {{ opacity: 1; }} }}
.progress-bg {{ background: rgba(51, 144, 236, 0.1); height: 10px; border-radius: 10px; overflow: hidden; margin: 20px 0 10px; }}
.progress-fill {{ background: var(--accent-color); height: 100%; width: 0%; border-radius: 10px; transition: width 1s linear; }}
.timer-text {{ font-size: 16px; font-weight: 600; color: var(--text-main); margin-bottom: 5px; }}
.sub-info {{ font-size: 13px; color: var(--text-muted); }}
.success-state {{ display: none; animation: fadeIn 0.4s ease; }}
.success-icon {{ background: rgba(52, 199, 89, 0.1); color: var(--success-color); }}
.success-text {{ color: var(--success-color); font-weight: 600; font-size: 18px; margin: 10px 0 20px; }}
.btn-success {{ background-color: var(--success-color); box-shadow: 0 4px 15px rgba(52, 199, 89, 0.3); }}
</style>
</head>
<body>
<div class="card" id="mainCard">
<div id="step1">
<div class="icon-container"><i class="fa-solid fa-lock"></i></div>
<h2>File Locked</h2>
<p class="desc">Please tap the button below to view a quick sponsor ad. Your file will unlock automatically in 15 seconds.</p>
<button class="btn" onclick="openAdAndStartTimer()">
<i class="fa-solid fa-play"></i> Watch Ad Now
</button>
</div>
<div id="step2" class="loading-state">
<div class="icon-container" style="animation: pulse 2s infinite;"><i class="fa-solid fa-hourglass-half"></i></div>
<h2 class="timer-text">Unlocking in <span id="countdown">15</span>s</h2>
<div class="progress-bg">
<div class="progress-fill" id="progressBar"></div>
</div>
<p class="sub-info">Ad opened in browser. Please wait here.</p>
</div>
<div id="step3" class="success-state">
<div class="icon-container success-icon"><i class="fa-solid fa-check"></i></div>
<h2>Unlocked!</h2>
<p class="success-text">Your file is ready.</p>
<p class="sub-info" style="margin-bottom: 20px;">If not redirected, tap below.</p>
<a href="#" id="fallbackBtn" class="btn btn-success" onclick="forceCloseAndRedirect()">
<i class="fa-brands fa-telegram"></i> Get My File
</a>
</div>
</div>
<style>
@keyframes pulse {{
0% {{ transform: scale(1); }}
50% {{ transform: scale(1.05); opacity: 0.8; }}
100% {{ transform: scale(1); }}
}}
</style>
<script>
const tg = window.Telegram.WebApp;
tg.expand();
tg.ready();
const botUrl = "https://t.me/{bot_username}?start={payload}";
function openAdAndStartTimer() {{
tg.openLink("{ad_link}");
document.getElementById("step1").style.display = "none";
document.getElementById("step2").style.display = "block";
let timeLeft = 15;
const totalTime = 15;
const progressBar = document.getElementById("progressBar");
const countdownText = document.getElementById("countdown");
let timerInterval = setInterval(function() {{
timeLeft--;
countdownText.innerText = timeLeft;
let percentage = ((totalTime - timeLeft) / totalTime) * 100;
progressBar.style.width = percentage + "%";
if (timeLeft <= 0) {{
clearInterval(timerInterval);
verifyAndSend();
}}
}}, 1000);
}}
function forceCloseAndRedirect() {{
window.location.href = botUrl;
setTimeout(() => {{ tg.close(); }}, 200);
}}
function verifyAndSend() {{
fetch('/api/verify', {{
method: 'POST',
headers: {{ 'Content-Type': 'application/json' }},
body: JSON.stringify({{ bot_id: {bot_id}, user_id: {user_id} }})
}})
.then(res => res.json())
.then(data => {{
if (data.success) {{
document.getElementById("step2").style.display = "none";
document.getElementById("step3").style.display = "block";
document.getElementById("fallbackBtn").href = botUrl;
setTimeout(() => {{
window.location.href = botUrl;
setTimeout(() => {{ tg.close(); }}, 500);
}}, 1500);
}} else {{
alert("Verification failed. Please try again.");
window.location.reload();
}}
}})
.catch(err => {{
alert("Network error. Please make sure you are connected to the internet.");
window.location.reload();
}});
}}
</script>
</body>
</html>
"""
return html_content
@app.route('/api/verify', methods=['POST'])
def api_verify():
try:
data = request.json
bot_id = int(data.get('bot_id'))
user_id = int(data.get('user_id'))
async def verify_user():
client = AsyncIOMotorClient(MONGO_URI)
db = client['FileStoreBot']
await db['clone_users'].update_one(
{"bot_id": bot_id, "user_id": user_id},
{"$set": {"last_ad_watched": time.time()}},
upsert=True
)
client.close()
asyncio.run(verify_user())
return jsonify({"success": True})
except Exception as e:
print(f"Verify API Error: {e}")
return jsonify({"success": False, "error": str(e)})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)