diff --git a/StreamingCommunity/Api/Template/site.py b/StreamingCommunity/Api/Template/site.py index ac2ef86..2723a11 100644 --- a/StreamingCommunity/Api/Template/site.py +++ b/StreamingCommunity/Api/Template/site.py @@ -1,11 +1,15 @@ # 19.06.24 +# Modifiche per integrare l'input da Telegram Bot import sys - # 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 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'] -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. + 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: - 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: - console.print("\n[red]No media items available.") + + # console.print("\n[red]No media items available.") 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 - color_index = 1 - for key in first_media_item.__dict__.keys(): + if site_constant.TELEGRAM_BOT: + bot = get_bot_instance() + + 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: - continue + if user_input_str is None: + bot.send_message("Timeout: nessuna selezione ricevuta.", None) + return None - if key in ('id', 'type', 'name', 'score'): # Custom prioritization of colors - if key == 'type': - column_info["Type"] = {'color': 'yellow'} - elif key == 'name': - column_info["Name"] = {'color': 'magenta'} - elif key == 'score': - column_info["Score"] = {'color': 'cyan'} + try: + chosen_index = int(user_input_str) + if 0 <= chosen_index < num_results_available: + selected_item = media_search_manager.get(chosen_index) + if selected_item: + # bot.send_message(f"Hai selezionato: {selected_item.name}", None) # Messaggio di conferma opzionale + 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: - column_info[key.capitalize()] = {'color': available_colors[color_index % len(available_colors)]} - color_index += 1 - - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - media_dict = {'Index': str(i)} + else: + if not media_search_manager.media_list: + console.print("\n[red]No media items available.") + return None + + first_media_item = media_search_manager.media_list[0] + column_info = {"Index": {'color': available_colors[0]}} + color_index = 1 for key in first_media_item.__dict__.keys(): if key.capitalize() in column_to_hide: 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 - media_dict[key.capitalize()] = str(getattr(media, key)) + table_show_manager.clear() + 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 = table_show_manager.run(force_int_input=True, max_int_input=len(media_search_manager.media_list)) - table_show_manager.clear() + last_command_str = table_show_manager.run(force_int_input=True, max_int_input=len(media_search_manager.media_list)) + table_show_manager.clear() - # Handle user's quit command - if last_command == "q" or last_command == "quit": - console.print("\n[red]Quit ...") - sys.exit(0) + if last_command_str is None or last_command_str.lower() in ["q", "quit"]: + console.print("\n[red]Selezione annullata o uscita.") + return None - # Check if the selected index is within range - if 0 <= int(last_command) < len(media_search_manager.media_list): - return media_search_manager.get(int(last_command)) - - else: - console.print("\n[red]Wrong index") - sys.exit(0) \ No newline at end of file + try: + + selected_index = int(last_command_str) + if 0 <= selected_index < len(media_search_manager.media_list): + return media_search_manager.get(selected_index) + else: + 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 diff --git a/StreamingCommunity/TelegramHelp/telegram_bot.py b/StreamingCommunity/TelegramHelp/telegram_bot.py index d24b68d..85eb4c9 100644 --- a/StreamingCommunity/TelegramHelp/telegram_bot.py +++ b/StreamingCommunity/TelegramHelp/telegram_bot.py @@ -575,6 +575,10 @@ class TelegramBot: cleaned_output = cleaned_output.replace( "\n\n", "\n" ) # 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 ## ## download_section = re.search(r"##(.*?)##", cleaned_output, re.DOTALL)