Skip to content

Commit d5eb856

Browse files
committed
Fix SeasonManager for "guardaserie"
1 parent cae0832 commit d5eb856

File tree

2 files changed

+76
-34
lines changed

2 files changed

+76
-34
lines changed

StreamingCommunity/Api/Site/guardaserie/series.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
extension_output = config_manager.get("M3U8_CONVERSION", "extension")
4141

4242

43-
def download_video(index_season_selected: int, index_episode_selected: int, scape_info_serie: GetSerieInfo) -> Tuple[str,bool]:
43+
def download_video(index_season_selected: int, index_episode_selected: int, scrape_serie: GetSerieInfo) -> Tuple[str,bool]:
4444
"""
4545
Downloads a specific episode from a specified season.
4646
4747
Parameters:
4848
- index_season_selected (int): Season number
4949
- index_episode_selected (int): Episode index
50-
- scape_info_serie (GetSerieInfo): Scraper object with series information
50+
- scrape_serie (GetSerieInfo): Scraper object with series information
5151
5252
Returns:
5353
- str: Path to downloaded file
@@ -56,13 +56,13 @@ def download_video(index_season_selected: int, index_episode_selected: int, scap
5656
start_message()
5757

5858
# Get episode information
59-
obj_episode = scape_info_serie.selectEpisode(index_season_selected, index_episode_selected-1)
60-
index_season_selected = dynamic_format_number(str(index_season_selected))
61-
console.print(f"\n[bold yellow]Download:[/bold yellow] [red]{site_constant.SITE_NAME}[/red] → [cyan]{scape_info_serie.tv_name}[/cyan] \\ [bold magenta]{obj_episode.get('name')}[/bold magenta] ([cyan]S{index_season_selected}E{index_episode_selected}[/cyan]) \n")
59+
obj_episode = scrape_serie.selectEpisode(index_season_selected, index_episode_selected-1)
60+
index_season_selected_formatted = dynamic_format_number(str(index_season_selected))
61+
console.print(f"\n[bold yellow]Download:[/bold yellow] [red]{site_constant.SITE_NAME}[/red] → [cyan]{scrape_serie.tv_name}[/cyan] \\ [bold magenta]{obj_episode.get('name')}[/bold magenta] ([cyan]S{index_season_selected_formatted}E{index_episode_selected}[/cyan]) \n")
6262

6363
# Define filename and path for the downloaded video
64-
mp4_name = f"{map_episode_title(scape_info_serie.tv_name, index_season_selected, index_episode_selected, obj_episode.get('name'))}.{extension_output}"
65-
mp4_path = os.path.join(site_constant.SERIES_FOLDER, scape_info_serie.tv_name, f"S{index_season_selected}")
64+
mp4_name = f"{map_episode_title(scrape_serie.tv_name, index_season_selected_formatted, index_episode_selected, obj_episode.get('name'))}.{extension_output}"
65+
mp4_path = os.path.join(site_constant.SERIES_FOLDER, scrape_serie.tv_name, f"S{index_season_selected_formatted}")
6666

6767
# Setup video source
6868
video_source = VideoSource(obj_episode.get('url'))
@@ -85,40 +85,38 @@ def download_video(index_season_selected: int, index_episode_selected: int, scap
8585
return hls_process['path'], hls_process['stopped']
8686

8787

88-
def download_episode(scape_info_serie: GetSerieInfo, index_season_selected: int, download_all: bool = False, episode_selection: str = None) -> None:
88+
def download_episode(index_season_selected: int, scrape_serie: GetSerieInfo, download_all: bool = False, episode_selection: str = None) -> None:
8989
"""
9090
Handle downloading episodes for a specific season.
9191
9292
Parameters:
93-
- scape_info_serie (GetSerieInfo): Scraper object with series information
9493
- index_season_selected (int): Season number
94+
- scrape_serie (GetSerieInfo): Scraper object with series information
9595
- download_all (bool): Whether to download all episodes
9696
- episode_selection (str, optional): Pre-defined episode selection that bypasses manual input
9797
"""
98-
# Get episodes for the selected season
99-
episodes = scape_info_serie.get_episode_number(index_season_selected)
98+
# Get episodes for the selected season using the new method
99+
episodes = scrape_serie.getEpisodeSeasons(index_season_selected)
100100
episodes_count = len(episodes)
101101

102102
if episodes_count == 0:
103103
console.print(f"[red]No episodes found for season {index_season_selected}")
104104
return
105105

106106
if download_all:
107-
108107
# Download all episodes in the season
109108
for i_episode in range(1, episodes_count + 1):
110-
path, stopped = download_video(index_season_selected, i_episode, scape_info_serie)
109+
path, stopped = download_video(index_season_selected, i_episode, scrape_serie)
111110

112111
if stopped:
113112
break
114113

115114
console.print(f"\n[red]End downloaded [yellow]season: [red]{index_season_selected}.")
116115

117116
else:
118-
119117
# Display episodes list and manage user selection
120118
if episode_selection is None:
121-
last_command = display_episodes_list(scape_info_serie.list_episodes)
119+
last_command = display_episodes_list(episodes)
122120
else:
123121
last_command = episode_selection
124122
console.print(f"\n[cyan]Using provided episode selection: [yellow]{episode_selection}")
@@ -129,7 +127,7 @@ def download_episode(scape_info_serie: GetSerieInfo, index_season_selected: int,
129127

130128
# Download selected episodes
131129
for i_episode in list_episode_select:
132-
path, stopped = download_video(index_season_selected, i_episode, scape_info_serie)
130+
path, stopped = download_video(index_season_selected, i_episode, scrape_serie)
133131

134132
if stopped:
135133
break
@@ -148,7 +146,9 @@ def download_series(dict_serie: MediaItem, season_selection: str = None, episode
148146

149147
# Create class
150148
scrape_serie = GetSerieInfo(dict_serie)
151-
seasons_count = scrape_serie.get_seasons_number()
149+
150+
# Get number of seasons
151+
seasons_count = scrape_serie.getNumberSeason()
152152

153153
# If season_selection is provided, use it instead of asking for input
154154
if season_selection is None:
@@ -163,7 +163,19 @@ def download_series(dict_serie: MediaItem, season_selection: str = None, episode
163163

164164
# Loop through the selected seasons and download episodes
165165
for i_season in list_season_select:
166-
if len(list_season_select) > 1 or index_season_selected == "*":
167-
download_episode(scrape_serie, i_season, download_all=True)
168-
else:
169-
download_episode(scrape_serie, i_season, download_all=False, episode_selection=episode_selection)
166+
try:
167+
season = scrape_serie.seasons_manager.get_season_by_number(i_season)
168+
if not season:
169+
console.print(f"[red]Season {i_season} not found! Available seasons: {[s.number for s in scrape_serie.seasons_manager.seasons]}")
170+
continue
171+
172+
season_number = season.number
173+
174+
if len(list_season_select) > 1 or index_season_selected == "*":
175+
download_episode(season_number, scrape_serie, download_all=True)
176+
else:
177+
download_episode(season_number, scrape_serie, download_all=False, episode_selection=episode_selection)
178+
179+
except Exception as e:
180+
console.print(f"[red]Error processing season {i_season}: {str(e)}")
181+
continue

StreamingCommunity/Api/Site/guardaserie/util/ScrapeSerie.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@
1111
# Internal utilities
1212
from StreamingCommunity.Util.headers import get_userAgent
1313
from StreamingCommunity.Util.http_client import create_client
14+
from StreamingCommunity.Api.Player.Helper.Vixcloud.util import SeasonManager
1415

1516

1617
# Logic class
1718
from StreamingCommunity.Api.Template.Class.SearchType import MediaItem
1819

1920

20-
2121
class GetSerieInfo:
2222
def __init__(self, dict_serie: MediaItem) -> None:
2323
"""
2424
Initializes the GetSerieInfo object with default values.
2525
2626
Parameters:
27-
dict_serie (MediaItem): Dictionary containing series information (optional).
27+
dict_serie (MediaItem): Dictionary containing series information.
2828
"""
2929
self.headers = {'user-agent': get_userAgent()}
3030
self.url = dict_serie.url
3131
self.tv_name = None
32-
self.list_episodes = None
32+
self.seasons_manager = SeasonManager()
3333

3434
def get_seasons_number(self) -> int:
3535
"""
36-
Retrieves the number of seasons of a TV series.
36+
Retrieves the number of seasons of a TV series and populates the seasons_manager.
3737
3838
Returns:
3939
int: Number of seasons of the TV series. Returns -1 if parsing fails.
@@ -45,20 +45,30 @@ def get_seasons_number(self) -> int:
4545
# Find the seasons container
4646
soup = BeautifulSoup(response.text, "html.parser")
4747
table_content = soup.find('div', class_="tt_season")
48-
seasons_number = len(table_content.find_all("li"))
48+
season_elements = table_content.find_all("li")
4949

