@@ -58,6 +58,8 @@ export class CompareBoletimUseCase {
5858 if ( isEqual ) {
5959 const relevantText = this . extractRelevantText ( storedFileText ) ;
6060
61+ // const ano = this.extractYear(relevantText);
62+
6163 const materias = this . extractMateriasAndNotas ( relevantText ) ;
6264
6365 console . log ( materias ) ;
@@ -99,54 +101,94 @@ export class CompareBoletimUseCase {
99101 }
100102
101103 private extractRelevantText ( text : string ) : string {
102- const start = text . indexOf ( " Resultado Final porComponente" ) ;
103- const end = text . indexOf ( " Assiduidade Parcial" ) ;
104-
105- if ( start === - 1 || end === - 1 ) {
104+ const startMatch = text . match ( / R e s u l t a d o \s * F i n a l \s * p o r \s * C o m p o n e n t e / i ) ;
105+ const endMatch = text . match ( / A s s i d u i d a d e \s * P a r c i a l / i ) ;
106+
107+ if ( ! startMatch || ! endMatch ) {
106108 throw new AppError ( "Texto do boletim incompleto ou não encontrado." ) ;
107109 }
108-
110+
111+ const start = startMatch . index ! + startMatch [ 0 ] . length ;
112+ const end = endMatch . index ! ;
113+
114+ return text . slice ( start , end ) . trim ( ) ;
115+ }
116+
117+ private extractYear ( text : string ) : string {
118+ const startMatch = text . match ( / A n o \s * L e t i v o \s * \/ \s * S e m e s t r e : \s * / i) ;
119+ const endMatch = text . match ( / \s * R M : / i) ;
120+
121+ if ( ! startMatch || ! endMatch ) {
122+ throw new AppError ( "Texto do boletim incompleto ou não encontrado." ) ;
123+ }
124+
125+ const start = startMatch . index ! + startMatch [ 0 ] . length ;
126+ const end = endMatch . index ! ;
127+
109128 return text . slice ( start , end ) . trim ( ) ;
110129 }
130+
111131
112132 private extractMateriasAndNotas ( text : string ) : any [ ] {
113- const materias = [ ] as any ;
133+ const materias : any [ ] = [ ] ;
114134 const lines = text . split ( / \r ? \n / ) . map ( line => line . trim ( ) ) . filter ( line => line . length > 0 ) ;
115-
116- lines . forEach ( line => {
117- const result = this . extractNotaFromLine ( line ) ;
135+
136+ let buffer = "" ;
137+
138+ lines . forEach ( ( line ) => {
139+ if ( ! / \d { 2 , 3 } / . test ( line ) || / ^ [ A - Z a - z \s ] + $ / . test ( line ) ) {
140+ buffer += ` ${ line } ` ;
141+ } else {
142+ buffer += ` ${ line } ` ;
143+ const result = this . extractNotaFromLine ( buffer . trim ( ) ) ;
144+ if ( result ) {
145+ materias . push ( result ) ;
146+ }
147+ buffer = "" ;
148+ }
149+ } ) ;
150+
151+ if ( buffer ) {
152+ const result = this . extractNotaFromLine ( buffer . trim ( ) ) ;
118153 if ( result ) {
119154 materias . push ( result ) ;
120155 }
121- } ) ;
122-
156+ }
157+
123158 return materias ;
124159 }
125160
126161 private extractNotaFromLine ( line : string ) : any | null {
127- // Separar a linha entre nome da matéria e as notas
128- const match = line . match ( / ^ ( . + ? ) ( \d { 2 , 3 } ) ( [ M B I R \- ] + ) $ / ) ;
162+ const match = line . match ( / ^ ( . + ? ) ( \d [ \d , ] * ) \s * ( . * ) / ) ;
163+
129164 if ( ! match ) return null ;
130165
131166 const nomeMateria = match [ 1 ] . trim ( ) ;
132- const notasRaw = match [ 3 ] . trim ( ) ;
133-
134- const bimestre1 = this . extractNota ( notasRaw . slice ( 0 , 2 ) ) ;
135- const bimestre2 = this . extractNota ( notasRaw . slice ( 2 , 4 ) ) ;
136-
167+
168+ const bimestres = [ ] ;
169+
170+ if ( match ) {
171+ const group3 = match [ 3 ] ;
172+
173+ let i = 0 ;
174+ while ( i < group3 . length && bimestres . length < 4 ) {
175+ if ( group3 [ i ] === 'M' && group3 [ i + 1 ] === 'B' ) {
176+ bimestres . push ( 'MB' ) ;
177+ i += 2 ;
178+ } else {
179+ bimestres . push ( group3 [ i ] ) ;
180+ i += 1 ;
181+ }
182+ }
183+
184+ console . log ( bimestres ) ;
185+ } else {
186+ throw new AppError ( `Erro ao comparar boletins` ) ;
187+ }
188+
137189 return {
138190 materia : nomeMateria ,
139- bimestre1,
140- bimestre2
191+ bimestres
141192 } ;
142193 }
143-
144- private extractNota ( notaRaw : string ) : string {
145- if ( notaRaw === 'MB' ) return 'MB' ;
146- if ( notaRaw [ 0 ] === 'M' ) return 'MB' ;
147- if ( notaRaw [ 0 ] === 'B' ) return 'B' ;
148- if ( notaRaw [ 0 ] === 'I' ) return 'I' ;
149- if ( notaRaw [ 0 ] === 'R' ) return 'R' ;
150- return '-' ;
151- }
152194}
0 commit comments