From 62a6c74b600bb7ab26a5a791fb8439ab89f61409 Mon Sep 17 00:00:00 2001 From: Lovi <62809003+Lovi-0@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:29:57 +0200 Subject: [PATCH] Dinamic get_select_title(). --- Src/Api/Template/__init__.py | 3 + Src/Api/Template/site.py | 88 +++++++++++++++++++ Src/Api/altadefinizione/__init__.py | 4 +- Src/Api/altadefinizione/site.py | 57 +----------- Src/Api/animeunity/Core/Class/EpisodeType.py | 17 ---- Src/Api/animeunity/__init__.py | 4 +- Src/Api/animeunity/site.py | 63 ++----------- Src/Api/ddlstreamitaly/__init__.py | 4 +- Src/Api/ddlstreamitaly/site.py | 57 ++---------- Src/Api/guardaserie/__init__.py | 4 +- Src/Api/guardaserie/site.py | 59 ++----------- .../Core/Class/EpisodeType.py | 18 ---- .../Core/Class/SearchType.py | 16 +--- Src/Api/streamingcommunity/__init__.py | 4 +- Src/Api/streamingcommunity/site.py | 58 +----------- 15 files changed, 125 insertions(+), 331 deletions(-) create mode 100644 Src/Api/Template/site.py diff --git a/Src/Api/Template/__init__.py b/Src/Api/Template/__init__.py index f15ac79..7a99f16 100644 --- a/Src/Api/Template/__init__.py +++ b/Src/Api/Template/__init__.py @@ -1 +1,4 @@ +# 19.06.24 + +from .site import get_select_title from .Util.get_domain import search_domain \ No newline at end of file diff --git a/Src/Api/Template/site.py b/Src/Api/Template/site.py new file mode 100644 index 0000000..08b3fd4 --- /dev/null +++ b/Src/Api/Template/site.py @@ -0,0 +1,88 @@ +# 19.06.24 + +import sys +import logging + + +# Internal utilities +from Src.Util.console import console + + +# Variable +available_colors = ['red', 'magenta', 'yellow', 'cyan', 'green', 'blue', 'white'] +column_to_hide = ['Slug', 'Sub_ita', 'Last_air_date', 'Seasons_count', 'Url'] + + +def get_select_title(table_show_manager, media_search_manager): + """ + Display a selection of titles and prompt the user to choose one. + + Returns: + MediaItem: The selected media item. + """ + + # Set up table for displaying titles + table_show_manager.set_slice_end(10) + + # Determine column_info dynamically + if not media_search_manager.media_list: + 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 key.capitalize() in column_to_hide: + continue + + 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'} + + 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)} + + for key in first_media_item.__dict__.keys(): + if key.capitalize() in column_to_hide: + continue + + # Ensure all values are strings for rich add table + 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() + + # Handle user's quit command + if last_command == "q": + console.print("\n[red]Quit [white]...") + sys.exit(0) + + # 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 diff --git a/Src/Api/altadefinizione/__init__.py b/Src/Api/altadefinizione/__init__.py index 561a33b..c2d9a28 100644 --- a/Src/Api/altadefinizione/__init__.py +++ b/Src/Api/altadefinizione/__init__.py @@ -5,7 +5,7 @@ from Src.Util.console import console, msg # Logic class -from .site import title_search, get_select_title +from .site import title_search, run_get_select_title from .film import download_film @@ -26,7 +26,7 @@ def search(): if len_database > 0: # Select title from list - select_title = get_select_title() + select_title = run_get_select_title() # Download only film download_film( diff --git a/Src/Api/altadefinizione/site.py b/Src/Api/altadefinizione/site.py index aca6105..6657a3c 100644 --- a/Src/Api/altadefinizione/site.py +++ b/Src/Api/altadefinizione/site.py @@ -12,13 +12,12 @@ from unidecode import unidecode # Internal utilities from Src.Util.headers import get_headers -from Src.Util.console import console from Src.Util.table import TVShowManager -from ..Template import search_domain +from ..Template import search_domain, get_select_title # Logic class -from .Core.Class.SearchType import MediaManager, MediaItem +from .Core.Class.SearchType import MediaManager # Variable @@ -68,56 +67,8 @@ def title_search(title_search: str) -> int: return media_search_manager.get_length() -def get_select_title(type_filter: list = None) -> MediaItem: +def run_get_select_title(): """ Display a selection of titles and prompt the user to choose one. - - Args: - - type_filter (list): A list of media types to filter. Can include 'film', 'tv', 'ova'. Ex. ['tv', 'film'] - - Returns: - MediaItem: The selected media item. """ - - # Set up table for displaying titles - table_show_manager.set_slice_end(10) - - # Add columns to the table - column_info = { - "Index": {'color': 'red'}, - "Name": {'color': 'magenta'}, - "Type": {'color': 'yellow'}, - "Score": {'color': 'cyan'}, - } - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - - # Filter for only a list of category - if type_filter is not None: - if str(media.type) not in type_filter: - continue - - table_show_manager.add_tv_show({ - 'Index': str(i), - 'Name': media.name, - 'Type': media.type, - 'Score': media.score, - }) - - # 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() - - # Handle user's quit command - if last_command == "q": - console.print("\n[red]Quit [white]...") - sys.exit(0) - - # 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) + return get_select_title(table_show_manager, media_search_manager) \ No newline at end of file diff --git a/Src/Api/animeunity/Core/Class/EpisodeType.py b/Src/Api/animeunity/Core/Class/EpisodeType.py index 4483274..f63a358 100644 --- a/Src/Api/animeunity/Core/Class/EpisodeType.py +++ b/Src/Api/animeunity/Core/Class/EpisodeType.py @@ -9,22 +9,6 @@ from ...costant import SITE_NAME, DOMAIN_NOW -class Image: - def __init__(self, image_data: Dict[str, Any]): - self.id: int = image_data.get('id', '') - self.filename: str = image_data.get('filename', '') - self.type: str = image_data.get('type', '') - self.imageable_type: str = image_data.get('imageable_type', '') - self.imageable_id: int = image_data.get('imageable_id', '') - self.created_at: str = image_data.get('created_at', '') - self.updated_at: str = image_data.get('updated_at', '') - self.original_url_field: str = image_data.get('original_url_field', '') - self.url: str = f"https://cdn.{SITE_NAME}.{DOMAIN_NOW}/images/{self.filename}" - - def __str__(self): - return f"Image(id={self.id}, filename='{self.filename}', type='{self.type}', imageable_type='{self.imageable_type}', url='{self.url}')" - - class Episode: def __init__(self, data: Dict[str, Any]): self.id: int = data.get('id', '') @@ -37,7 +21,6 @@ class Episode: self.created_by: str = data.get('created_by', '') self.created_at: str = data.get('created_at', '') self.updated_at: str = data.get('updated_at', '') - self.images: List[Image] = [Image(image_data) for image_data in data.get('images', [])] def __str__(self): return f"Episode(id={self.id}, number={self.number}, name='{self.name}', plot='{self.plot}', duration={self.duration} sec)" diff --git a/Src/Api/animeunity/__init__.py b/Src/Api/animeunity/__init__.py index a8de4b7..77d699d 100644 --- a/Src/Api/animeunity/__init__.py +++ b/Src/Api/animeunity/__init__.py @@ -5,7 +5,7 @@ from Src.Util.console import console, msg # Logic class -from .site import title_search, get_select_title +from .site import title_search, run_get_select_title from .anime import donwload_film, donwload_series @@ -22,7 +22,7 @@ def search(): if len_database > 0: # Select title from list - select_title = get_select_title() + select_title = run_get_select_title() if select_title.type == 'TV': donwload_series( diff --git a/Src/Api/animeunity/site.py b/Src/Api/animeunity/site.py index ea83d79..7f8d6b6 100644 --- a/Src/Api/animeunity/site.py +++ b/Src/Api/animeunity/site.py @@ -1,6 +1,5 @@ # 10.12.23 -import sys import logging @@ -11,18 +10,16 @@ from unidecode import unidecode # Internal utilities -from Src.Util.console import console -from Src.Util._jsonConfig import config_manager from Src.Util.table import TVShowManager -from ..Template import search_domain +from ..Template import search_domain, get_select_title # Logic class -from .Core.Class.SearchType import MediaManager, MediaItem +from .Core.Class.SearchType import MediaManager # Variable -from .costant import SITE_NAME, DOMAIN_NOW +from .costant import SITE_NAME media_search_manager = MediaManager() table_show_manager = TVShowManager() @@ -142,58 +139,8 @@ def title_search(title: str) -> int: -def get_select_title(type_filter: list = None) -> MediaItem: +def run_get_select_title(): """ Display a selection of titles and prompt the user to choose one. - - Args: - - type_filter (list): A list of media types to filter. Can include 'film', 'tv', 'ova'. Ex. ['tv', 'film'] - - Returns: - MediaItem: The selected media item. """ - - # Set up table for displaying titles - table_show_manager.set_slice_end(10) - - # Add columns to the table - column_info = { - "Index": {'color': 'red'}, - "Name": {'color': 'magenta'}, - "Type": {'color': 'yellow'}, - "Score": {'color': 'cyan'}, - "Date": {'color': 'green'} - } - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - - # Filter for only a list of category - if type_filter is not None: - if str(media.type) not in type_filter: - continue - - table_show_manager.add_tv_show({ - 'Index': str(i), - 'Name': media.name, - 'Type': media.type, - 'Score': media.score, - 'Date': media.last_air_date - }) - - # 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() - - # Handle user's quit command - if last_command == "q": - console.print("\n[red]Quit [white]...") - sys.exit(0) - - # 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) + return get_select_title(table_show_manager, media_search_manager) \ No newline at end of file diff --git a/Src/Api/ddlstreamitaly/__init__.py b/Src/Api/ddlstreamitaly/__init__.py index 01eae2a..b3d75d1 100644 --- a/Src/Api/ddlstreamitaly/__init__.py +++ b/Src/Api/ddlstreamitaly/__init__.py @@ -9,7 +9,7 @@ from Src.Util.console import console, msg # Logic class -from .site import title_search, get_select_title +from .site import title_search, run_get_select_title from .series import download_thread @@ -30,7 +30,7 @@ def search(): if len_database > 0: # Select title from list - select_title = get_select_title() + select_title = run_get_select_title() # Download only film if "Serie TV" in str(select_title.type): diff --git a/Src/Api/ddlstreamitaly/site.py b/Src/Api/ddlstreamitaly/site.py index ad0495b..f54b03c 100644 --- a/Src/Api/ddlstreamitaly/site.py +++ b/Src/Api/ddlstreamitaly/site.py @@ -1,6 +1,5 @@ # 09.06.24 -import sys import logging @@ -10,10 +9,10 @@ from bs4 import BeautifulSoup # Internal utilities -from Src.Util.table import TVShowManager -from Src.Util.console import console, msg -from Src.Util._jsonConfig import config_manager from Src.Util.headers import get_headers +from Src.Util._jsonConfig import config_manager +from Src.Util.table import TVShowManager +from ..Template import search_domain, get_select_title # Logic class @@ -73,54 +72,8 @@ def title_search(word_to_search) -> int: return -9999 -def get_select_title(type_filter: list = None) -> MediaItem: +def run_get_select_title(): """ Display a selection of titles and prompt the user to choose one. - - Args: - - type_filter (list): A list of media types to filter. Can include 'film', 'tv', 'ova'. Ex. ['tv', 'film'] - - Returns: - MediaItem: The selected media item. """ - - # Set up table for displaying titles - table_show_manager.set_slice_end(10) - - # Add columns to the table - column_info = { - "Index": {'color': 'red'}, - "Name": {'color': 'magenta'}, - "Type": {'color': 'yellow'}, - } - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - - # Filter for only a list of category - if type_filter is not None: - if str(media.type) not in type_filter: - continue - - table_show_manager.add_tv_show({ - 'Index': str(i), - 'Name': media.name, - 'Type': media.type, - }) - - # 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() - - # Handle user's quit command - if last_command == "q": - console.print("\n[red]Quit [white]...") - sys.exit(0) - - # 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) + return get_select_title(table_show_manager, media_search_manager) \ No newline at end of file diff --git a/Src/Api/guardaserie/__init__.py b/Src/Api/guardaserie/__init__.py index f360c76..8faa635 100644 --- a/Src/Api/guardaserie/__init__.py +++ b/Src/Api/guardaserie/__init__.py @@ -5,7 +5,7 @@ from Src.Util.console import console, msg # Logic class -from .site import title_search, get_select_title +from .site import title_search, run_get_select_title from .series import download_series @@ -26,7 +26,7 @@ def search(): if len_database > 0: # Select title from list - select_title = get_select_title() + select_title = run_get_select_title() # Download only film download_series(select_title) diff --git a/Src/Api/guardaserie/site.py b/Src/Api/guardaserie/site.py index 74c6e3b..02d7905 100644 --- a/Src/Api/guardaserie/site.py +++ b/Src/Api/guardaserie/site.py @@ -1,6 +1,5 @@ # 09.06.24 -import sys import logging @@ -10,13 +9,13 @@ from bs4 import BeautifulSoup # Internal utilities -from Src.Util.table import TVShowManager -from Src.Util.console import console, msg from Src.Util.headers import get_headers +from Src.Util.table import TVShowManager +from ..Template import search_domain, get_select_title # Logic class -from .Core.Class.SearchType import MediaManager, MediaItem +from .Core.Class.SearchType import MediaManager # Variable @@ -60,56 +59,8 @@ def title_search(word_to_search) -> int: return media_search_manager.get_length() -def get_select_title(type_filter: list = None) -> MediaItem: +def run_get_select_title(): """ Display a selection of titles and prompt the user to choose one. - - Args: - - type_filter (list): A list of media types to filter. Can include 'film', 'tv', 'ova'. Ex. ['tv', 'film'] - - Returns: - MediaItem: The selected media item. """ - - # Set up table for displaying titles - table_show_manager.set_slice_end(10) - - # Add columns to the table - column_info = { - "Index": {'color': 'red'}, - "Name": {'color': 'magenta'}, - "Type": {'color': 'yellow'}, - "Score": {'color': 'cyan'}, - } - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - - # Filter for only a list of category - if type_filter is not None: - if str(media.type) not in type_filter: - continue - - table_show_manager.add_tv_show({ - 'Index': str(i), - 'Name': media.name, - 'Type': media.type, - 'Score': media.score, - }) - - # 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() - - # Handle user's quit command - if last_command == "q": - console.print("\n[red]Quit [white]...") - sys.exit(0) - - # 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) + return get_select_title(table_show_manager, media_search_manager) \ No newline at end of file diff --git a/Src/Api/streamingcommunity/Core/Class/EpisodeType.py b/Src/Api/streamingcommunity/Core/Class/EpisodeType.py index e98fd62..06d9f35 100644 --- a/Src/Api/streamingcommunity/Core/Class/EpisodeType.py +++ b/Src/Api/streamingcommunity/Core/Class/EpisodeType.py @@ -7,23 +7,6 @@ from typing import Dict, Any, List from ...costant import SITE_NAME, DOMAIN_NOW - -class Image: - def __init__(self, image_data: Dict[str, Any]): - self.id: int = image_data.get('id', '') - self.filename: str = image_data.get('filename', '') - self.type: str = image_data.get('type', '') - self.imageable_type: str = image_data.get('imageable_type', '') - self.imageable_id: int = image_data.get('imageable_id', '') - self.created_at: str = image_data.get('created_at', '') - self.updated_at: str = image_data.get('updated_at', '') - self.original_url_field: str = image_data.get('original_url_field', '') - self.url: str = f"https://cdn.{SITE_NAME}.{DOMAIN_NOW}/images/{self.filename}" - - def __str__(self): - return f"Image(id={self.id}, filename='{self.filename}', type='{self.type}', imageable_type='{self.imageable_type}', url='{self.url}')" - - class Episode: def __init__(self, data: Dict[str, Any]): self.id: int = data.get('id', '') @@ -36,7 +19,6 @@ class Episode: self.created_by: str = data.get('created_by', '') self.created_at: str = data.get('created_at', '') self.updated_at: str = data.get('updated_at', '') - self.images: List[Image] = [Image(image_data) for image_data in data.get('images', [])] def __str__(self): return f"Episode(id={self.id}, number={self.number}, name='{self.name}', plot='{self.plot}', duration={self.duration} sec)" diff --git a/Src/Api/streamingcommunity/Core/Class/SearchType.py b/Src/Api/streamingcommunity/Core/Class/SearchType.py index 1b7355f..17eefcc 100644 --- a/Src/Api/streamingcommunity/Core/Class/SearchType.py +++ b/Src/Api/streamingcommunity/Core/Class/SearchType.py @@ -8,19 +8,6 @@ from ...costant import SITE_NAME, DOMAIN_NOW -class Image: - def __init__(self, data: dict): - self.imageable_id: int = data.get('imageable_id') - self.imageable_type: str = data.get('imageable_type') - self.filename: str = data.get('filename') - self.type: str = data.get('type') - self.original_url_field: str = data.get('original_url_field') - self.url: str = f"https://cdn.{SITE_NAME}.{DOMAIN_NOW}/images/{self.filename}" - - def __str__(self): - return f"Image(imageable_id={self.imageable_id}, imageable_type='{self.imageable_type}', filename='{self.filename}', type='{self.type}', url='{self.url}')" - - class MediaItem: def __init__(self, data: dict): self.id: int = data.get('id') @@ -31,10 +18,9 @@ class MediaItem: self.sub_ita: int = data.get('sub_ita') self.last_air_date: str = data.get('last_air_date') self.seasons_count: int = data.get('seasons_count') - self.images: List[Image] = [Image(image_data) for image_data in data.get('images', [])] def __str__(self): - return f"MediaItem(id={self.id}, slug='{self.slug}', name='{self.name}', type='{self.type}', score='{self.score}', sub_ita={self.sub_ita}, last_air_date='{self.last_air_date}', seasons_count={self.seasons_count}, images={self.images})" + return f"MediaItem(id={self.id}, slug='{self.slug}', name='{self.name}', type='{self.type}', score='{self.score}', sub_ita={self.sub_ita}, last_air_date='{self.last_air_date}', seasons_count={self.seasons_count})" class MediaManager: diff --git a/Src/Api/streamingcommunity/__init__.py b/Src/Api/streamingcommunity/__init__.py index 9994906..9aaddf4 100644 --- a/Src/Api/streamingcommunity/__init__.py +++ b/Src/Api/streamingcommunity/__init__.py @@ -8,7 +8,7 @@ from Src.Util.console import console, msg from .site import ( get_version_and_domain, title_search, - get_select_title + run_get_select_title ) from .film import download_film @@ -35,7 +35,7 @@ def search(): if len_database > 0: # Select title from list - select_title = get_select_title() + select_title = run_get_select_title() # For series if select_title.type == 'tv': diff --git a/Src/Api/streamingcommunity/site.py b/Src/Api/streamingcommunity/site.py index 698f5c6..a0ee431 100644 --- a/Src/Api/streamingcommunity/site.py +++ b/Src/Api/streamingcommunity/site.py @@ -17,12 +17,12 @@ from unidecode import unidecode from Src.Util.headers import get_headers from Src.Util.console import console from Src.Util.table import TVShowManager -from ..Template import search_domain +from ..Template import search_domain, get_select_title # Logic class -from .Core.Class.SearchType import MediaManager, MediaItem +from .Core.Class.SearchType import MediaManager # Config @@ -111,58 +111,8 @@ def title_search(title_search: str, domain: str) -> int: return media_search_manager.get_length() -def get_select_title(type_filter: list = None) -> MediaItem: +def run_get_select_title(): """ Display a selection of titles and prompt the user to choose one. - - Args: - - type_filter (list): A list of media types to filter. Can include 'film', 'tv', 'ova'. Ex. ['tv', 'film'] - - Returns: - MediaItem: The selected media item. """ - - # Set up table for displaying titles - table_show_manager.set_slice_end(10) - - # Add columns to the table - column_info = { - "Index": {'color': 'red'}, - "Name": {'color': 'magenta'}, - "Type": {'color': 'yellow'}, - "Score": {'color': 'cyan'}, - "Date": {'color': 'green'} - } - table_show_manager.add_column(column_info) - - # Populate the table with title information - for i, media in enumerate(media_search_manager.media_list): - - # Filter for only a list of category - if type_filter is not None: - if str(media.type) not in type_filter: - continue - - table_show_manager.add_tv_show({ - 'Index': str(i), - 'Name': media.name, - 'Type': media.type, - 'Score': media.score, - 'Date': media.last_air_date - }) - - # 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() - - # Handle user's quit command - if last_command == "q": - console.print("\n[red]Quit [white]...") - sys.exit(0) - - # 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) + return get_select_title(table_show_manager, media_search_manager) \ No newline at end of file