mirror of
https://github.com/Arrowar/StreamingCommunity.git
synced 2025-06-06 19:45:24 +00:00
Fix telegram_bot
This commit is contained in:
parent
e0a1e28625
commit
97c9ddcd1f
@ -1,11 +1,15 @@
|
|||||||
# 19.06.24
|
# 19.06.24
|
||||||
|
# Modifiche per integrare l'input da Telegram Bot
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
# External library
|
# External library
|
||||||
from rich.console import Console
|
from rich.console import Console # Usata per output console
|
||||||
|
|
||||||
|
# Internal utilities
|
||||||
|
|
||||||
|
from StreamingCommunity.Api.Template.config_loader import site_constant
|
||||||
|
from StreamingCommunity.TelegramHelp.telegram_bot import get_bot_instance
|
||||||
|
|
||||||
# Variable
|
# Variable
|
||||||
console = Console()
|
console = Console()
|
||||||
@ -13,72 +17,107 @@ available_colors = ['red', 'magenta', 'yellow', 'cyan', 'green', 'blue', 'white'
|
|||||||
column_to_hide = ['Slug', 'Sub_ita', 'Last_air_date', 'Seasons_count', 'Url', 'Image', 'Path_id']
|
column_to_hide = ['Slug', 'Sub_ita', 'Last_air_date', 'Seasons_count', 'Url', 'Image', 'Path_id']
|
||||||
|
|
||||||
|
|
||||||
def get_select_title(table_show_manager, media_search_manager):
|
def get_select_title(table_show_manager, media_search_manager, num_results_available):
|
||||||
"""
|
"""
|
||||||
Display a selection of titles and prompt the user to choose one.
|
Display a selection of titles and prompt the user to choose one.
|
||||||
|
Handles both console and Telegram bot input.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
table_show_manager: Manager for console table display.
|
||||||
|
media_search_manager: Manager holding the list of media items.
|
||||||
|
num_results_available (int): The number of media items available for selection.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
MediaItem: The selected media item.
|
MediaItem: The selected media item, or None if no selection is made or an error occurs.
|
||||||
"""
|
"""
|
||||||
# Determine column_info dynamically for (search site)
|
|
||||||
if not media_search_manager.media_list:
|
if not media_search_manager.media_list:
|
||||||
console.print("\n[red]No media items available.")
|
|
||||||
|
# console.print("\n[red]No media items available.")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Example of available colors for columns
|
|
||||||
available_colors = ['red', 'magenta', 'yellow', 'cyan', 'green', 'blue', 'white']
|
|
||||||
|
|
||||||
# Retrieve the keys of the first media item as column headers
|
|
||||||
first_media_item = media_search_manager.media_list[0]
|
|
||||||
column_info = {"Index": {'color': available_colors[0]}} # Always include Index with a fixed color
|
|
||||||
|
|
||||||
# Assign colors to the remaining keys dynamically
|
if site_constant.TELEGRAM_BOT:
|
||||||
color_index = 1
|
bot = get_bot_instance()
|
||||||
for key in first_media_item.__dict__.keys():
|
|
||||||
|
prompt_message = f"Inserisci il numero del titolo che vuoi selezionare (da 0 a {num_results_available - 1}):"
|
||||||
|
|
||||||
|
user_input_str = bot.ask(
|
||||||
|
"select_title_from_list_number",
|
||||||
|
prompt_message,
|
||||||
|
None
|
||||||
|
)
|
||||||
|
|
||||||
if key.capitalize() in column_to_hide:
|
if user_input_str is None:
|
||||||
continue
|
bot.send_message("Timeout: nessuna selezione ricevuta.", None)
|
||||||
|
return None
|
||||||
|
|
||||||
if key in ('id', 'type', 'name', 'score'): # Custom prioritization of colors
|
try:
|
||||||
if key == 'type':
|
chosen_index = int(user_input_str)
|
||||||
column_info["Type"] = {'color': 'yellow'}
|
if 0 <= chosen_index < num_results_available:
|
||||||
elif key == 'name':
|
selected_item = media_search_manager.get(chosen_index)
|
||||||
column_info["Name"] = {'color': 'magenta'}
|
if selected_item:
|
||||||
elif key == 'score':
|
# bot.send_message(f"Hai selezionato: {selected_item.name}", None) # Messaggio di conferma opzionale
|
||||||
column_info["Score"] = {'color': 'cyan'}
|
return selected_item
|
||||||
|
else:
|
||||||
|
bot.send_message(f"Errore interno: Impossibile recuperare il titolo con indice {chosen_index}.", None)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
bot.send_message(f"Selezione '{chosen_index}' non valida. Inserisci un numero compreso tra 0 e {num_results_available - 1}.", None)
|
||||||
|
return None
|
||||||
|
except ValueError:
|
||||||
|
bot.send_message(f"Input '{user_input_str}' non valido. Devi inserire un numero.", None)
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
bot.send_message(f"Si è verificato un errore durante la selezione: {e}", None)
|
||||||
|
return None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
column_info[key.capitalize()] = {'color': available_colors[color_index % len(available_colors)]}
|
if not media_search_manager.media_list:
|
||||||
color_index += 1
|
console.print("\n[red]No media items available.")
|
||||||
|
return None
|
||||||
table_show_manager.add_column(column_info)
|
|
||||||
|
first_media_item = media_search_manager.media_list[0]
|
||||||
# Populate the table with title information
|
column_info = {"Index": {'color': available_colors[0]}}
|
||||||
for i, media in enumerate(media_search_manager.media_list):
|
|
||||||
media_dict = {'Index': str(i)}
|
|
||||||
|
|
||||||
|
color_index = 1
|
||||||
for key in first_media_item.__dict__.keys():
|
for key in first_media_item.__dict__.keys():
|
||||||
if key.capitalize() in column_to_hide:
|
if key.capitalize() in column_to_hide:
|
||||||
continue
|
continue
|
||||||
|
if key in ('id', 'type', 'name', 'score'):
|
||||||
|
if key == 'type': column_info["Type"] = {'color': 'yellow'}
|
||||||
|
elif key == 'name': column_info["Name"] = {'color': 'magenta'}
|
||||||
|
elif key == 'score': column_info["Score"] = {'color': 'cyan'}
|
||||||
|
else:
|
||||||
|
column_info[key.capitalize()] = {'color': available_colors[color_index % len(available_colors)]}
|
||||||
|
color_index += 1
|
||||||
|
|
||||||
# Ensure all values are strings for rich add table
|
table_show_manager.clear()
|
||||||
media_dict[key.capitalize()] = str(getattr(media, key))
|
table_show_manager.add_column(column_info)
|
||||||
|
|
||||||
table_show_manager.add_tv_show(media_dict)
|
for i, media in enumerate(media_search_manager.media_list):
|
||||||
|
media_dict = {'Index': str(i)}
|
||||||
|
for key in first_media_item.__dict__.keys():
|
||||||
|
if key.capitalize() in column_to_hide:
|
||||||
|
continue
|
||||||
|
media_dict[key.capitalize()] = str(getattr(media, key))
|
||||||
|
table_show_manager.add_tv_show(media_dict)
|
||||||
|
|
||||||
# Run the table and handle user input
|
last_command_str = table_show_manager.run(force_int_input=True, max_int_input=len(media_search_manager.media_list))
|
||||||
last_command = table_show_manager.run(force_int_input=True, max_int_input=len(media_search_manager.media_list))
|
table_show_manager.clear()
|
||||||
table_show_manager.clear()
|
|
||||||
|
|
||||||
# Handle user's quit command
|
if last_command_str is None or last_command_str.lower() in ["q", "quit"]:
|
||||||
if last_command == "q" or last_command == "quit":
|
console.print("\n[red]Selezione annullata o uscita.")
|
||||||
console.print("\n[red]Quit ...")
|
return None
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
# Check if the selected index is within range
|
try:
|
||||||
if 0 <= int(last_command) < len(media_search_manager.media_list):
|
|
||||||
return media_search_manager.get(int(last_command))
|
selected_index = int(last_command_str)
|
||||||
|
if 0 <= selected_index < len(media_search_manager.media_list):
|
||||||
else:
|
return media_search_manager.get(selected_index)
|
||||||
console.print("\n[red]Wrong index")
|
else:
|
||||||
sys.exit(0)
|
console.print("\n[red]Indice errato o non valido.")
|
||||||
|
# sys.exit(0)
|
||||||
|
return None
|
||||||
|
except ValueError:
|
||||||
|
console.print("\n[red]Input non numerico ricevuto dalla tabella.")
|
||||||
|
# sys.exit(0)
|
||||||
|
return None
|
||||||
|
@ -575,6 +575,10 @@ class TelegramBot:
|
|||||||
cleaned_output = cleaned_output.replace(
|
cleaned_output = cleaned_output.replace(
|
||||||
"\n\n", "\n"
|
"\n\n", "\n"
|
||||||
) # Rimuovi newline multipli
|
) # Rimuovi newline multipli
|
||||||
|
|
||||||
|
# Inizializza le variabili
|
||||||
|
cleaned_output_0 = None # o ""
|
||||||
|
cleaned_output_1 = None # o ""
|
||||||
|
|
||||||
# Dentro cleaned_output c'è una stringa recupero quello che si trova tra ## ##
|
# Dentro cleaned_output c'è una stringa recupero quello che si trova tra ## ##
|
||||||
download_section = re.search(r"##(.*?)##", cleaned_output, re.DOTALL)
|
download_section = re.search(r"##(.*?)##", cleaned_output, re.DOTALL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user