Skip to content

Commit 56860c9

Browse files
authored
Long-Term Fix for Act numbers
In case the roman numerals go beyond 3, it will handle any roman numeral up to 100. And in case the episodes start using roman numerals.
1 parent bfae848 commit 56860c9

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

src/content.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,47 +70,83 @@ def get_act_episode_from_act_id(self, act_id):
7070
"act": None,
7171
"episode": None
7272
}
73-
74-
def parse_season_number(name):
75-
# Convert roman numerals to integers
76-
roman_map = {
73+
74+
def roman_to_int(roman):
75+
"""Convert a Roman numeral to an integer."""
76+
# Basic Roman numeral values
77+
roman_values = {
7778
'I': 1,
78-
'II': 2,
79-
'III': 3,
80-
'IV': 4,
81-
'V': 5
79+
'V': 5,
80+
'X': 10,
81+
'L': 50,
82+
'C': 100
8283
}
8384

84-
# For episodes (using regular numbers)
85+
total = 0
86+
prev_value = 0
87+
88+
# Process the Roman numeral from right to left
89+
for char in reversed(roman.upper()):
90+
if char not in roman_values:
91+
return None
92+
93+
current_value = roman_values[char]
94+
if current_value < prev_value:
95+
total -= current_value
96+
else:
97+
total += current_value
98+
prev_value = current_value
99+
100+
return total
101+
102+
def parse_season_number(name):
103+
"""Parse the season number from a name string.
104+
Handles both regular numbers and Roman numerals."""
105+
# Check for empty or invalid input
106+
if not name or not isinstance(name, str):
107+
return None
108+
109+
parts = name.split()
110+
if not parts:
111+
return None
112+
113+
number_part = parts[-1]
114+
115+
# For episodes (using regular numbers primarily)
85116
if name.startswith('EPISODE'):
86117
try:
87-
return int(name.split()[-1])
88-
except (ValueError, IndexError):
89-
return None
90-
91-
# For acts (using Roman numerals)
118+
return int(number_part)
119+
except ValueError:
120+
return roman_to_int(number_part)
121+
122+
# For acts (using Roman numerals primarily)
92123
elif name.startswith('ACT'):
124+
roman_result = roman_to_int(number_part)
125+
if roman_result is not None:
126+
return roman_result
127+
93128
try:
94-
roman_numeral = name.split()[-1]
95-
return roman_map.get(roman_numeral)
96-
except (KeyError, IndexError):
129+
return int(number_part)
130+
except ValueError:
97131
return None
98-
132+
99133
return None
100134

135+
# Process seasons to find act and episode
101136
act_found = False
102137
for season in self.content["Seasons"]:
103-
138+
# Check for matching act ID
104139
if season["ID"].lower() == act_id.lower():
105140
act_num = parse_season_number(season["Name"])
106141
if act_num is not None:
107142
final["act"] = act_num
108143
act_found = True
109-
144+
145+
# Find the first episode after the act
110146
if act_found and season["Type"] == "episode":
111147
episode_num = parse_season_number(season["Name"])
112148
if episode_num is not None:
113149
final["episode"] = episode_num
114150
break
115-
151+
116152
return final

0 commit comments

Comments
 (0)