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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
async def main():
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
f.unlink()
|
| 23 |
-
print("Deleted old session:", f)
|
| 24 |
await app.start()
|
| 25 |
-
print("Bot
|
| 26 |
-
await
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import asyncio
|
| 4 |
+
import hashlib
|
| 5 |
+
import sqlite3
|
| 6 |
+
import shutil
|
| 7 |
+
import aiohttp
|
| 8 |
|
| 9 |
+
from pyrogram import Client, filters, idle
|
| 10 |
+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 11 |
+
from huggingface_hub import HfApi
|
| 12 |
|
| 13 |
+
# ================= CONFIG =================
|
| 14 |
+
|
| 15 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 16 |
+
API_ID = int(os.getenv("API_ID"))
|
| 17 |
+
API_HASH = os.getenv("API_HASH")
|
| 18 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 19 |
+
|
| 20 |
+
DATASET_REPO = "Cxops/Bzxv"
|
| 21 |
+
|
| 22 |
+
SPACE_URL = "https://cxops-push.hf.space"
|
| 23 |
+
|
| 24 |
+
BASE_DIR = "/data/files"
|
| 25 |
+
DB_PATH = "/data/map.db"
|
| 26 |
+
|
| 27 |
+
DISK_LIMIT_GB = 45
|
| 28 |
+
|
| 29 |
+
os.makedirs(BASE_DIR, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
app = Client("hf_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
|
| 32 |
+
hf = HfApi(token=HF_TOKEN)
|
| 33 |
+
|
| 34 |
+
# ================= DB =================
|
| 35 |
+
|
| 36 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 37 |
+
cur = conn.cursor()
|
| 38 |
+
|
| 39 |
+
cur.execute("""
|
| 40 |
+
CREATE TABLE IF NOT EXISTS files (
|
| 41 |
+
user_id INTEGER,
|
| 42 |
+
username TEXT,
|
| 43 |
+
folder TEXT,
|
| 44 |
+
real_name TEXT,
|
| 45 |
+
fake_name TEXT
|
| 46 |
+
)
|
| 47 |
+
""")
|
| 48 |
+
conn.commit()
|
| 49 |
+
|
| 50 |
+
# ================= UTIL =================
|
| 51 |
+
|
| 52 |
+
def hash_id(text: str):
|
| 53 |
+
return hashlib.sha256(text.encode()).hexdigest()[:18]
|
| 54 |
+
|
| 55 |
+
def get_folder(user_id, username):
|
| 56 |
+
return hash_id(f"{user_id}:{username}")
|
| 57 |
+
|
| 58 |
+
def disk_ok():
|
| 59 |
+
total, used, free = shutil.disk_usage("/data")
|
| 60 |
+
return free > (DISK_LIMIT_GB * 1024**3)
|
| 61 |
+
|
| 62 |
+
def user_path(folder):
|
| 63 |
+
path = os.path.join(BASE_DIR, folder)
|
| 64 |
+
os.makedirs(path, exist_ok=True)
|
| 65 |
+
return path
|
| 66 |
+
|
| 67 |
+
# ================= KEEPALIVE =================
|
| 68 |
+
|
| 69 |
+
async def keepalive():
|
| 70 |
+
while True:
|
| 71 |
+
try:
|
| 72 |
+
async with aiohttp.ClientSession() as session:
|
| 73 |
+
await session.get(SPACE_URL)
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
await asyncio.sleep(300)
|
| 77 |
+
|
| 78 |
+
# ================= HF UPLOAD =================
|
| 79 |
+
|
| 80 |
+
async def upload_hf(local_file, remote_path):
|
| 81 |
+
loop = asyncio.get_event_loop()
|
| 82 |
+
await loop.run_in_executor(
|
| 83 |
+
None,
|
| 84 |
+
lambda: hf.upload_file(
|
| 85 |
+
path_or_fileobj=local_file,
|
| 86 |
+
path_in_repo=remote_path,
|
| 87 |
+
repo_id=DATASET_REPO,
|
| 88 |
+
repo_type="dataset"
|
| 89 |
+
)
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# ================= QUEUE =================
|
| 93 |
+
|
| 94 |
+
queue = asyncio.Queue()
|
| 95 |
+
|
| 96 |
+
async def worker():
|
| 97 |
+
while True:
|
| 98 |
+
job = await queue.get()
|
| 99 |
+
|
| 100 |
+
user_id = job["user_id"]
|
| 101 |
+
folder = job["folder"]
|
| 102 |
+
file_id = job["file_id"]
|
| 103 |
+
msg = job["msg"]
|
| 104 |
+
|
| 105 |
+
if not disk_ok():
|
| 106 |
+
await msg.reply("β Server busy (disk full). Try later.")
|
| 107 |
+
continue
|
| 108 |
+
|
| 109 |
+
status = await msg.reply("β¬ Downloading...")
|
| 110 |
+
|
| 111 |
+
local_dir = user_path(folder)
|
| 112 |
+
local_file = os.path.join(local_dir, job["fake"])
|
| 113 |
+
|
| 114 |
+
path = await app.download_media(file_id, file_name=local_file)
|
| 115 |
+
|
| 116 |
+
await status.edit("β Uploading to HuggingFace...")
|
| 117 |
+
|
| 118 |
+
remote_path = f"{folder}/{job['fake']}"
|
| 119 |
+
await upload_hf(path, remote_path)
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
os.remove(path)
|
| 123 |
+
except:
|
| 124 |
+
pass
|
| 125 |
+
|
| 126 |
+
await status.edit("β
Done & cleaned")
|
| 127 |
+
|
| 128 |
+
# ================= HANDLERS =================
|
| 129 |
+
|
| 130 |
+
pending = {}
|
| 131 |
|
| 132 |
@app.on_message(filters.command("start"))
|
| 133 |
async def start(_, msg):
|
| 134 |
+
await msg.reply("Send me files. I will store them safely in dataset.")
|
| 135 |
+
|
| 136 |
+
@app.on_message(filters.document | filters.video | filters.audio)
|
| 137 |
+
async def handle(_, msg):
|
| 138 |
+
user = msg.from_user
|
| 139 |
+
folder = get_folder(user.id, user.username or str(user.id))
|
| 140 |
+
|
| 141 |
+
file_id = msg.document.file_id if msg.document else (
|
| 142 |
+
msg.video.file_id if msg.video else msg.audio.file_id
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
fake = hash_id(file_id)
|
| 146 |
+
|
| 147 |
+
pending[user.id] = {
|
| 148 |
+
"file_id": file_id,
|
| 149 |
+
"folder": folder,
|
| 150 |
+
"fake": fake,
|
| 151 |
+
"msg": msg
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
kb = InlineKeyboardMarkup([
|
| 155 |
+
[
|
| 156 |
+
InlineKeyboardButton("β¬ Upload All", callback_data="up"),
|
| 157 |
+
InlineKeyboardButton("β Cancel", callback_data="no")
|
| 158 |
+
]
|
| 159 |
+
])
|
| 160 |
+
|
| 161 |
+
await msg.reply("File received. Upload?", reply_markup=kb)
|
| 162 |
+
|
| 163 |
+
@app.on_callback_query()
|
| 164 |
+
async def cb(_, q):
|
| 165 |
+
uid = q.from_user.id
|
| 166 |
+
|
| 167 |
+
if q.data == "no":
|
| 168 |
+
pending.pop(uid, None)
|
| 169 |
+
return await q.message.edit("Cancelled")
|
| 170 |
+
|
| 171 |
+
if q.data == "up":
|
| 172 |
+
if uid in pending:
|
| 173 |
+
await queue.put({
|
| 174 |
+
**pending[uid],
|
| 175 |
+
"user_id": uid
|
| 176 |
+
})
|
| 177 |
+
pending.pop(uid)
|
| 178 |
+
await q.message.edit("Queued β")
|
| 179 |
+
|
| 180 |
+
@app.on_message(filters.command("files"))
|
| 181 |
+
async def list_files(_, msg):
|
| 182 |
+
user = msg.from_user
|
| 183 |
+
folder = get_folder(user.id, user.username or str(user.id))
|
| 184 |
|
| 185 |
+
path = user_path(folder)
|
| 186 |
+
files = os.listdir(path)
|
| 187 |
+
|
| 188 |
+
if not files:
|
| 189 |
+
return await msg.reply("No files.")
|
| 190 |
+
|
| 191 |
+
kb = [[InlineKeyboardButton(f, callback_data=f"get:{f}")] for f in files]
|
| 192 |
+
|
| 193 |
+
await msg.reply("Your files:", reply_markup=InlineKeyboardMarkup(kb))
|
| 194 |
+
|
| 195 |
+
@app.on_callback_query(filters.regex("get:"))
|
| 196 |
+
async def send_file(_, q):
|
| 197 |
+
user = q.from_user
|
| 198 |
+
folder = get_folder(user.id, user.username or str(user.id))
|
| 199 |
+
|
| 200 |
+
fname = q.data.split(":")[1]
|
| 201 |
+
path = os.path.join(BASE_DIR, folder, fname)
|
| 202 |
+
|
| 203 |
+
if not os.path.exists(path):
|
| 204 |
+
return await q.message.reply("Missing file")
|
| 205 |
+
|
| 206 |
+
size = os.path.getsize(path)
|
| 207 |
+
limit = 2 * 1024 * 1024 * 1024
|
| 208 |
+
|
| 209 |
+
await q.message.reply("Uploading back...")
|
| 210 |
+
|
| 211 |
+
if size <= limit:
|
| 212 |
+
await app.send_document(q.message.chat.id, path)
|
| 213 |
+
os.remove(path)
|
| 214 |
+
return
|
| 215 |
+
|
| 216 |
+
part = 0
|
| 217 |
+
with open(path, "rb") as f:
|
| 218 |
+
while chunk := f.read(limit):
|
| 219 |
+
part_file = f"{path}.part{part}"
|
| 220 |
+
with open(part_file, "wb") as pf:
|
| 221 |
+
pf.write(chunk)
|
| 222 |
+
|
| 223 |
+
await app.send_document(q.message.chat.id, part_file)
|
| 224 |
+
os.remove(part_file)
|
| 225 |
+
part += 1
|
| 226 |
+
|
| 227 |
+
os.remove(path)
|
| 228 |
+
|
| 229 |
+
# ================= MAIN =================
|
| 230 |
|
| 231 |
async def main():
|
| 232 |
+
asyncio.create_task(worker())
|
| 233 |
+
asyncio.create_task(keepalive())
|
| 234 |
+
|
|
|
|
|
|
|
| 235 |
await app.start()
|
| 236 |
+
print("Bot running")
|
| 237 |
+
await idle()
|
| 238 |
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
+
asyncio.run(main())
|