-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (60 loc) · 2.54 KB
/
Copy pathmain.py
File metadata and controls
73 lines (60 loc) · 2.54 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
# main.py
import threading
import time
from dotenv import load_dotenv
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import BotCommand
from commands.start import start
from commands.help import help_command
from commands.download import download
from commands.music import music
from commands.stats import stats
from commands.auto_download import auto_download
from utils.cache import load_cache
from utils.token_loader import get_token
from config import CLEANUP_INTERVAL_HOURS
from utils.disk_manager import clear_downloads, cleanup_by_retention
from utils.logger import console_logger
load_dotenv(".env", override=False)
def scheduled_cleanup():
"""Thread de nettoyage périodique — respecte la rétention (mtimes)."""
interval_seconds = CLEANUP_INTERVAL_HOURS * 3600
console_logger.info(
f"[CLEANUP] Rotation planifiée activée — nettoyage toutes les {CLEANUP_INTERVAL_HOURS}h."
)
while True:
time.sleep(interval_seconds)
console_logger.info("[CLEANUP] Nettoyage périodique (rétention)...")
cleanup_by_retention()
console_logger.info("[CLEANUP] Nettoyage périodique terminé.")
def main():
console_logger.info("[INIT] Nettoyage du dossier downloads...")
clear_downloads()
load_cache()
token = get_token()
updater = Updater(token, use_context=True)
dp = updater.dispatcher
# Enregistrement des handlers pour les commandes du bot
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_command))
dp.add_handler(CommandHandler("download", download))
dp.add_handler(CommandHandler("music", music))
dp.add_handler(CommandHandler("stats", stats))
# Handler pour les messages contenant des liens (auto-download)
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, auto_download))
# Configuration du menu des commandes pour Telegram
bot = updater.bot
bot.set_my_commands([
BotCommand("start", "Pour commencer"),
BotCommand("help", "Pour obtenir de l'aide"),
BotCommand("download", "Télécharger une vidéo avec yt-dlp"),
])
console_logger.info("[INIT] Menu des commandes configuré.")
# Démarrage du thread de nettoyage périodique (daemon = s'arrête avec le bot)
cleanup_thread = threading.Thread(target=scheduled_cleanup, daemon=True)
cleanup_thread.start()
console_logger.info("[INIT] Démarrage du bot et lancement du polling...")
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()