Skip to content

Commit

Permalink
Merge pull request #67 from joanasesinando/feature/optimize_algorithm…
Browse files Browse the repository at this point in the history
…_periods_mismatch

Optimized algorithm to obtain periods when names mismatch
  • Loading branch information
joanasesinando authored Sep 5, 2023
2 parents 559e8e9 + 1b4a3d5 commit 6cc69ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gerador-horarios-ist",
"version": "1.4.15",
"version": "1.4.16",
"description": "Gerador de horários para o Instituto Superior Técnico.",
"homepage": "https://web.tecnico.ulisboa.pt/joanasesinando/gerador-horarios",
"license": "MIT. See license in https://github.com/joanasesinando/gerador-horarios-ist/blob/master/LICENSE",
Expand Down
33 changes: 24 additions & 9 deletions src/app/_services/fenix/fenix.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ export class FenixService {
return new Degree(parseInt(degreeJson.id, 10), degreeJson.name, degreeJson.acronym);
}

async parseCourseBasicInfo(academicTerm: string, course, htmlCurriculum: HTMLHtmlElement, coursesNames): Promise<Course> {
async parseCourseBasicInfo(academicTerm: string, course, htmlCurriculum: HTMLHtmlElement, coursesNames,
cachedLinksHtml: {}): Promise<Course> {
if (!course.id) throw new Error('No ID found for course');
if (!course.name) throw new Error('No name found for course ' + course.id);
if (!course.acronym) throw new Error('No acronym found for course ' + course.id);
if (!course.credits) throw new Error('No credits found for course ' + course.id);
if (!course.academicTerm) throw new Error('No academic term found for course ' + course.id);

const period = await this.parseCoursePeriod(academicTerm, htmlCurriculum, course, coursesNames);
const period = await this.parseCoursePeriod(academicTerm, htmlCurriculum, course, coursesNames, cachedLinksHtml);

return new Course(parseInt(course.id, 10), course.name, course.acronym, parseFloat(course.credits),
parseInt(course.academicTerm[0], 10), period);
}

async parseCoursePeriod(academicTerm: string, htmlCurriculum: HTMLHtmlElement, course, coursesNames: string[]): Promise<string> {
async parseCoursePeriod(academicTerm: string, htmlCurriculum: HTMLHtmlElement, course, coursesNames: string[],
cachedLinksHtml: {}): Promise<string> {
if (!this.isMEPPAcademicTerm(academicTerm)) return null;

let courseName = course.name;
Expand All @@ -75,7 +77,7 @@ export class FenixService {
try {
text = $('a:contains(\'' + courseName + '\') + div', htmlCurriculum)[0].innerText;
} catch (error) {
const courseInfo: {[id: string]: string} = {};
let courseInfo = '';
if (courseURL === '') {
await this.httpGet('courses/' + course.id)
.then(r => r.json())
Expand All @@ -95,17 +97,29 @@ export class FenixService {
const textElement: HTMLElement = div.children[1] as HTMLElement;
const info = textElement.innerText;

if (link in cachedLinksHtml) {
if ($('a[href$="' + courseURL + '"]', cachedLinksHtml[link]).length > 0) {
courseInfo = info;
break;
} else {
continue;
}
}

promises.push(this.getCourseCurriculumPage(link).then(htmlLink => {
cachedLinksHtml[link] = htmlLink;
if ($('a[href$="' + courseURL + '"]', htmlLink).length > 0) {
courseInfo[course.id] = info;
courseInfo = info;
} else {
return Promise.reject();
}
}));
}

await Promise.all(promises);
await Promise.any(promises).catch(() => null);

if (course.id in courseInfo) {
text = courseInfo[course.id];
if (courseInfo) {
text = courseInfo;
} else {
return null;
}
Expand Down Expand Up @@ -272,9 +286,10 @@ export class FenixService {

const courses: Course[] = [];
const coursesNames: string[] = coursesJson.map(c => c.name);
const cachedLinksHtml: {} = {};
for (let course of coursesJson) {
try {
course = await this.parseCourseBasicInfo(academicTerm, course, htmlCurriculum, coursesNames);
course = await this.parseCourseBasicInfo(academicTerm, course, htmlCurriculum, coursesNames, cachedLinksHtml);

// Remove optional courses (example acronym: O32)
if (course.acronym[0] === 'O' && course.acronym[1] >= '0' && course.acronym[1] <= '9') continue;
Expand Down

0 comments on commit 6cc69ef

Please sign in to comment.