-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (69 loc) · 2.64 KB
/
Copy pathmain.py
File metadata and controls
87 lines (69 loc) · 2.64 KB
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
import asyncio
import logging
import os
import subprocess
import threading
from pyrogram import filters, idle
from pyrogram.handlers import MessageHandler
import state
from clients import app, call_py, user_app
from config import COOKIES_FILE, DEPLOYED_OWNER_ID, YOUTUBE_COOKIES
from handlers.router import register_handlers
from handlers.system import active_bots_command, clone_command
from server import start_server
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - [KustMusic] - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
if not os.path.exists("downloads"):
os.makedirs("downloads")
if YOUTUBE_COOKIES:
with open(COOKIES_FILE, "w", encoding="utf-8") as _cf:
_cf.write(YOUTUBE_COOKIES)
logger.info(f"Cookies written to {COOKIES_FILE} from YOUTUBE_COOKIES env")
def _init_ejs_solver():
try:
result = subprocess.run(["deno", "--version"], capture_output=True, text=True)
if result.returncode == 0:
logger.info(f"Deno detected: {result.stdout.strip()}")
else:
logger.warning("Deno not found in PATH — yt-dlp JS challenges may fail")
subprocess.run(["yt-dlp", "--rm-cache-dir"], check=False, capture_output=True)
subprocess.run(
["yt-dlp", "--remote-components", "ejs:github",
"--simulate", "https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
check=False, capture_output=True,
)
logger.info("yt-dlp EJS solver initialized")
except Exception as e:
logger.warning(f"EJS solver init failed: {e}")
threading.Thread(target=_init_ejs_solver, daemon=True).start()
async def main():
logger.info("Starting KustMusic")
await start_server()
await app.start()
app.clone_owner = DEPLOYED_OWNER_ID
app.is_main = True
register_handlers(app)
app.add_handler(MessageHandler(clone_command, filters.command("clone") & filters.private))
app.add_handler(MessageHandler(active_bots_command, filters.command("active") & filters.private))
state.active_clients[app.me.id] = app
await user_app.start()
me = await user_app.get_me()
state.ASSISTANT_ID = me.id
state.ASSISTANT_USERNAME = me.username
logger.info(f"Assistant: {state.ASSISTANT_ID} (@{state.ASSISTANT_USERNAME})")
await call_py.start()
logger.info(f"Bot running: @{app.me.username}")
await idle()
await call_py.stop()
await user_app.stop()
for c in list(state.active_clients.values()):
try:
await c.stop()
except Exception:
pass
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())