Spaces:
Running
Running
Download main.py from CuteStarOC01/MX01: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/spaces/CuteStarOC01/MX01/resolve/main/main.py
- Command line
-
hf download hf://spaces/CuteStarOC01/MX01/main.py
-
curl -L -o main.py https://huggingface.co/spaces/CuteStarOC01/MX01/resolve/main/main.py
3.33 kB
| import sys | |
| import asyncio | |
| import os | |
| _lock_handle = None | |
| # Cross-platform singleton check | |
| def prevent_multiple_instances(): | |
| lock_file = "bot.lock" | |
| if os.name == 'nt': | |
| # Windows approach: Check for existing lock file and try to delete it | |
| if os.path.exists(lock_file): | |
| try: | |
| os.remove(lock_file) | |
| except OSError: | |
| print("Error: The bot is already running. Please close the other instance first.") | |
| sys.exit(1) | |
| # Create and keep the file open until the process exits | |
| try: | |
| # Note: We don't close this handle so it stays locked by the OS | |
| global _lock_handle | |
| _lock_handle = open(lock_file, "w") | |
| except: pass | |
| else: | |
| # Linux/macOS approach: fcntl | |
| try: | |
| import fcntl | |
| f = open(lock_file, "w") | |
| fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
| except (ImportError, IOError): | |
| # Check if it was an import error (Windows case should be handled by os.name == 'nt') | |
| # If we are on Linux and fcntl is missing, we might have a problem, but usually it's there. | |
| pass | |
| prevent_multiple_instances() | |
| # Install uvloop FIRST before any event loop is created (must come before pyrogram) | |
| if sys.platform != 'win32': | |
| try: | |
| import uvloop | |
| asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | |
| uvloop.install() | |
| except ImportError: | |
| pass | |
| # Start dummy web server instantly to satisfy Render's health checks | |
| import os | |
| import threading | |
| from app import app | |
| import logging | |
| PORT = int(os.environ.get("PORT", "7860")) | |
| WEBS_HOST = os.environ.get("WEBS_HOST", True) | |
| if WEBS_HOST: | |
| def start_web_server(): | |
| try: | |
| # Reduce flask logging noise | |
| log = logging.getLogger('werkzeug') | |
| log.setLevel(logging.ERROR) | |
| app.run(host='0.0.0.0', port=PORT, debug=False) | |
| except Exception as e: | |
| print(f"Flask server error: {e}") | |
| threading.Thread(target=start_web_server, daemon=True).start() | |
| # Keep-alive pinger to prevent HF Spaces / Render / Koyeb from sleeping | |
| from keep_alive import keep_alive | |
| keep_alive() | |
| # Create event loop AFTER uvloop policy is installed (required for Python 3.12+) | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| from Tools.auto import get_updates_manga, logger | |
| from Tools.my_token import expired_token_ | |
| from Tools.db import database | |
| import os, shutil | |
| from Tools.cworker import worker | |
| from bot import Bot, Vars | |
| folder_path = "Process" | |
| if os.path.exists(folder_path) and os.path.isdir(folder_path): | |
| shutil.rmtree(folder_path) | |
| async def main_exp_(): | |
| sleep_timeout = 1 # mins | |
| while True: | |
| try: | |
| await get_updates_manga() | |
| if Vars.SHORTENER: | |
| await database.remove_expired_users() | |
| expired_token_() | |
| except Exception as err: | |
| logger.exception(f"Error in main_exp_: {err}") | |
| finally: | |
| await asyncio.sleep(sleep_timeout * 60) | |
| async def worker_tasks(): | |
| tasks = [ | |
| asyncio.create_task(worker(i)) | |
| for i in range(1) | |
| ] | |
| tasks.append(asyncio.create_task(main_exp_())) | |
| await asyncio.gather(*tasks, return_exceptions=True) | |
| if __name__ == "__main__": | |
| loop.create_task(worker_tasks()) | |
| Bot.run() | |