v2 Add comment guardaserie.

This commit is contained in:
Lovi 2024-06-13 12:13:18 +02:00
parent 9794d50e79
commit 2b2317705b
6 changed files with 146 additions and 83 deletions

View File

@ -3,6 +3,8 @@
import sys import sys
import logging import logging
from typing import List, Dict
# External libraries # External libraries
import httpx import httpx
@ -20,45 +22,78 @@ from .SearchType import MediaItem
class GetSerieInfo: class GetSerieInfo:
def __init__(self, dict_serie: MediaItem = None) -> None: def __init__(self, dict_serie: MediaItem) -> None:
""" """
Initializes the VideoSource object with default values. Initializes the GetSerieInfo object with default values.
Attributes: Args:
headers (dict): An empty dictionary to store HTTP headers. dict_serie (MediaItem): Dictionary containing series information (optional).
""" """
self.headers = {'user-agent': get_headers()} self.headers = {'user-agent': get_headers()}
self.url = dict_serie.url self.url = dict_serie.url
self.tv_name = None self.tv_name = None
self.list_episodes = None self.list_episodes = None
self.list_episodes = None
def get_seasons_number(self): def get_seasons_number(self) -> int:
"""
Retrieves the number of seasons of a TV series.
Returns:
int: Number of seasons of the TV series.
"""
try:
# Make an HTTP request to the series URL
response = httpx.get(self.url, headers=self.headers) response = httpx.get(self.url, headers=self.headers)
response.raise_for_status()
# Create soup and find table # Parse HTML content of the page
soup = BeautifulSoup(response.text, "html.parser") soup = BeautifulSoup(response.text, "html.parser")
# Find the container of seasons
table_content = soup.find('div', class_="tt_season") table_content = soup.find('div', class_="tt_season")
# Count the number of seasons
seasons_number = len(table_content.find_all("li")) seasons_number = len(table_content.find_all("li"))
# Extract the name of the series
self.tv_name = soup.find("h1", class_="front_title").get_text(strip=True) self.tv_name = soup.find("h1", class_="front_title").get_text(strip=True)
return seasons_number return seasons_number
def get_episode_number(self, n_season: int): except Exception as e:
logging.error(f"Error parsing HTML page: {e}")
return -999
def get_episode_number(self, n_season: int) -> List[Dict[str, str]]:
"""
Retrieves the number of episodes for a specific season.
Args:
n_season (int): The season number.
Returns:
List[Dict[str, str]]: List of dictionaries containing episode information.
"""
try:
# Make an HTTP request to the series URL
response = httpx.get(self.url, headers=self.headers) response = httpx.get(self.url, headers=self.headers)
response.raise_for_status()
# Create soup and find table # Parse HTML content of the page
soup = BeautifulSoup(response.text, "html.parser") soup = BeautifulSoup(response.text, "html.parser")
# Find the container of episodes for the specified season
table_content = soup.find('div', class_="tab-pane", id=f"season-{n_season}") table_content = soup.find('div', class_="tab-pane", id=f"season-{n_season}")
# Scrape info episode # Extract episode information
episode_content = table_content.find_all("li") episode_content = table_content.find_all("li")
list_dict_episode = [] list_dict_episode = []
for episode_div in episode_content: for episode_div in episode_content:
index = episode_div.find("a").get("data-num") index = episode_div.find("a").get("data-num")
link = episode_div.find("a").get("data-link") link = episode_div.find("a").get("data-link")
name = episode_div.find("a").get("data-title") name = episode_div.find("a").get("data-title")
@ -68,8 +103,12 @@ class GetSerieInfo:
'name': name, 'name': name,
'url': link 'url': link
} }
list_dict_episode.append(obj_episode) list_dict_episode.append(obj_episode)
self.list_episodes = list_dict_episode self.list_episodes = list_dict_episode
return list_dict_episode return list_dict_episode
except Exception as e:
logging.error(f"Error parsing HTML page: {e}")
return []

View File

@ -7,6 +7,7 @@ from typing import List
# Internal utilities # Internal utilities
from Src.Util._jsonConfig import config_manager from Src.Util._jsonConfig import config_manager
from Src.Util.os import remove_special_characters
# Config # Config
@ -44,4 +45,27 @@ def manage_selection(cmd_insert: str, max_count: int) -> List[int]:
logging.info(f"List return: {list_season_select}") logging.info(f"List return: {list_season_select}")
return list_season_select return list_season_select
def map_episode_title(tv_name: str, number_season: int, episode_number: int, episode_name: str) -> str:
"""
Maps the episode title to a specific format.
Args:
tv_name (str): The name of the TV show.
number_season (int): The season number.
episode_number (int): The episode number.
episode_name (str): The original name of the episode.
Returns:
str: The mapped episode title.
"""
map_episode_temp = MAP_EPISODE
map_episode_temp = map_episode_temp.replace("%(tv_name)", remove_special_characters(tv_name))
map_episode_temp = map_episode_temp.replace("%(season)", str(number_season))
map_episode_temp = map_episode_temp.replace("%(episode)", str(episode_number))
map_episode_temp = map_episode_temp.replace("%(episode_name)", remove_special_characters(episode_name))
# Additional fix
map_episode_temp = map_episode_temp.replace(".", "_")
logging.info(f"Map episode string return: {map_episode_temp}")
return map_episode_temp

