Browse Source

fix: 修正长课程正则匹配

dezige131 3 weeks ago
parent
commit
85db115b8e
1 changed files with 20 additions and 4 deletions
  1. 20 4
      resources/HNUST/HNUST_01.js

+ 20 - 4
resources/HNUST/HNUST_01.js

@@ -21,6 +21,9 @@ function parseWeeks(weekStr) {
 /**
  * 提取课程数据
  */
+/**
+ * 提取课程数据(已优化多节连堂课合并与去重逻辑)
+ */
 function extractCoursesFromDoc(doc) {
     let parsedCourses = [];
     const table = doc.getElementById('timetable');
@@ -69,11 +72,12 @@ function extractCoursesFromDoc(doc) {
                     let timeFont = tempDiv.querySelector('font[title="周次(节次)"]');
                     if (timeFont) {
                         let timeText = timeFont.innerText.trim();
-                        let timeMatch = timeText.match(/(.+?)\(周\)\[(\d+)-(\d+)节\]/);
+                        let timeMatch = timeText.match(/(.+?)\(周\)\[([\d-]+)节\]/);
                         if (timeMatch) {
                             courseObj.weeks = parseWeeks(timeMatch[1]);
-                            courseObj.startSection = parseInt(timeMatch[2]);
-                            courseObj.endSection = parseInt(timeMatch[3]);
+                            let sections = timeMatch[2].split('-');
+                            courseObj.startSection = parseInt(sections[0]);
+                            courseObj.endSection = parseInt(sections[sections.length - 1]);
                         } else {
                             let weekOnlyMatch = timeText.match(/(.+?)\(周\)/);
                             if (weekOnlyMatch) {
@@ -85,7 +89,19 @@ function extractCoursesFromDoc(doc) {
                     } else return; 
 
                     if (courseObj.name && courseObj.weeks && courseObj.weeks.length > 0) {
-                        parsedCourses.push(courseObj);
+                        let isDuplicate = parsedCourses.some(c => 
+                            c.day === courseObj.day &&
+                            c.name === courseObj.name &&
+                            c.startSection === courseObj.startSection &&
+                            c.endSection === courseObj.endSection &&
+                            c.teacher === courseObj.teacher &&
+                            c.position === courseObj.position &&
+                            JSON.stringify(c.weeks) === JSON.stringify(courseObj.weeks)
+                        );
+                        
+                        if (!isDuplicate) {
+                            parsedCourses.push(courseObj);
+                        }
                     }
                 });
             });