@@ -125,19 +125,6 @@ function useId(id: string): string {
125
125
return `#${ id . replace ( / : / g, "\\:" ) } ` ;
126
126
}
127
127
128
- const puppetConfig : LaunchOptions = {
129
- headless : true ,
130
- args : [
131
- "--no-sandbox" ,
132
- "--disable-setuid-sandbox" ,
133
- "--disable-dev-shm-usage" ,
134
- "--disable-accelerated-2d-canvas" ,
135
- "--no-first-run" ,
136
- "--no-zygote" ,
137
- "--disable-gpu" ,
138
- ] ,
139
- } ;
140
-
141
128
/**
142
129
* Scrape course from old SIA
143
130
*
@@ -181,6 +168,19 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
181
168
const updatedByRef = serverFirestore . doc ( auth . id ) ;
182
169
let attemp = 0 ;
183
170
171
+ const puppetConfig : LaunchOptions = {
172
+ headless : ! debugScrapper ,
173
+ args : [
174
+ "--no-sandbox" ,
175
+ "--disable-setuid-sandbox" ,
176
+ "--disable-dev-shm-usage" ,
177
+ "--disable-accelerated-2d-canvas" ,
178
+ "--no-first-run" ,
179
+ "--no-zygote" ,
180
+ "--disable-gpu" ,
181
+ ] ,
182
+ } ;
183
+
184
184
// Setup puppeteer
185
185
const puppetBrowser : Browser = await launch ( puppetConfig ) ;
186
186
const puppetPage : Page = await puppetBrowser . newPage ( ) ;
@@ -392,81 +392,124 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
392
392
place : eSIAPlace ,
393
393
faculties : uSIAFaculty [ ] = [ ] ,
394
394
programs : uSIAProgram [ ] = [ ] ,
395
- typologies : eSIATypology [ ] = [ ]
395
+ typologies : eSIATypology [ ] = [ ] ,
396
+ programsLE : uSIAProgram [ ] = [ ...programs ]
396
397
) : Promise < ScrapedCourse > {
397
398
// Compare against multiple sources before giving up
398
- const LE = typologies . includes ( eSIATypology . LIBRE_ELECCIÓN ) ;
399
- const LEByProgram = ! programs [ 0 ] ;
400
399
const LEByFaculty = ! faculties [ 0 ] ;
400
+ const LEByProgram = ! programs [ 0 ] ;
401
+ const LEByTypology = typologies . includes ( eSIATypology . LIBRE_ELECCIÓN ) ;
401
402
402
403
// Override data if missing, assume LE
403
- if ( LEByProgram || LE || LEByFaculty ) {
404
+ if ( LEByProgram || LEByFaculty || LEByTypology ) {
404
405
if ( ! typologies . includes ( eSIATypology . LIBRE_ELECCIÓN ) ) {
405
406
typologies . push ( eSIATypology . LIBRE_ELECCIÓN ) ;
406
407
}
407
408
408
409
switch ( place ) {
409
410
case eSIAPlace . BOGOTÁ :
410
411
if ( LEByFaculty ) faculties . push ( eSIABogotaFaculty . SEDE_BOGOTÁ ) ;
411
- if ( LEByProgram || LE ) {
412
+ if ( LEByProgram ) {
413
+ // Try LE last
412
414
programs . push ( eSIABogotaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
413
415
}
416
+ if ( LEByTypology ) {
417
+ // Try LE first
418
+ programsLE . unshift ( eSIABogotaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
419
+ }
414
420
415
421
break ;
416
422
case eSIAPlace . LA_PAZ :
417
423
if ( LEByFaculty ) faculties . push ( eSIALaPazFaculty . SEDE_LA_PAZ ) ;
418
- if ( LEByProgram || LE ) {
424
+ if ( LEByProgram ) {
425
+ // Try LE last
419
426
programs . push ( eSIALaPazProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
420
427
}
428
+ if ( LEByTypology ) {
429
+ // Try LE first
430
+ programsLE . unshift ( eSIALaPazProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
431
+ }
421
432
422
433
break ;
423
434
case eSIAPlace . MEDELLÍN :
424
435
if ( LEByFaculty ) faculties . push ( eSIAMedellinFaculty . SEDE_MEDELLÍN ) ;
425
436
if ( LEByProgram ) {
437
+ // Try LE last
426
438
programs . push ( eSIAMedellinProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
427
439
}
440
+ if ( LEByTypology ) {
441
+ // Try LE first
442
+ programsLE . unshift ( eSIAMedellinProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
443
+ }
428
444
429
445
break ;
430
446
case eSIAPlace . MANIZALES :
431
447
if ( LEByFaculty ) faculties . push ( eSIAManizalesFaculty . SEDE_MANIZALES ) ;
432
448
if ( LEByProgram ) {
449
+ // Try LE last
433
450
programs . push ( eSIAManizalesProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
434
451
}
452
+ if ( LEByTypology ) {
453
+ // Try LE first
454
+ programsLE . unshift ( eSIAManizalesProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
455
+ }
435
456
436
457
break ;
437
458
case eSIAPlace . PALMIRA :
438
459
if ( LEByFaculty ) faculties . push ( eSIAPalmiraFaculty . SEDE_PALMIRA ) ;
439
- if ( LEByProgram || LE ) {
440
- programs . push ( eSIAPalmiraProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
460
+ if ( LEByProgram ) programs . push ( eSIAPalmiraProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
461
+ if ( LEByTypology ) {
462
+ // Try LE first
463
+ programsLE . unshift ( eSIAPalmiraProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
441
464
}
442
465
443
466
break ;
444
467
case eSIAPlace . TUMACO :
445
468
if ( LEByFaculty ) faculties . push ( eSIATumacoFaculty . SEDE_TUMACO ) ;
446
- if ( LEByProgram || LE ) {
469
+ if ( LEByProgram ) {
470
+ // Try LE last
447
471
programs . push ( eSIATumacoProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
448
472
}
473
+ if ( LEByTypology ) {
474
+ // Try LE first
475
+ programsLE . unshift ( eSIATumacoProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
476
+ }
449
477
450
478
break ;
451
479
case eSIAPlace . AMAZONÍA :
452
480
if ( LEByFaculty ) faculties . push ( eSIAAmazoniaFaculty . SEDE_AMAZONIA ) ;
453
481
if ( LEByProgram ) {
482
+ // Try LE last
454
483
programs . push ( eSIAAmazoniaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
455
484
}
485
+ if ( LEByTypology ) {
486
+ // Try LE first
487
+ programsLE . unshift ( eSIAAmazoniaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
488
+ }
456
489
457
490
break ;
458
491
case eSIAPlace . CARIBE :
459
492
if ( LEByFaculty ) faculties . push ( eSIACaribeFaculty . SEDE_CARIBE ) ;
460
- if ( LEByProgram || LE ) {
493
+ if ( LEByProgram ) {
494
+ // Try LE last
461
495
programs . push ( eSIACaribeProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
462
496
}
497
+ if ( LEByTypology ) {
498
+ // Try LE first
499
+ programsLE . unshift ( eSIACaribeProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
500
+ }
463
501
464
502
break ;
465
503
case eSIAPlace . ORINOQUÍA :
466
504
if ( LEByFaculty ) faculties . push ( eSIAOrinoquiaFaculty . SEDE_ORINOQUIA ) ;
467
505
if ( LEByProgram ) {
506
+ // Try LE last
468
507
programs . push ( eSIAOrinoquiaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
469
508
}
509
+ if ( LEByTypology ) {
510
+ // Try LE first
511
+ programsLE . unshift ( eSIAOrinoquiaProgram . COMPONENTE_DE_LIBRE_ELECCIÓN ) ;
512
+ }
470
513
471
514
break ;
472
515
}
@@ -565,11 +608,12 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
565
608
566
609
// Attempt to get groups from this program
567
610
if ( ! typologies . length ) {
568
- const { errors = [ ] , ...partialResponse } = await getResponse ( ) ;
611
+ const results = await getResponse ( ) ;
612
+ const { errors = [ ] , ...partial } = results ;
569
613
570
614
response . errors ?. push ( ...errors ) ;
571
615
response = {
572
- ...partialResponse ,
616
+ ...partial ,
573
617
lastScrapedWith : [
574
618
level ,
575
619
place ,
@@ -612,6 +656,9 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
612
656
typologyValue . value
613
657
) ;
614
658
659
+ // Necessary for switching between typologies
660
+ await puppetPage . waitForNetworkIdle ( ) ;
661
+
615
662
// Additional actions for LE
616
663
if ( typology === eSIATypology . LIBRE_ELECCIÓN ) {
617
664
await puppetPage . waitForSelector (
@@ -632,49 +679,95 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
632
679
puppetPage ,
633
680
eIds . PROGRAM_LE
634
681
) ;
635
- const programLeValue = programOptionsLE . find (
682
+ const programLeValues = programOptionsLE . filter (
636
683
( { alias } ) => {
637
684
return (
638
685
alias &&
639
- programs . includes ( < uSIAProgram > alias )
686
+ programsLE . includes ( < uSIAProgram > alias )
640
687
) ;
641
688
}
642
689
) ;
643
690
644
- if ( ! programLeValue ) {
645
- throw new Error ( "LE program not found" ) ;
691
+ if ( ! programLeValues . length ) {
692
+ throw new Error ( "LE programs not found" ) ;
646
693
}
647
694
648
- await puppetPage . click ( useId ( eIds . PROGRAM_LE ) ) ;
649
- await puppetPage . select (
650
- useId ( eIds . PROGRAM_LE ) ,
651
- programLeValue . value
652
- ) ;
695
+ // Iterate over associated LE programs
696
+ for ( const programLeValue of programLeValues ) {
697
+ if ( response . code ) break ;
698
+
699
+ debugFirebaseServer (
700
+ event ,
701
+ "api:groups:scrape:scraper:typology:LE" ,
702
+ [
703
+ facultyValue . alias ,
704
+ programValue . alias ,
705
+ typology ,
706
+ programLeValue . alias ,
707
+ ] ,
708
+ response . groups ?. length
709
+ ) ;
710
+
711
+ try {
712
+ // Search by LE program
713
+ await puppetPage . click ( useId ( eIds . PROGRAM_LE ) ) ;
714
+ await puppetPage . select (
715
+ useId ( eIds . PROGRAM_LE ) ,
716
+ programLeValue . value
717
+ ) ;
653
718
654
- debugFirebaseServer (
655
- event ,
656
- "api:groups:scrape:scraper:typology:LE" ,
657
- programLeValue
658
- ) ;
719
+ // Necessary for switching between LE programs
720
+ await puppetPage . waitForNetworkIdle ( ) ;
721
+
722
+ // Attempt to get groups from this LE program
723
+ const results = await getResponse ( ) ;
724
+ const { errors = [ ] , ...partial } = results ;
725
+
726
+ response . errors ?. push ( ...errors ) ;
727
+ response = {
728
+ ...partial ,
729
+ lastScrapedWith : [
730
+ level ,
731
+ place ,
732
+ < uSIAFaculty > facultyValue . alias ,
733
+ < uSIAProgram > programValue . alias ,
734
+ typology ,
735
+ < uSIAProgram > programLeValue . alias ,
736
+ ] ,
737
+ } ;
738
+ } catch ( err ) {
739
+ response . errors ?. push ( err ) ; // save LE programs errors
740
+
741
+ debugFirebaseServer (
742
+ event ,
743
+ "api:groups:scrape:scraper:typology:LEError" ,
744
+ [
745
+ facultyValue . alias ,
746
+ programValue . alias ,
747
+ typology ,
748
+ programLeValue . alias ,
749
+ ] ,
750
+ err
751
+ ) ;
752
+ }
753
+ }
754
+ } else {
755
+ // Attempt to get groups from this typology
756
+ const results = await getResponse ( ) ;
757
+ const { errors = [ ] , ...partial } = results ;
758
+
759
+ response . errors ?. push ( ...errors ) ;
760
+ response = {
761
+ ...partial ,
762
+ lastScrapedWith : [
763
+ level ,
764
+ place ,
765
+ < uSIAFaculty > facultyValue . alias ,
766
+ < uSIAProgram > programValue . alias ,
767
+ typology ,
768
+ ] ,
769
+ } ;
659
770
}
660
-
661
- // Necessary for switching between typologies
662
- await puppetPage . waitForNetworkIdle ( ) ;
663
-
664
- // Attempt to get groups from this typology
665
- const { errors = [ ] , ...partial } = await getResponse ( ) ;
666
-
667
- response . errors ?. push ( ...errors ) ;
668
- response = {
669
- ...partial ,
670
- lastScrapedWith : [
671
- level ,
672
- place ,
673
- < uSIAFaculty > facultyValue . alias ,
674
- < uSIAProgram > programValue . alias ,
675
- typology ,
676
- ] ,
677
- } ;
678
771
} catch ( err ) {
679
772
response . errors ?. push ( err ) ; // save typologies errors
680
773
@@ -742,12 +835,20 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
742
835
if ( ! lastScrapedWith ) throw new Error ( "LastScrapedWith is not defined" ) ;
743
836
744
837
// Try last scraped with
745
- const [ level , place , faculty , program , typology ] = lastScrapedWith ;
838
+ const [ level , place , faculty , program , typology , programLE ] = lastScrapedWith ;
746
839
const faculties = faculty ? [ faculty ] : [ ] ;
747
840
const programs = program ? [ program ] : [ ] ;
748
841
const typologies = typology ? [ typology ] : [ ] ;
749
-
750
- SIAScraps = await scraper ( level , place , faculties , programs , typologies ) ;
842
+ const programsLE = programLE ? [ programLE ] : [ ] ;
843
+
844
+ SIAScraps = await scraper (
845
+ level ,
846
+ place ,
847
+ faculties ,
848
+ programs ,
849
+ typologies ,
850
+ programsLE
851
+ ) ;
751
852
} catch ( err ) {
752
853
const {
753
854
level = eSIALevel . PREGRADO ,
@@ -766,7 +867,8 @@ export default defineConditionallyCachedEventHandler(async (event, instance, aut
766
867
debugFirebaseServer (
767
868
event ,
768
869
"api:groups:scrape:updateCourse" ,
769
- `Success scraping: ${ code } , ${ SIAScraps . name } `
870
+ `Success scraping: ${ code } , ${ SIAScraps . name } ` ,
871
+ SIAScraps . groups
770
872
) ;
771
873
772
874
// Prevent updating if missing groups
0 commit comments