View File

@ -16,7 +16,7 @@ from Src.Lib.Hls.downloader import Downloader
# Logic class # Logic class
from .Core.Class.SearchType import MediaItem from .Core.Class.SearchType import MediaItem
from .Core.Class.ScrapeSerie import GetSerieInfo from .Core.Class.ScrapeSerie import GetSerieInfo
from .Core.Util.manage_ep import manage_selection from .Core.Util.manage_ep import manage_selection, map_episode_title
from .Core.Player.supervideo import VideoSource from .Core.Player.supervideo import VideoSource
@ -48,7 +48,7 @@ def donwload_video(scape_info_serie: GetSerieInfo, index_season_selected: int, i
print() print()
# Define filename and path for the downloaded video # Define filename and path for the downloaded video
mp4_name = f"{obj_episode.get('name')}.mp4" mp4_name = f"{map_episode_title(scape_info_serie.tv_name, index_season_selected, index_episode_selected, obj_episode.get('name'))}.mp4"
mp4_path = os.path.join(ROOT_PATH, MAIN_FOLDER, SERIES_FOLDER, scape_info_serie.tv_name, f"S{index_season_selected}") mp4_path = os.path.join(ROOT_PATH, MAIN_FOLDER, SERIES_FOLDER, scape_info_serie.tv_name, f"S{index_season_selected}")
# Setup video source # Setup video source

View File

@ -7,6 +7,7 @@ from typing import List
# Internal utilities # Internal utilities
from Src.Util._jsonConfig import config_manager from Src.Util._jsonConfig import config_manager
from Src.Util.os import remove_special_characters
# Logic class # Logic class
@ -62,10 +63,10 @@ def map_episode_title(tv_name: str, episode: Episode, number_season: int):
str: The mapped episode title. str: The mapped episode title.
""" """
map_episode_temp = MAP_EPISODE map_episode_temp = MAP_EPISODE
map_episode_temp = map_episode_temp.replace("%(tv_name)", tv_name) map_episode_temp = map_episode_temp.replace("%(tv_name)", remove_special_characters(tv_name))
map_episode_temp = map_episode_temp.replace("%(season)", str(number_season).zfill(2)) map_episode_temp = map_episode_temp.replace("%(season)", str(number_season).zfill(2))
map_episode_temp = map_episode_temp.replace("%(episode)", str(episode.number).zfill(2)) map_episode_temp = map_episode_temp.replace("%(episode)", str(episode.number).zfill(2))
map_episode_temp = map_episode_temp.replace("%(episode_name)", episode.name) map_episode_temp = map_episode_temp.replace("%(episode_name)", remove_special_characters(episode.name))
# Additional fix # Additional fix
map_episode_temp = map_episode_temp.replace(".", "_") map_episode_temp = map_episode_temp.replace(".", "_")

View File

@ -28,44 +28,6 @@ video_source = VideoSource()
table_show_manager = TVShowManager() table_show_manager = TVShowManager()
def display_episodes_list() -> str:
"""
Display episodes list and handle user input.
Returns:
last_command (str): Last command entered by the user.
"""
# Set up table for displaying episodes
table_show_manager.set_slice_end(10)
# Add columns to the table
column_info = {
"Index": {'color': 'red'},
"Name": {'color': 'magenta'},
"Duration": {'color': 'green'}
}
table_show_manager.add_column(column_info)
# Populate the table with episodes information
for i, media in enumerate(video_source.obj_episode_manager.episodes):
table_show_manager.add_tv_show({
'Index': str(media.number),
'Name': media.name,
'Duration': str(media.duration)
})
# Run the table and handle user input
last_command = table_show_manager.run()
if last_command == "q":
console.print("\n[red]Quit [white]...")
sys.exit(0)
return last_command
def donwload_video(tv_name: str, index_season_selected: int, index_episode_selected: int) -> None: def donwload_video(tv_name: str, index_season_selected: int, index_episode_selected: int) -> None:
""" """
Download a single episode video. Download a single episode video.
@ -187,3 +149,40 @@ def download_series(tv_id: str, tv_name: str, version: str, domain: str) -> None
else: else:
for i_season in list_season_select: for i_season in list_season_select:
donwload_episode(tv_name, i_season) donwload_episode(tv_name, i_season)
def display_episodes_list() -> str:
"""
Display episodes list and handle user input.
Returns:
last_command (str): Last command entered by the user.
"""
# Set up table for displaying episodes
table_show_manager.set_slice_end(10)
# Add columns to the table
column_info = {
"Index": {'color': 'red'},
"Name": {'color': 'magenta'},
"Duration": {'color': 'green'}
}
table_show_manager.add_column(column_info)
# Populate the table with episodes information
for i, media in enumerate(video_source.obj_episode_manager.episodes):
table_show_manager.add_tv_show({
'Index': str(media.number),
'Name': media.name,
'Duration': str(media.duration)
})
# Run the table and handle user input
last_command = table_show_manager.run()
if last_command == "q":
console.print("\n[red]Quit [white]...")
sys.exit(0)
return last_command