Dinamic get_select_title().

This commit is contained in:
Lovi 2024-06-19 22:29:57 +02:00
parent 6036bbeb20
commit 62a6c74b60
15 changed files with 125 additions and 331 deletions

View File

@ -1 +1,4 @@
# 19.06.24
from .site import get_select_title
from .Util.get_domain import search_domain

88
Src/Api/Template/site.py Normal file
View File

@ -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)

View File

@ -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(

View File

@ -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)

View File

@ -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)"

View File

@ -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(

View File

@ -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)

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)"

View File

@ -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:

View File

@ -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':

View File

@ -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)