5050
# Try to get the title, with fallback
5151
self.tv_name = soup.find('h1', class_="front_title").get_text(strip=True) if soup.find('h1', class_="front_title") else "Unknown Series"
5252

53-
return seasons_number
53+
# Clear existing seasons and add new ones to SeasonManager
54+
self.seasons_manager.seasons = []
55+
for idx, season_element in enumerate(season_elements, start=1):
56+
self.seasons_manager.add_season({
57+
'id': idx,
58+
'number': idx,
59+
'name': f"Season {idx}",
60+
'slug': f"season-{idx}",
61+
})
62+
63+
return len(season_elements)
5464

5565
except Exception as e:
5666
logging.error(f"Error parsing HTML page: {str(e)}")
5767
return -1
5868

5969
def get_episode_number(self, n_season: int) -> List[Dict[str, str]]:
6070
"""
61-
Retrieves the number of episodes for a specific season.
71+
Retrieves the episodes for a specific season.
6272
6373
Parameters:
6474
n_season (int): The season number.
@@ -78,6 +88,12 @@ def get_episode_number(self, n_season: int) -> List[Dict[str, str]]:
7888
episode_content = table_content.find_all("li")
7989
list_dict_episode = []
8090

91+
# Get the season from seasons_manager
92+
season = self.seasons_manager.get_season_by_number(n_season)
93+
94+
if season:
95+
season.episodes.episodes = []
96+
8197
for episode_div in episode_content:
8298
index = episode_div.find("a").get("data-num")
8399
link = episode_div.find("a").get("data-link")
@@ -86,33 +102,47 @@ def get_episode_number(self, n_season: int) -> List[Dict[str, str]]:
86102
obj_episode = {
87103
'number': index,
88104
'name': name,
89-
'url': link
105+
'url': link,
106+
'id': index
90107
}
91108

92109
list_dict_episode.append(obj_episode)
110+
111+
# Add episode to the season in seasons_manager
112+
if season:
113+
season.episodes.add(obj_episode)
93114

94-
self.list_episodes = list_dict_episode
95115
return list_dict_episode
96116

97117
except Exception as e:
98118
logging.error(f"Error parsing HTML page: {e}")
99119

100120
return []
101121

102-
103122
# ------------- FOR GUI -------------
104123
def getNumberSeason(self) -> int:
105124
"""
106125
Get the total number of seasons available for the series.
107126
"""
108-
return self.get_seasons_number()
127+
if not self.seasons_manager.seasons:
128+
return self.get_seasons_number()
129+
return len(self.seasons_manager.seasons)
109130

110131
def getEpisodeSeasons(self, season_number: int) -> list:
111132
"""
112133
Get all episodes for a specific season.
113134
"""
114-
episodes = self.get_episode_number(season_number)
115-
return episodes
135+
season = self.seasons_manager.get_season_by_number(season_number)
136+
137+
if not season:
138+
logging.error(f"Season {season_number} not found")
139+
return []
140+
141+
# If episodes are not loaded yet, fetch them
142+
if not season.episodes.episodes:
143+
self.get_episode_number(season_number)
144+
145+
return season.episodes.episodes
116146

117147
def selectEpisode(self, season_number: int, episode_index: int) -> dict:
118148
"""

0 commit comments

Comments
 (0)