File size: 5,660 Bytes
35e8395 86f1c25 4c0e136 cf57358 35e8395 e67347b 35e8395 e67347b 35e8395 e67347b 35e8395 e67347b 35e8395 cf57358 050e922 86f1c25 35e8395 4f0eebb d2556c3 dda777d d2556c3 cdcc975 dda777d d2556c3 e67347b dda777d 86f1c25 050e922 86f1c25 35e8395 cf57358 d2556c3 4c0e136 35e8395 050e922 35e8395 cf57358 35e8395 cf57358 35e8395 050e922 35e8395 cf57358 35e8395 cf57358 35e8395 cf57358 35e8395 cf57358 35e8395 cf57358 35e8395 cf57358 97dc0cf 35e8395 cf57358 35e8395 050e922 cf57358 35e8395 cf57358 35e8395 8a77e1d 35e8395 cf57358 35e8395 97dc0cf 35e8395 80e98ea 97dc0cf 86f1c25 97dc0cf 35e8395 cf57358 35e8395 97dc0cf 35e8395 97dc0cf d2556c3 cf57358 86f1c25 cf57358 35e8395 cf57358 35e8395 050e922 35e8395 cf57358 35e8395 cf57358 35e8395 97dc0cf 35e8395 97dc0cf 35e8395 86f1c25 cf57358 35e8395 cf57358 35e8395 86f1c25 cf57358 35e8395 86f1c25 cf57358 35e8395 cf57358 35e8395 86f1c25 35e8395 97dc0cf 86f1c25 e67347b 86f1c25 cf57358 35e8395 cf57358 86f1c25 cf57358 35e8395 86f1c25 e2202d4 35e8395 e67347b 35e8395 97dc0cf 35e8395 cf57358 35e8395 97dc0cf 050e922 e67347b 86f1c25 050e922 86f1c25 35e8395 e67347b 35e8395 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | import os
import tempfile
import shutil
import threading
import time
import requests
from flask import Flask
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from huggingface_hub import HfApi, hf_hub_download
# =========================
# CONFIG
# =========================
API_ID = int(os.environ["API_ID"])
API_HASH = os.environ["API_HASH"]
BOT_TOKEN = os.environ["BOT_TOKEN"]
HF_TOKEN = os.environ["HF_TOKEN"]
DATASET_REPO = "Cxops/Bzxv"
# ✅ FIXED: correct Space runtime URL (IMPORTANT)
KEEP_ALIVE_URL = "https://cxops-push.hf.space/"
PING_INTERVAL = 300 # 5 minutes
# =========================
# FLASK HEALTH SERVER
# =========================
web = Flask(__name__)
@web.route("/")
def home():
return "Bot Running..."
def run_web():
web.run(host="0.0.0.0", port=7860)
# =========================
# KEEP ALIVE PINGER
# =========================
def keep_alive():
headers = {
"User-Agent": "Mozilla/5.0 (KeepAliveBot)"
}
while True:
try:
r = requests.get(KEEP_ALIVE_URL, headers=headers, timeout=20)
print(f"[KEEPALIVE] Pinged {KEEP_ALIVE_URL} | Status: {r.status_code}")
except Exception as e:
print(f"[KEEPALIVE ERROR] {e}")
time.sleep(PING_INTERVAL)
# =========================
# HUGGINGFACE
# =========================
hf = HfApi(token=HF_TOKEN)
# =========================
# PYROGRAM BOT
# =========================
app = Client(
"storage_bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN
)
# =========================
# CLEANUP HELPER
# =========================
def cleanup(path):
try:
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)
elif os.path.isfile(path):
os.remove(path)
except Exception:
pass
# =========================
# COMMANDS
# =========================
@app.on_message(filters.command("start"))
async def start(_, message):
await message.reply_text(
"Send me a file.\n\n"
"/files - List uploaded files"
)
@app.on_message(filters.command("files"))
async def files(_, message):
user_id = str(message.from_user.id)
try:
repo_files = hf.list_repo_files(
repo_id=DATASET_REPO,
repo_type="dataset"
)
user_files = [
f for f in repo_files
if f.startswith(f"{user_id}/")
]
if not user_files:
return await message.reply_text("You have no uploaded files.")
buttons = [
[
InlineKeyboardButton(
os.path.basename(path)[:60],
callback_data=f"get:{path}"
)
]
for path in user_files
]
await message.reply_text(
"Your uploaded files:",
reply_markup=InlineKeyboardMarkup(buttons)
)
except Exception as e:
await message.reply_text(f"Error:\n{e}")
# =========================
# DOWNLOAD FROM HF
# =========================
@app.on_callback_query(filters.regex("^get:"))
async def get_file(_, query):
path = query.data.replace("get:", "")
temp_dir = tempfile.mkdtemp()
try:
await query.message.edit_text("Downloading from dataset...")
local_file = hf_hub_download(
repo_id=DATASET_REPO,
repo_type="dataset",
filename=path,
token=HF_TOKEN,
local_dir=temp_dir
)
await query.message.reply_document(
local_file,
caption=os.path.basename(path)
)
try:
await query.message.delete()
except Exception:
pass
except Exception as e:
await query.message.reply_text(f"Error:\n{e}")
finally:
cleanup(temp_dir)
# =========================
# FILE UPLOAD HANDLER
# =========================
@app.on_message(
filters.document |
filters.video |
filters.audio |
filters.photo
)
async def upload_file(_, message):
temp_dir = tempfile.mkdtemp()
try:
status = await message.reply_text("Downloading file...")
if message.document:
media = message.document
filename = media.file_name
elif message.video:
media = message.video
filename = media.file_name or f"{media.file_unique_id}.mp4"
elif message.audio:
media = message.audio
filename = media.file_name or f"{media.file_unique_id}.mp3"
else:
media = message.photo[-1]
filename = f"{media.file_unique_id}.jpg"
local_path = await message.download(
file_name=os.path.join(temp_dir, filename)
)
await status.edit_text("Uploading to Hugging Face...")
repo_path = f"{message.from_user.id}/{filename}"
hf.upload_file(
path_or_fileobj=local_path,
path_in_repo=repo_path,
repo_id=DATASET_REPO,
repo_type="dataset"
)
await status.edit_text(f"Uploaded successfully.\n\n{filename}")
except Exception as e:
await message.reply_text(f"Upload failed:\n{e}")
finally:
cleanup(temp_dir)
# =========================
# MAIN
# =========================
if __name__ == "__main__":
print("Starting Flask health server...")
threading.Thread(target=run_web, daemon=True).start()
print("Starting Keep-Alive Pinger...")
threading.Thread(target=keep_alive, daemon=True).start()
print("Starting Telegram bot...")
app.run() |