Refactor user input handling and improve messaging in __init__.py

This commit is contained in:
l1n00 2025-05-18 11:03:10 +02:00
parent b1a3a169cf
commit d021cf6b2e

View File

@ -26,7 +26,7 @@ from .series import download_series
# Variable # Variable
indice = 0 indice = 0
_useFor = "Film_&_Serie" _useFor = "Film_&_Serie" # "Movies_&_Series"
_priority = 0 _priority = 0
_engineDownload = "hls" _engineDownload = "hls"
_deprecate = False _deprecate = False
@ -39,6 +39,7 @@ def get_user_input(string_to_search: str = None):
""" """
Asks the user to input a search term. Asks the user to input a search term.
Handles both Telegram bot input and direct input. 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: if string_to_search is not None:
return string_to_search.strip() return string_to_search.strip()
@ -46,25 +47,26 @@ def get_user_input(string_to_search: str = None):
if site_constant.TELEGRAM_BOT: if site_constant.TELEGRAM_BOT:
bot = get_bot_instance() bot = get_bot_instance()
user_response = bot.ask( 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: ", "Enter the search term\nor type 'back' to return to the menu: ",
None None
) )
if user_response is 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 return None
if user_response.lower() == 'back': if user_response.lower() == 'back':
bot.send_message("Ritorno al menu principale...", None) bot.send_message("Returning to the main menu...", None)
try: try:
# Restart the script
subprocess.Popen([sys.executable] + sys.argv) subprocess.Popen([sys.executable] + sys.argv)
sys.exit() sys.exit()
except Exception as e: except Exception as e:
bot.send_message(f"Errore durante il tentativo di riavvio: {e}", None) bot.send_message(f"Error during restart attempt: {e}", None)
return None return None # Return None if restart fails
return user_response.strip() 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. Handles the search result and initiates the download for either a film or series.
Parameters: 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 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 not select_title:
if site_constant.TELEGRAM_BOT: if site_constant.TELEGRAM_BOT:
bot = get_bot_instance() 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: else:
console.print("[yellow]Nessun titolo selezionato o selezione annullata.") console.print("[yellow]No title selected or selection cancelled.")
return return
if select_title.type == 'tv': 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) download_series(select_title, season_selection, episode_selection, proxy)
else: else: # 'movie' or other types assumed to be film-like
download_film(select_title) 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): 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. Main function of the application for search.
Parameters: Parameters:
string_to_search (str, optional): String to search for. Può essere passato da run.py. string_to_search (str, optional): String to search for. Can be passed from run.py.
get_onlyDatabase (bool, optional): If True, return only the database object. If 'back', special handling might occur in get_user_input.
direct_item (dict, optional): Direct item to process (bypass search). get_onlyDatabase (bool, optional): If True, return only the database search manager object.
selections (dict, optional): Dictionary containing selection inputs that bypass manual input. 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 bot = None
if site_constant.TELEGRAM_BOT: if site_constant.TELEGRAM_BOT:
@ -117,49 +122,45 @@ def search(string_to_search: str = None, get_onlyDatabase: bool = False, direct_
if direct_item: if direct_item:
select_title_obj = MediaItem(**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 return
actual_search_query = get_user_input(string_to_search) 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 bot:
if actual_search_query is None: if actual_search_query is None: # Specifically for timeout from bot.ask or failed restart
bot.send_message("Termine di ricerca non fornito. Ritorno al menu precedente.", None) 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 return
# Search on database # Perform search on the database using the obtained query
finder = ProxyFinder(site_constant.FULL_URL) finder = ProxyFinder(site_constant.FULL_URL)
proxy = finder.find_fast_proxy() proxy = finder.find_fast_proxy()
len_database = title_search(actual_search_query, proxy) len_database = title_search(actual_search_query, proxy)
if string_to_search == 'back': # If only the database object (media_search_manager populated by title_search) is needed
# 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 get_onlyDatabase: if get_onlyDatabase:
return media_search_manager return media_search_manager
if len_database > 0: if len_database > 0:
select_title = get_select_title(table_show_manager, media_search_manager) # *** THE FIX IS HERE: Added len_database as the third argument ***
process_search_result(select_title, selections, proxy) select_title = get_select_title(table_show_manager, media_search_manager, len_database)
process_search_result(select_title, selections, proxy) # Pass proxy
else: 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: if bot:
bot.send_message(no_results_message, None) bot.send_message(no_results_message, None)
else: else:
console.print(f"\n[red]Nothing matching was found for[white]: [purple]{actual_search_query}") console.print(f"\n[red]Nothing matching was found for[white]: [purple]{actual_search_query}")
# NON chiamare search() ricorsivamente. # Do not call search() recursively here to avoid infinite loops on no results.
return # The flow should return to the caller (e.g., main menu in run.py).
return