@@ -70,47 +70,83 @@ def get_act_episode_from_act_id(self, act_id):
70
70
"act" : None ,
71
71
"episode" : None
72
72
}
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 = {
77
78
'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
82
83
}
83
84
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)
85
116
if name .startswith ('EPISODE' ):
86
117
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 )
92
123
elif name .startswith ('ACT' ):
124
+ roman_result = roman_to_int (number_part )
125
+ if roman_result is not None :
126
+ return roman_result
127
+
93
128
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 :
97
131
return None
98
-
132
+
99
133
return None
100
134
135
+ # Process seasons to find act and episode
101
136
act_found = False
102
137
for season in self .content ["Seasons" ]:
103
-
138
+ # Check for matching act ID
104
139
if season ["ID" ].lower () == act_id .lower ():
105
140
act_num = parse_season_number (season ["Name" ])
106
141
if act_num is not None :
107
142
final ["act" ] = act_num
108
143
act_found = True
109
-
144
+
145
+ # Find the first episode after the act
110
146
if act_found and season ["Type" ] == "episode" :
111
147
episode_num = parse_season_number (season ["Name" ])
112
148
if episode_num is not None :
113
149
final ["episode" ] = episode_num
114
150
break
115
-
151
+
116
152
return final
0 commit comments