From d021cf6b2ed02b7e8ef6f882a8486875126ff93c Mon Sep 17 00:00:00 2001 From: l1n00 <> Date: Sun, 18 May 2025 11:03:10 +0200 Subject: [PATCH] Refactor user input handling and improve messaging in __init__.py --- .../Api/Site/streamingcommunity/__init__.py | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/StreamingCommunity/Api/Site/streamingcommunity/__init__.py b/StreamingCommunity/Api/Site/streamingcommunity/__init__.py index 8a2e9db..d4323f3 100644 --- a/StreamingCommunity/Api/Site/streamingcommunity/__init__.py +++ b/StreamingCommunity/Api/Site/streamingcommunity/__init__.py @@ -26,7 +26,7 @@ from .series import download_series # Variable indice = 0 -_useFor = "Film_&_Serie" +_useFor = "Film_&_Serie" # "Movies_&_Series" _priority = 0 _engineDownload = "hls" _deprecate = False @@ -39,6 +39,7 @@ def get_user_input(string_to_search: str = None): """ Asks the user to input a search term. Handles both Telegram bot input and direct input. + If string_to_search is provided, it's returned directly (after stripping). """ if string_to_search is not None: return string_to_search.strip() @@ -46,25 +47,26 @@ def get_user_input(string_to_search: str = None): if site_constant.TELEGRAM_BOT: bot = get_bot_instance() user_response = bot.ask( - "key_search", # Tipo di richiesta + "key_search", # Request type "Enter the search term\nor type 'back' to return to the menu: ", None ) if user_response is None: - bot.send_message("Timeout: Nessun termine di ricerca inserito.", None) + bot.send_message("Timeout: No search term entered.", None) return None if user_response.lower() == 'back': - bot.send_message("Ritorno al menu principale...", None) + bot.send_message("Returning to the main menu...", None) try: + # Restart the script subprocess.Popen([sys.executable] + sys.argv) sys.exit() except Exception as e: - bot.send_message(f"Errore durante il tentativo di riavvio: {e}", None) - return None + bot.send_message(f"Error during restart attempt: {e}", None) + return None # Return None if restart fails return user_response.strip() @@ -76,16 +78,17 @@ def process_search_result(select_title, selections=None, proxy=None): Handles the search result and initiates the download for either a film or series. Parameters: - select_title (MediaItem): The selected media item. Può essere None se la selezione fallisce. + select_title (MediaItem): The selected media item. Can be None if selection fails. selections (dict, optional): Dictionary containing selection inputs that bypass manual input - {'season': season_selection, 'episode': episode_selection} + e.g., {'season': season_selection, 'episode': episode_selection} + proxy (str, optional): The proxy to use for downloads. """ if not select_title: if site_constant.TELEGRAM_BOT: bot = get_bot_instance() - bot.send_message("Nessun titolo selezionato o selezione annullata.", None) + bot.send_message("No title selected or selection cancelled.", None) else: - console.print("[yellow]Nessun titolo selezionato o selezione annullata.") + console.print("[yellow]No title selected or selection cancelled.") return if select_title.type == 'tv': @@ -98,18 +101,20 @@ def process_search_result(select_title, selections=None, proxy=None): download_series(select_title, season_selection, episode_selection, proxy) - else: - download_film(select_title) + else: # 'movie' or other types assumed to be film-like + download_film(select_title, proxy) # Assuming download_film might also need proxy def search(string_to_search: str = None, get_onlyDatabase: bool = False, direct_item: dict = None, selections: dict = None): """ Main function of the application for search. Parameters: - string_to_search (str, optional): String to search for. Può essere passato da run.py. - get_onlyDatabase (bool, optional): If True, return only the database object. - direct_item (dict, optional): Direct item to process (bypass search). - selections (dict, optional): Dictionary containing selection inputs that bypass manual input. + string_to_search (str, optional): String to search for. Can be passed from run.py. + If 'back', special handling might occur in get_user_input. + get_onlyDatabase (bool, optional): If True, return only the database search manager object. + direct_item (dict, optional): Direct item to process (bypasses search). + selections (dict, optional): Dictionary containing selection inputs that bypass manual input + for series (season/episode). """ bot = None if site_constant.TELEGRAM_BOT: @@ -117,49 +122,45 @@ def search(string_to_search: str = None, get_onlyDatabase: bool = False, direct_ if direct_item: select_title_obj = MediaItem(**direct_item) - process_search_result(select_title_obj, selections) + # Note: If direct_item processing requires a proxy, it should be fetched here. + # For now, assuming process_search_result handles proxy=None if not provided. + finder = ProxyFinder(site_constant.FULL_URL) # Get proxy for direct item too + proxy = finder.find_fast_proxy() + process_search_result(select_title_obj, selections, proxy) return actual_search_query = get_user_input(string_to_search) - if not actual_search_query: # Se l'utente ha scritto 'back' (gestito da get_user_input) o input vuoto/timeout + # Handle cases where user input is empty, or 'back' was handled (sys.exit or None return) + if not actual_search_query: if bot: - if actual_search_query is None: - bot.send_message("Termine di ricerca non fornito. Ritorno al menu precedente.", None) + if actual_search_query is None: # Specifically for timeout from bot.ask or failed restart + bot.send_message("Search term not provided or operation cancelled. Returning.", None) + # If not bot, or empty string, just return; will likely lead to no results or previous menu. return - # Search on database + # Perform search on the database using the obtained query finder = ProxyFinder(site_constant.FULL_URL) proxy = finder.find_fast_proxy() len_database = title_search(actual_search_query, proxy) - if string_to_search == 'back': - - # Restart the script - subprocess.Popen([sys.executable] + sys.argv) - sys.exit() - else: - string_to_search = msg.ask(f"\n[purple]Insert a word to search in [green]{site_constant.SITE_NAME}").strip() - - # Search on database - finder = ProxyFinder(site_constant.FULL_URL) - proxy = finder.find_fast_proxy() - len_database = title_search(string_to_search, proxy) - - # If only the database is needed, return the manager + # If only the database object (media_search_manager populated by title_search) is needed if get_onlyDatabase: - return media_search_manager + return media_search_manager if len_database > 0: - select_title = get_select_title(table_show_manager, media_search_manager) - process_search_result(select_title, selections, proxy) + # *** THE FIX IS HERE: Added len_database as the third argument *** + select_title = get_select_title(table_show_manager, media_search_manager, len_database) + process_search_result(select_title, selections, proxy) # Pass proxy else: - no_results_message = f"Nessun risultato trovato per: '{actual_search_query}'" + # No results found + no_results_message = f"No results found for: '{actual_search_query}'" if bot: bot.send_message(no_results_message, None) else: console.print(f"\n[red]Nothing matching was found for[white]: [purple]{actual_search_query}") - # NON chiamare search() ricorsivamente. - return + # Do not call search() recursively here to avoid infinite loops on no results. + # The flow should return to the caller (e.g., main menu in run.py). + return \ No newline at end of file