mirror of
https://github.com/Arrowar/StreamingCommunity.git
synced 2025-06-06 11:35:29 +00:00
stostato il file dentro la cartella HelpTg
This commit is contained in:
parent
0a4df59126
commit
80aefb4fba
3
.gitignore
vendored
3
.gitignore
vendored
@ -49,4 +49,5 @@ note.txt
|
||||
list_proxy.txt
|
||||
cmd.txt
|
||||
bot_config.json
|
||||
script.json
|
||||
script.json
|
||||
active_requests.json
|
@ -12,7 +12,7 @@ from .site import title_search, run_get_select_title, media_search_manager
|
||||
from .title import download_title
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
import sys
|
||||
|
@ -18,7 +18,7 @@ from StreamingCommunity.Api.Template.Util import search_domain
|
||||
from StreamingCommunity.Api.Template.Class.SearchType import MediaManager
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
||||
|
@ -24,7 +24,7 @@ from StreamingCommunity.Api.Template.Class.SearchType import MediaItem
|
||||
from .costant import DOMAIN_NOW, SITE_NAME, MOVIE_FOLDER
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from session import get_session, updateScriptId, deleteScriptId
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
@ -12,7 +12,7 @@ from .site import title_search, run_get_select_title, media_search_manager
|
||||
from .film import download_film
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
@ -25,7 +25,7 @@ from StreamingCommunity.Api.Player.supervideo import VideoSource
|
||||
from .costant import MOVIE_FOLDER
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from session import get_session, updateScriptId, deleteScriptId
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
@ -26,7 +26,7 @@ max_timeout = config_manager.get_int("REQUESTS", "timeout")
|
||||
disable_searchDomain = config_manager.get_bool("DEFAULT", "disable_searchDomain")
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
@ -12,7 +12,7 @@ from .site import title_search, run_get_select_title, media_search_manager
|
||||
from .film_serie import download_film, download_series
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
import sys
|
||||
|
@ -27,7 +27,7 @@ from .costant import SITE_NAME, ANIME_FOLDER, MOVIE_FOLDER
|
||||
KILL_HANDLER = bool(False)
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from session import get_session, updateScriptId, deleteScriptId
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
@ -20,7 +20,7 @@ from StreamingCommunity.Api.Template.Util import search_domain
|
||||
from StreamingCommunity.Api.Template.Class.SearchType import MediaManager
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
@ -15,7 +15,7 @@ from .series import download_series
|
||||
|
||||
# Telegram bot instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
# Variable
|
||||
|
@ -25,7 +25,7 @@ from StreamingCommunity.Api.Player.vixcloud import VideoSource
|
||||
from .costant import SITE_NAME, MOVIE_FOLDER
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from session import get_session, updateScriptId, deleteScriptId
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
@ -27,7 +27,7 @@ from StreamingCommunity.Api.Player.vixcloud import VideoSource
|
||||
from .costant import SITE_NAME, SERIES_FOLDER
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from session import get_session, updateScriptId, deleteScriptId
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
@ -28,7 +28,7 @@ from StreamingCommunity.Api.Template.Class.SearchType import MediaManager
|
||||
from .costant import SITE_NAME, DOMAIN_NOW
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
# Variable
|
||||
|
79
StreamingCommunity/HelpTg/request_manager.py
Normal file
79
StreamingCommunity/HelpTg/request_manager.py
Normal file
@ -0,0 +1,79 @@
|
||||
import json
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
class RequestManager:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if not cls._instance:
|
||||
cls._instance = super().__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self, json_file: str = "active_requests.json"):
|
||||
if not hasattr(self, 'initialized'):
|
||||
self.json_file = json_file
|
||||
self.initialized = True
|
||||
self.on_response_callback = None # Aggiungi un campo per il callback
|
||||
|
||||
def create_request(self, type: str) -> str:
|
||||
request_data = {
|
||||
"type": type,
|
||||
"response": None,
|
||||
"timestamp": time.time()
|
||||
}
|
||||
|
||||
# Aggiungi il tipo al salvataggio della richiesta
|
||||
with open(self.json_file, "w") as f:
|
||||
json.dump(request_data, f)
|
||||
|
||||
return "Ok"
|
||||
|
||||
def save_response(self, message_text: str) -> bool:
|
||||
try:
|
||||
# Carica il file JSON
|
||||
with open(self.json_file, "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Controlla se esiste la chiave 'type' e se la risposta è presente
|
||||
if "type" in data and "response" in data:
|
||||
data["response"] = message_text # Aggiorna la risposta
|
||||
|
||||
# Scrivi il file JSON aggiornato
|
||||
with open(self.json_file, "w") as f:
|
||||
json.dump(data, f, indent=4) # Formatta il file JSON
|
||||
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
except (FileNotFoundError, json.JSONDecodeError) as e:
|
||||
print(f"⚠️ save_response - errore: {e}")
|
||||
return False
|
||||
|
||||
def get_response(self) -> Optional[str]:
|
||||
try:
|
||||
with open(self.json_file, "r") as f:
|
||||
data = json.load(f)
|
||||
# Verifica se esiste la chiave "response"
|
||||
if "response" in data:
|
||||
response = data["response"] # Ottieni la risposta direttamente
|
||||
if response is not None and self.on_response_callback:
|
||||
# Se la risposta è disponibile, chiama il callback
|
||||
self.on_response_callback(response)
|
||||
return response
|
||||
|
||||
except (FileNotFoundError, json.JSONDecodeError) as e:
|
||||
print(f"get_response - errore: {e}")
|
||||
return None
|
||||
|
||||
def clear_file(self) -> bool:
|
||||
try:
|
||||
# Svuota il file JSON scrivendo un oggetto vuoto
|
||||
with open(self.json_file, "w") as f:
|
||||
json.dump({}, f)
|
||||
print(f"File {self.json_file} è stato svuotato con successo.")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"⚠️ clear_file - errore: {e}")
|
||||
return False
|
62
StreamingCommunity/HelpTg/session.py
Executable file
62
StreamingCommunity/HelpTg/session.py
Executable file
@ -0,0 +1,62 @@
|
||||
import json
|
||||
|
||||
session_data = {}
|
||||
|
||||
def set_session(value):
|
||||
# salvo script_id in session_data
|
||||
session_data['script_id'] = value
|
||||
|
||||
def get_session():
|
||||
# controllo se script_id è presente in session_data
|
||||
return session_data.get('script_id', 'unknown')
|
||||
|
||||
def updateScriptId(screen_id, titolo):
|
||||
# definisco il nome del file json
|
||||
json_file = "scripts.json"
|
||||
try:
|
||||
# apro il file json
|
||||
with open(json_file, 'r') as f:
|
||||
scripts_data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
# Se il file non esiste, inizializzo la lista vuota
|
||||
scripts_data = []
|
||||
|
||||
# cerco lo script con lo screen_id
|
||||
for script in scripts_data:
|
||||
if script["screen_id"] == screen_id:
|
||||
# se trovo il match, aggiorno il titolo
|
||||
script["titolo"] = titolo
|
||||
# aggiorno il file json
|
||||
with open(json_file, 'w') as f:
|
||||
json.dump(scripts_data, f, indent=4)
|
||||
#print(f"Titolo aggiornato per screen_id {screen_id}")
|
||||
return
|
||||
|
||||
# se non trovo nessuno script con lo screen_id
|
||||
print(f"Screen_id {screen_id} non trovato.")
|
||||
|
||||
# creo la funzione che elimina lo script con lo screen_id specificato
|
||||
def deleteScriptId(screen_id):
|
||||
# definisco il nome del file json
|
||||
json_file = "scripts.json"
|
||||
try:
|
||||
# apro il file json
|
||||
with open(json_file, 'r') as f:
|
||||
scripts_data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
# Se il file non esiste, inizializzo la lista vuota
|
||||
scripts_data = []
|
||||
|
||||
# cerco lo script con lo screen_id
|
||||
for script in scripts_data:
|
||||
if script["screen_id"] == screen_id:
|
||||
# se trovo il match, elimino lo script
|
||||
scripts_data.remove(script)
|
||||
# aggiorno il file json
|
||||
with open(json_file, 'w') as f:
|
||||
json.dump(scripts_data, f, indent=4)
|
||||
print(f"Script eliminato per screen_id {screen_id}")
|
||||
return
|
||||
|
||||
# se non trovo nessuno script con lo screen_id
|
||||
print(f"Screen_id {screen_id} non trovato.")
|
537
StreamingCommunity/HelpTg/telegram_bot.py
Normal file
537
StreamingCommunity/HelpTg/telegram_bot.py
Normal file
@ -0,0 +1,537 @@
|
||||
import telebot
|
||||
from telebot import types
|
||||
import time
|
||||
import uuid
|
||||
import subprocess
|
||||
import os, re, sys
|
||||
import json
|
||||
from request_manager import RequestManager
|
||||
import threading
|
||||
|
||||
# Funzione per caricare variabili da un file .env
|
||||
def load_env(file_path=".env"):
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path) as f:
|
||||
for line in f:
|
||||
if line.strip() and not line.startswith("#"):
|
||||
key, value = line.strip().split("=", 1)
|
||||
os.environ[key] = value
|
||||
|
||||
# Carica le variabili
|
||||
load_env()
|
||||
|
||||
class TelegramBot:
|
||||
_instance = None
|
||||
_config_file = "bot_config.json"
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
if cls._instance is None:
|
||||
# Prova a caricare la configurazione e inizializzare il bot
|
||||
if os.path.exists(cls._config_file):
|
||||
with open(cls._config_file, 'r') as f:
|
||||
config = json.load(f)
|
||||
cls._instance = cls.init_bot(config['token'], config['authorized_user_id'])
|
||||
else:
|
||||
raise Exception("Bot non ancora inizializzato. Chiamare prima init_bot() con token e authorized_user_id")
|
||||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
def init_bot(cls, token, authorized_user_id):
|
||||
if cls._instance is None:
|
||||
cls._instance = cls(token, authorized_user_id)
|
||||
# Salva la configurazione
|
||||
config = {
|
||||
'token': token,
|
||||
'authorized_user_id': authorized_user_id
|
||||
}
|
||||
with open(cls._config_file, 'w') as f:
|
||||
json.dump(config, f)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self, token, authorized_user_id):
|
||||
|
||||
def monitor_scripts():
|
||||
while True:
|
||||
try:
|
||||
with open("scripts.json", "r") as f:
|
||||
scripts_data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
scripts_data = []
|
||||
|
||||
current_time = time.time()
|
||||
|
||||
# Crea una nuova lista senza gli script che sono scaduti
|
||||
scripts_data_to_save = []
|
||||
|
||||
for script in scripts_data:
|
||||
if "titolo" not in script and script["status"] == "running" and (current_time - script["start_time"]) > 600:
|
||||
# Prova a terminare la sessione screen
|
||||
try:
|
||||
subprocess.check_output(["screen", "-S", script["screen_id"], "-X", "quit"])
|
||||
print(f"✅ La sessione screen con ID {script['screen_id']} è stata fermata automaticamente.")
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"⚠️ Impossibile fermare la sessione screen con ID {script['screen_id']}.")
|
||||
|
||||
# Aggiungi solo gli script che non sono scaduti
|
||||
print(f"⚠️ Lo script con ID {script['screen_id']} ha superato i 10 minuti e verrà rimosso.")
|
||||
else:
|
||||
scripts_data_to_save.append(script)
|
||||
|
||||
# Salva la lista aggiornata, senza gli script scaduti
|
||||
with open("scripts.json", "w") as f:
|
||||
json.dump(scripts_data_to_save, f, indent=4)
|
||||
|
||||
time.sleep(60) # Controlla ogni minuto
|
||||
|
||||
# Avvia il thread di monitoraggio
|
||||
monitor_thread = threading.Thread(target=monitor_scripts, daemon=True)
|
||||
monitor_thread.start()
|
||||
|
||||
|
||||
if TelegramBot._instance is not None:
|
||||
raise Exception("Questa classe è un singleton! Usa get_instance() per ottenere l'istanza.")
|
||||
|
||||
self.token = token
|
||||
self.authorized_user_id = authorized_user_id
|
||||
self.chat_id = authorized_user_id
|
||||
self.bot = telebot.TeleBot(token)
|
||||
self.request_manager = RequestManager()
|
||||
|
||||
# Registra gli handler
|
||||
self.register_handlers()
|
||||
|
||||
def register_handlers(self):
|
||||
|
||||
""" @self.bot.message_handler(commands=['start'])
|
||||
def start(message):
|
||||
self.handle_start(message) """
|
||||
|
||||
@self.bot.message_handler(commands=['get_id'])
|
||||
def get_id(message):
|
||||
self.handle_get_id(message)
|
||||
|
||||
@self.bot.message_handler(commands=['start'])
|
||||
def start_script(message):
|
||||
self.handle_start_script(message)
|
||||
|
||||
@self.bot.message_handler(commands=['list'])
|
||||
def list_scripts(message):
|
||||
self.handle_list_scripts(message)
|
||||
|
||||
@self.bot.message_handler(commands=['stop'])
|
||||
def stop_script(message):
|
||||
self.handle_stop_script(message)
|
||||
|
||||
@self.bot.message_handler(commands=['screen'])
|
||||
def screen_status(message):
|
||||
self.handle_screen_status(message)
|
||||
|
||||
""" @self.bot.message_handler(commands=['replay'])
|
||||
def send_welcome(message):
|
||||
# Crea una tastiera personalizzata
|
||||
markup = types.ReplyKeyboardMarkup(row_width=2)
|
||||
itembtn1 = types.KeyboardButton('Start')
|
||||
itembtn2 = types.KeyboardButton('Lista')
|
||||
markup.add(itembtn1, itembtn2)
|
||||
|
||||
# Invia un messaggio con la tastiera
|
||||
self.bot.send_message(message.chat.id, "Scegli un'opzione:", reply_markup=markup) """
|
||||
|
||||
"""@self.bot.message_handler(commands=['inline'])
|
||||
def send_welcome(message):
|
||||
# Crea una tastiera inline
|
||||
markup = types.InlineKeyboardMarkup()
|
||||
itembtn1 = types.InlineKeyboardButton('Azione 1', callback_data='action1')
|
||||
itembtn2 = types.InlineKeyboardButton('Azione 2', callback_data='action2')
|
||||
itembtn3 = types.InlineKeyboardButton('Azione 3', callback_data='action3')
|
||||
markup.add(itembtn1, itembtn2, itembtn3)
|
||||
|
||||
# Invia un messaggio con la tastiera inline
|
||||
self.bot.send_message(message.chat.id, "Scegli un'opzione:", reply_markup=markup)
|
||||
|
||||
# Gestisce le callback delle tastiere inline
|
||||
@self.bot.callback_query_handler(func=lambda call: True)
|
||||
def handle_callback(call):
|
||||
if call.data == 'action1':
|
||||
self.bot.answer_callback_query(call.id, "Hai scelto Azione 1!")
|
||||
self.bot.send_message(call.message.chat.id, "Hai eseguito Azione 1!")
|
||||
elif call.data == 'action2':
|
||||
self.bot.answer_callback_query(call.id, "Hai scelto Azione 2!")
|
||||
self.bot.send_message(call.message.chat.id, "Hai eseguito Azione 2!")
|
||||
elif call.data == 'action3':
|
||||
self.bot.answer_callback_query(call.id, "Hai scelto Azione 3!")
|
||||
self.bot.send_message(call.message.chat.id, "Hai eseguito Azione 3!")
|
||||
"""
|
||||
|
||||
@self.bot.message_handler(func=lambda message: True)
|
||||
def handle_all_messages(message):
|
||||
self.handle_response(message)
|
||||
|
||||
def is_authorized(self, user_id):
|
||||
return user_id == self.authorized_user_id
|
||||
|
||||
def handle_get_id(self, message):
|
||||
if not self.is_authorized(message.from_user.id):
|
||||
print(f"❌ Non sei autorizzato.")
|
||||
self.bot.send_message(message.chat.id, "❌ Non sei autorizzato.")
|
||||
return
|
||||
|
||||
print(f"Il tuo ID utente è: `{message.from_user.id}`")
|
||||
self.bot.send_message(message.chat.id, f"Il tuo ID utente è: `{message.from_user.id}`", parse_mode="Markdown")
|
||||
|
||||
def handle_start_script(self, message):
|
||||
if not self.is_authorized(message.from_user.id):
|
||||
print(f"❌ Non sei autorizzato. {message.from_user.id}")
|
||||
self.bot.send_message(message.chat.id, "❌ Non sei autorizzato.")
|
||||
return
|
||||
|
||||
screen_id = str(uuid.uuid4())[:8]
|
||||
#screen_id = '0000'
|
||||
|
||||
# Impostare a True per avviare il test_run.py in modalità verbose per il debug
|
||||
|
||||
debug_mode = os.getenv("DEBUG")
|
||||
verbose = debug_mode
|
||||
|
||||
if debug_mode == "True":
|
||||
subprocess.Popen(["python3", "test_run.py", screen_id])
|
||||
else:
|
||||
# Verifica se lo screen con il nome esiste già
|
||||
try:
|
||||
subprocess.check_output(["screen", "-list"])
|
||||
existing_screens = subprocess.check_output(["screen", "-list"]).decode('utf-8')
|
||||
if screen_id in existing_screens:
|
||||
print(f"⚠️ Lo script con ID {screen_id} è già in esecuzione.")
|
||||
self.bot.send_message(message.chat.id, f"⚠️ Lo script con ID {screen_id} è già in esecuzione.")
|
||||
return
|
||||
except subprocess.CalledProcessError:
|
||||
pass # Se il comando fallisce, significa che non ci sono screen attivi.
|
||||
|
||||
# Crea la sessione screen e avvia lo script al suo interno
|
||||
command = ["screen", "-dmS", screen_id, "python3", "test_run.py", screen_id]
|
||||
|
||||
# Avvia il comando tramite subprocess
|
||||
subprocess.Popen(command)
|
||||
|
||||
# Creazione oggetto script info
|
||||
script_info = {
|
||||
"screen_id": screen_id,
|
||||
"start_time": time.time(),
|
||||
"status": "running",
|
||||
"user_id": message.from_user.id
|
||||
}
|
||||
|
||||
# Salvataggio nel file JSON
|
||||
json_file = "scripts.json"
|
||||
|
||||
# Carica i dati esistenti o crea una nuova lista
|
||||
try:
|
||||
with open(json_file, 'r') as f:
|
||||
scripts_data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
scripts_data = []
|
||||
|
||||
# Aggiungi il nuovo script
|
||||
scripts_data.append(script_info)
|
||||
|
||||
# Scrivi il file aggiornato
|
||||
with open(json_file, 'w') as f:
|
||||
json.dump(scripts_data, f, indent=4)
|
||||
|
||||
def handle_list_scripts(self, message):
|
||||
if not self.is_authorized(message.from_user.id):
|
||||
print(f"❌ Non sei autorizzato.")
|
||||
self.bot.send_message(message.chat.id, "❌ Non sei autorizzato.")
|
||||
return
|
||||
|
||||
try:
|
||||
with open("scripts.json", "r") as f:
|
||||
scripts_data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
scripts_data = []
|
||||
|
||||
if not scripts_data:
|
||||
print(f"⚠️ Nessuno script registrato.")
|
||||
self.bot.send_message(message.chat.id, "⚠️ Nessuno script registrato.")
|
||||
return
|
||||
|
||||
current_time = time.time()
|
||||
msg = ["🖥️ **Script Registrati:**\n"]
|
||||
|
||||
for script in scripts_data:
|
||||
# Calcola la durata
|
||||
duration = current_time - script["start_time"]
|
||||
if "end_time" in script:
|
||||
duration = script["end_time"] - script["start_time"]
|
||||
|
||||
# Formatta la durata
|
||||
hours, rem = divmod(duration, 3600)
|
||||
minutes, seconds = divmod(rem, 60)
|
||||
duration_str = f"{int(hours)}h {int(minutes)}m {int(seconds)}s"
|
||||
|
||||
# Icona stato
|
||||
status_icons = {
|
||||
"running": "🟢",
|
||||
"stopped": "🔴",
|
||||
"completed": "⚪"
|
||||
}
|
||||
|
||||
# Costruisci riga
|
||||
line = (
|
||||
f"• ID: `{script['screen_id']}`\n"
|
||||
f"• Stato: {status_icons.get(script['status'], '⚫')}\n"
|
||||
f"• Stop: `/stop {script['screen_id']}`\n"
|
||||
f"• Screen: `/screen {script['screen_id']}`\n"
|
||||
f"• Durata: {duration_str}\n"
|
||||
f"• Download:\n{script.get('titolo', 'N/A')}\n"
|
||||
)
|
||||
msg.append(line)
|
||||
|
||||
# Formatta la risposta finale
|
||||
final_msg = "\n".join(msg)
|
||||
if len(final_msg) > 4000:
|
||||
final_msg = final_msg[:4000] + "\n[...] (messaggio troncato)"
|
||||
|
||||
print(f"{final_msg}")
|
||||
self.bot.send_message(message.chat.id, final_msg, parse_mode="Markdown")
|
||||
|
||||
def handle_stop_script(self, message):
|
||||
if not self.is_authorized(message.from_user.id):
|
||||
print(f"❌ Non sei autorizzato.")
|
||||
self.bot.send_message(message.chat.id, "❌ Non sei autorizzato.")
|
||||
return
|
||||
|
||||
parts = message.text.split()
|
||||
if len(parts) < 2:
|
||||
try:
|
||||
with open("scripts.json", "r") as f:
|
||||
scripts_data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
scripts_data = []
|
||||
|
||||
running_scripts = [s for s in scripts_data if s["status"] == "running"]
|
||||
|
||||
if not running_scripts:
|
||||
print(f"⚠️ Nessuno script attivo da fermare.")
|
||||
self.bot.send_message(message.chat.id, "⚠️ Nessuno script attivo da fermare.")
|
||||
return
|
||||
|
||||
msg = "🖥️ **Script Attivi:**\n"
|
||||
for script in running_scripts:
|
||||
msg += f"🔹 `/stop {script['screen_id']}` per fermarlo\n"
|
||||
|
||||
print(f"{msg}")
|
||||
self.bot.send_message(message.chat.id, msg, parse_mode="Markdown")
|
||||
|
||||
elif len(parts) == 2:
|
||||
screen_id = parts[1]
|
||||
|
||||
try:
|
||||
with open("scripts.json", "r") as f:
|
||||
scripts_data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
scripts_data = []
|
||||
|
||||
# Filtra la lista eliminando lo script con l'ID specificato
|
||||
new_scripts_data = [script for script in scripts_data if script["screen_id"] != screen_id]
|
||||
|
||||
if len(new_scripts_data) == len(scripts_data):
|
||||
# Nessun elemento rimosso, quindi ID non trovato
|
||||
print(f"⚠️ Nessuno script attivo con ID `{screen_id}`.")
|
||||
self.bot.send_message(message.chat.id, f"⚠️ Nessuno script attivo con ID `{screen_id}`.", parse_mode="Markdown")
|
||||
return
|
||||
|
||||
# Terminare la sessione screen
|
||||
try:
|
||||
subprocess.check_output(["screen", "-S", screen_id, "-X", "quit"])
|
||||
print(f"✅ La sessione screen con ID {screen_id} è stata fermata.")
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"⚠️ Impossibile fermare la sessione screen con ID `{screen_id}`.")
|
||||
self.bot.send_message(message.chat.id, f"⚠️ Impossibile fermare la sessione screen con ID `{screen_id}`.", parse_mode="Markdown")
|
||||
return
|
||||
|
||||
# Salva la lista aggiornata senza lo script eliminato
|
||||
with open("scripts.json", "w") as f:
|
||||
json.dump(new_scripts_data, f, indent=4)
|
||||
|
||||
print(f"✅ Script `{screen_id}` terminato con successo!")
|
||||
self.bot.send_message(message.chat.id, f"✅ Script `{screen_id}` terminato con successo!", parse_mode="Markdown")
|
||||
|
||||
def handle_response(self, message):
|
||||
""" if message.text == 'Start':
|
||||
self.handle_start_script(self, message)
|
||||
elif message.text == 'Lista':
|
||||
self.handle_list_scripts(self, message)
|
||||
elif message.text == 'Azione 3':
|
||||
self.bot.send_message(message.chat.id, "Hai scelto Azione 3!")
|
||||
else:
|
||||
self.bot.send_message(message.chat.id, "Comando non riconosciuto.")
|
||||
|
||||
if not self.is_authorized(message.from_user.id):
|
||||
self.bot.reply_to(message, "❌ Non sei autorizzato.")
|
||||
return """
|
||||
|
||||
text = message.text
|
||||
if self.request_manager.save_response(text):
|
||||
print(f"📥 Risposta salvata correttamente per il tipo {text}")
|
||||
else:
|
||||
print("⚠️ Nessuna richiesta attiva.")
|
||||
self.bot.reply_to(message, "❌ Nessuna richiesta attiva.")
|
||||
|
||||
def handle_screen_status(self, message):
|
||||
command_parts = message.text.split()
|
||||
if len(command_parts) < 2:
|
||||
print(f"⚠️ ID mancante nel comando. Usa: /screen <ID>")
|
||||
self.bot.send_message(message.chat.id, "⚠️ ID mancante nel comando. Usa: /screen <ID>")
|
||||
return
|
||||
|
||||
screen_id = command_parts[1]
|
||||
temp_file = f"/tmp/screen_output_{screen_id}.txt"
|
||||
|
||||
try:
|
||||
# Cattura l'output della screen
|
||||
subprocess.run(["screen", "-X", "-S", screen_id, "hardcopy", "-h", temp_file], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"❌ Errore durante la cattura dell'output della screen: {e}")
|
||||
self.bot.send_message(message.chat.id, f"❌ Errore durante la cattura dell'output della screen: {e}")
|
||||
return
|
||||
|
||||
if not os.path.exists(temp_file):
|
||||
print(f"❌ Impossibile catturare l'output della screen.")
|
||||
self.bot.send_message(message.chat.id, f"❌ Impossibile catturare l'output della screen.")
|
||||
return
|
||||
|
||||
try:
|
||||
# Leggi il file con la codifica corretta
|
||||
with open(temp_file, 'r', encoding='latin-1') as file:
|
||||
screen_output = file.read()
|
||||
|
||||
# Pulisci l'output
|
||||
cleaned_output = re.sub(r'[\x00-\x1F\x7F]', '', screen_output) # Rimuovi caratteri di controllo
|
||||
cleaned_output = cleaned_output.replace('\n\n', '\n') # Rimuovi newline multipli
|
||||
|
||||
# Estrarre tutte le parti da "Download:" fino a "Video" o "Subtitle", senza includerli
|
||||
download_matches = re.findall(r"Download: (.*?)(?:Video|Subtitle)", cleaned_output)
|
||||
if download_matches:
|
||||
# Serie TV e Film StreamingCommunity
|
||||
|
||||
proc_matches = re.findall(r"Proc: ([\d\.]+%)", cleaned_output)
|
||||
|
||||
# Creare una stringa unica con tutti i risultati
|
||||
result_string = "\n".join([f"Download: {download_matches[i].strip()}\nDownload al {proc_matches[i]}" for i in range(len(download_matches)) if i < len(proc_matches)])
|
||||
|
||||
if result_string != "":
|
||||
cleaned_output = result_string
|
||||
else:
|
||||
print(f"❌ La parola 'Download:' non è stata trovata nella stringa.")
|
||||
else:
|
||||
|
||||
download_list = []
|
||||
|
||||
# Estrai tutte le righe che iniziano con "Download:" fino al prossimo "Download" o alla fine della riga
|
||||
matches = re.findall(r"Download:\s*(.*?)(?=Download|$)", cleaned_output)
|
||||
|
||||
# Se sono stati trovati download, stampali
|
||||
if matches:
|
||||
for i, match in enumerate(matches, 1):
|
||||
# rimuovo solo la parte "downloader.py:57Result:400" se esiste
|
||||
match = re.sub(r"downloader.py:\d+Result:400", "", match)
|
||||
match = match.strip() # Rimuovo gli spazi bianchi in eccesso
|
||||
if match: # Assicurati che la stringa non sia vuota
|
||||
print(f"Download {i}: {match}")
|
||||
|
||||
# Aggiungi il risultato modificato alla lista
|
||||
download_list.append(f"Download {i}: {match}")
|
||||
|
||||
# Creare una stringa unica con tutti i risultati
|
||||
cleaned_output = "\n".join(download_list)
|
||||
else:
|
||||
print("❌ Nessun download trovato")
|
||||
|
||||
# Invia l'output pulito
|
||||
print(f"📄 Output della screen {screen_id}:\n{cleaned_output}")
|
||||
self._send_long_message(message.chat.id, f"📄 Output della screen {screen_id}:\n{cleaned_output}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Errore durante la lettura o l'invio dell'output della screen: {e}")
|
||||
self.bot.send_message(message.chat.id, f"❌ Errore durante la lettura o l'invio dell'output della screen: {e}")
|
||||
|
||||
# Cancella il file temporaneo
|
||||
os.remove(temp_file)
|
||||
|
||||
def send_message(self, message, choices):
|
||||
if choices is None:
|
||||
if self.chat_id:
|
||||
print(f"{message}")
|
||||
self.bot.send_message(self.chat_id, message)
|
||||
else:
|
||||
formatted_choices = "\n".join(choices)
|
||||
message = f"{message}\n\n{formatted_choices}"
|
||||
if self.chat_id:
|
||||
print(f"{message}")
|
||||
self.bot.send_message(self.chat_id, message)
|
||||
|
||||
def _send_long_message(self, chat_id, text, chunk_size=4096):
|
||||
"""Suddivide e invia un messaggio troppo lungo in più parti."""
|
||||
for i in range(0, len(text), chunk_size):
|
||||
print(f"{text[i:i+chunk_size]}")
|
||||
self.bot.send_message(chat_id, text[i:i+chunk_size])
|
||||
|
||||
def ask(self, type, prompt_message, choices, timeout=60):
|
||||
self.request_manager.create_request(type)
|
||||
|
||||
if choices is None:
|
||||
print(f"{prompt_message}")
|
||||
self.bot.send_message(
|
||||
self.chat_id,
|
||||
f"{prompt_message}",
|
||||
)
|
||||
else:
|
||||
print(f"{prompt_message}\n\nOpzioni: {', '.join(choices)}")
|
||||
self.bot.send_message(
|
||||
self.chat_id,
|
||||
f"{prompt_message}\n\nOpzioni: {', '.join(choices)}",
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
response = self.request_manager.get_response()
|
||||
if response is not None:
|
||||
return response
|
||||
time.sleep(1)
|
||||
|
||||
print(f"⚠️ Timeout: nessuna risposta ricevuta.")
|
||||
self.bot.send_message(self.chat_id, "⚠️ Timeout: nessuna risposta ricevuta.")
|
||||
self.request_manager.clear_file()
|
||||
return None
|
||||
|
||||
def run(self):
|
||||
print("🚀 Avvio del bot...")
|
||||
# svuoto il file scripts.json
|
||||
with open("scripts.json", "w") as f:
|
||||
json.dump([], f)
|
||||
self.bot.infinity_polling()
|
||||
|
||||
def get_bot_instance():
|
||||
return TelegramBot.get_instance()
|
||||
|
||||
# Esempio di utilizzo
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Usa le variabili
|
||||
token = os.getenv("TOKEN_TELEGRAM")
|
||||
authorized_user_id = os.getenv("AUTHORIZED_USER_ID")
|
||||
|
||||
TOKEN = token # Inserisci il token del tuo bot Telegram sul file .env
|
||||
AUTHORIZED_USER_ID = int(authorized_user_id) # Inserisci il tuo ID utente Telegram sul file .env
|
||||
|
||||
# Inizializza il bot
|
||||
bot = TelegramBot.init_bot(TOKEN, AUTHORIZED_USER_ID)
|
||||
bot.run()
|
||||
|
||||
"""
|
||||
start - Avvia lo script
|
||||
list - Lista script attivi
|
||||
get - Mostra ID utente Telegram
|
||||
"""
|
@ -38,7 +38,7 @@ from ...M3U8 import (
|
||||
from .segments import M3U8_Segments
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
# Config
|
||||
|
@ -30,7 +30,7 @@ import urllib3
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
||||
|
@ -19,7 +19,7 @@ from .message import start_message
|
||||
from .call_stack import get_call_stack
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
StreamingCommunity.HelpTg. import get_bot_instance
|
||||
from StreamingCommunity.Util._jsonConfig import config_manager
|
||||
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
|
||||
|
||||
|
@ -21,7 +21,7 @@ from StreamingCommunity.Util.os import os_summary
|
||||
from StreamingCommunity.Util.logger import Logger
|
||||
|
||||
# Telegram bot instance
|
||||
from telegram_bot import get_bot_instance
|
||||
from StreamingCommunity.HelpTg.telegram_bot import get_bot_instance
|
||||
|
||||
# Config
|
||||
CLOSE_CONSOLE = config_manager.get_bool('DEFAULT', 'not_close')
|
||||
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"type": "select_title",
|
||||
"response": "0",
|
||||
"timestamp": 1738667185.9680498
|
||||
}
|
@ -1 +0,0 @@
|
||||
{"token": "7924628948:AAGbG4yjRbEeXhVz1RALcV9mt89gB0yMehs", "authorized_user_id": 676749122}
|
16
config.json
16
config.json
@ -2,17 +2,17 @@
|
||||
"DEFAULT": {
|
||||
"debug": false,
|
||||
"log_file": "app.log",
|
||||
"log_to_file": true,
|
||||
"show_message": true,
|
||||
"log_to_file": false,
|
||||
"show_message": false,
|
||||
"clean_console": true,
|
||||
"root_path": "Video",
|
||||
"root_path": "/home/giuseppepiccolo/Develop/docker/jellyfin/media/",
|
||||
"movie_folder_name": "Movie",
|
||||
"serie_folder_name": "TV",
|
||||
"serie_folder_name": "Serie",
|
||||
"anime_folder_name": "Anime",
|
||||
"map_episode_name": "E%(episode)_%(episode_name)",
|
||||
"map_episode_name": "%(tv_name)_S%(season)E%(episode)_%(episode_name)",
|
||||
"config_qbit_tor": {
|
||||
"host": "192.168.1.99",
|
||||
"port": "7060",
|
||||
"host": "192.168.5.172",
|
||||
"port": "8080",
|
||||
"user": "admin",
|
||||
"pass": "adminadmin"
|
||||
},
|
||||
@ -35,7 +35,7 @@
|
||||
"specific_list_audio": [
|
||||
"ita"
|
||||
],
|
||||
"merge_subs": true,
|
||||
"merge_subs": false,
|
||||
"specific_list_subtitles": [
|
||||
"eng",
|
||||
"spa"
|
||||
|
@ -1,17 +0,0 @@
|
||||
httpx
|
||||
bs4
|
||||
rich
|
||||
tqdm
|
||||
m3u8
|
||||
psutil
|
||||
unidecode
|
||||
jsbeautifier
|
||||
pathvalidate
|
||||
pycryptodomex
|
||||
googlesearch-python
|
||||
fake-useragent
|
||||
qbittorrent-api
|
||||
python-qbittorrent
|
||||
serpapi
|
||||
pyTelegramBotAPI
|
||||
Pillow
|
@ -11,4 +11,6 @@ pycryptodomex
|
||||
googlesearch-python
|
||||
fake-useragent
|
||||
qbittorrent-api
|
||||
python-qbittorrent
|
||||
python-qbittorrent
|
||||
pyTelegramBotAPI
|
||||
Pillow
|
17
scripts.json
17
scripts.json
@ -1 +1,16 @@
|
||||
[]
|
||||
[
|
||||
{
|
||||
"screen_id": "0e830205",
|
||||
"start_time": 1738674260.611711,
|
||||
"status": "running",
|
||||
"user_id": 676749122,
|
||||
"titolo": "Doctor Strange"
|
||||
},
|
||||
{
|
||||
"screen_id": "29057a57",
|
||||
"start_time": 1738674321.6443458,
|
||||
"status": "running",
|
||||
"user_id": 676749122,
|
||||
"titolo": "Guardiani della Galassia Vol. 2"
|
||||
}
|
||||
]
|
@ -2,8 +2,8 @@
|
||||
|
||||
import sys
|
||||
from StreamingCommunity.run import main
|
||||
from request_manager import RequestManager
|
||||
from session import set_session
|
||||
from StreamingCommunity.HelpTg.request_manager import RequestManager
|
||||
from StreamingCommunity.HelpTg.session import set_session
|
||||
|
||||
# Svuoto il file
|
||||
request_manager = RequestManager()
|
||||
|
Loading…
x
Reference in New Issue
Block a user