Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.26.0

require (
github.com/PuerkitoBio/goquery v1.12.0
github.com/UTDNebula/nebula-api v1.2.1-0.20261005155620-603b6b10f3ec
github.com/UTDNebula/nebula-api v1.2.1-0.20261114203121-26bbd574a0cf
github.com/chromedp/cdproto v0.0.0-20260321001828-e3e3800016bc
github.com/chromedp/chromedp v0.15.1
github.com/dongri/phonenumber v0.1.12
Expand Down Expand Up @@ -37,7 +37,7 @@ require (
github.com/envoyproxy/go-control-plane/envoy v1.39.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.3.3 // indirect
github.com/felixge/httpsnoop v1.1.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/go-jose/go-jose/v4 v4.1.6 // indirect
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapp
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.60.0/go.mod h1:SRAbhyZ4R4FagHMM9VtRgSY/lheRoht2fKelZXQUenk=
github.com/PuerkitoBio/goquery v1.12.0 h1:pAcL4g3WRXekcB9AU/y1mbKez2dbY2AajVhtkO8RIBo=
github.com/PuerkitoBio/goquery v1.12.0/go.mod h1:802ej+gV2y7bbIhOIoPY5sT183ZW0YFofScC4q/hIpQ=
github.com/UTDNebula/nebula-api v1.2.1-0.20261005155620-603b6b10f3ec h1:8tR4hi5XnNjmz4M6tWveIP0P4qiP8CxIRxyfFMdggAc=
github.com/UTDNebula/nebula-api v1.2.1-0.20261005155620-603b6b10f3ec/go.mod h1:OoCuc8ktZCTZwO17bIz8bXwt06KIW8NYvzVvZ/8TbWc=
github.com/UTDNebula/nebula-api v1.2.1-0.20261114203121-26bbd574a0cf h1:q54Ub5hA4rU0ooWuVBOOFvWeiElJsnBFGELQT8UMh6E=
github.com/UTDNebula/nebula-api v1.2.1-0.20261114203121-26bbd574a0cf/go.mod h1:ckjoQ1cy323RvxYfGpmLFUYwb3pXSOvT7V0sCzPnFp4=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand Down Expand Up @@ -62,8 +62,8 @@ github.com/getsentry/sentry-go v0.48.0 h1:FRZNr7Uk1C86ev1bSJmYlUkL9oyivQA6YOcdYf
github.com/getsentry/sentry-go v0.48.0/go.mod h1:E5UkA5wp1qR2+MDydNYlVeUiNN2xEdjYMidkgf0Qoss=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-jose/go-jose/v4 v4.1.6 h1:sNRkftxFJxwsILiQukqp5z83QBSmntVwmgRY7AqBDO4=
github.com/go-jose/go-jose/v4 v4.1.6/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVwYFP/L05h5TKQxvkXoKxNvTpjxYKdF1Nlwuao=
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
38 changes: 37 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"

"github.com/UTDNebula/api-tools/utils"
Expand Down Expand Up @@ -128,11 +130,45 @@ func parse(path string) {
panic(err)
}

parseSection(getRowInfo(doc), getClassInfo(doc))
parseSection(getRowInfo(doc), getClassInfo(doc), getScrapedAt(path))

utils.VPrint("Parsed!")
}

// getScrapedAt finds the timestamp file in the HTML file's directory
// or its parent directories.
func getScrapedAt(path string) *time.Time {
dir, err := filepath.Abs(filepath.Dir(path))
if err != nil {
panic(err)
}

// Find scraped_at timestamp for this semester by searching parents
for {
timestampPath := filepath.Join(dir, "scraped_at.txt")
data, err := os.ReadFile(timestampPath)
if err == nil {
scrapedAt, err := time.Parse(time.RFC3339, strings.TrimSpace(string(data)))
if err != nil {
panic(fmt.Errorf("invalid scrape timestamp %s: %w", timestampPath, err))
}
scrapedAt = scrapedAt.UTC()
return &scrapedAt
}

if !os.IsNotExist(err) {
panic(err)
}


parent := filepath.Dir(dir)
if parent == dir {
return nil
}
dir = parent
}
}

func getRowInfo(doc *goquery.Document) map[string]*goquery.Selection {
infoRows := doc.FindMatcher(goquery.Single("table.courseinfo__overviewtable > tbody")).ChildrenFiltered("tr")
rowInfo := make(map[string]*goquery.Selection, len(infoRows.Nodes))
Expand Down
11 changes: 11 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func updateTestData() error {
}
defer os.RemoveAll(tempDir)

// Keep shared input files when regenerating testdata.
for _, name := range []string{"scraped_at.txt", "enrollment.json"} {
if data, err := os.ReadFile(filepath.Join("testdata", name)); err == nil {
if err := os.WriteFile(filepath.Join(tempDir, name), data, 0644); err != nil {
return err
}
} else if !os.IsNotExist(err) {
return err
}
}

GradeMap, err = loadGrades("../static-data/grades")
if err != nil {
return err
Expand Down
66 changes: 65 additions & 1 deletion parser/sectionParser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package parser

import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -33,12 +35,15 @@ var (

// meetingTimesRegexp matches a time in 12-hour AM/PM format (e.g., "5:00 pm", "11:30am")
meetingTimesRegexp = regexp.MustCompile(utils.R_TIME_AM_PM)

// enrollmentComponentRegexp matches enrollment total (may list counts for cross-listed sections (e.g. Enrolled Total: 14 (acn5312.002.26f) + 0 (hcs5312.002.26f))
enrollmentComponentRegexp = regexp.MustCompile(`^([0-9]+)(?:\s+\(([^()]+)\))?$`)
)

// parseSection creates a schema.Section from rowInfo and ClassInfo,
// adds it to Sections, and updates the associated Course and Professors.
// Internally calls parseCourse and parseProfessors, which modify global maps.
func parseSection(rowInfo map[string]*goquery.Selection, classInfo map[string]string) {
func parseSection(rowInfo map[string]*goquery.Selection, classInfo map[string]string, scrapedAt *time.Time) {
classNum, courseNum := getInternalClassAndCourseNum(classInfo)
session := getAcademicSession(rowInfo)
courseRef := parseCourse(courseNum, session, rowInfo, classInfo)
Expand All @@ -60,6 +65,7 @@ func parseSection(rowInfo map[string]*goquery.Selection, classInfo map[string]st
Core_flags: getCoreFlags(rowInfo),
Syllabus_uri: getSyllabusUri(rowInfo),
Grade_distribution: getGradeDistribution(session, sectionNumber, courseRef),
Latest_enrollment: getEnrollment(rowInfo, scrapedAt),
}

// Add new section to section map
Expand Down Expand Up @@ -165,6 +171,64 @@ func getInstructionMode(classInfo map[string]string) string {
return ""
}

// getEnrollment reads the labeled values in CourseBook's Status row.
// A missing row or count stays absent rather than becoming a reported zero.
func getEnrollment(rowInfo map[string]*goquery.Selection, scrapedAt *time.Time) *schema.SectionEnrollment {
row, ok := rowInfo["Status:"]
if !ok {
return nil
}

values := make(map[string]string)
var label string
row.Contents().Each(func(_ int, s *goquery.Selection) {
if goquery.NodeName(s) == "b" {
label = strings.Join(strings.Fields(s.Text()), " ")
} else {
values[label] += s.Text()
}
})

parseCount := func(label string, allowNegative bool) *int {
value := strings.TrimSpace(values[label])
if value == "" {
return nil
}
count, err := strconv.Atoi(value)
if err != nil || (!allowNegative && count < 0) {
panic(fmt.Sprintf("invalid %s %q", label, value))
}
return &count
}

enrollment := &schema.SectionEnrollment{
Updated_at: scrapedAt,
Status: strings.TrimSpace(values["Enrollment Status:"]),
Available_seats: parseCount("Available Seats:", true),
Waitlist: parseCount("Waitlist:", false),
}
if value := strings.TrimSpace(values["Enrolled Total:"]); value != "" {
total := 0
components := strings.Split(value, "+")
for _, component := range components {
match := enrollmentComponentRegexp.FindStringSubmatch(strings.TrimSpace(component))
if match == nil || (len(components) > 1 && match[2] == "") {
panic(fmt.Sprintf("invalid Enrolled Total: %q", value))
}
count, err := strconv.Atoi(match[1])
if err != nil {
panic(err)
}
total += count
}
enrollment.Enrolled_total = &total
}
if enrollment.Status == "" && enrollment.Available_seats == nil && enrollment.Enrolled_total == nil && enrollment.Waitlist == nil {
return nil
}
return enrollment
}

// getMeetings parses meeting schedule information from the row information map.
//
// The function does not guarantee any number of meetings nor any fields of
Expand Down
50 changes: 50 additions & 0 deletions parser/sectionParser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,62 @@ package parser

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/UTDNebula/nebula-api/rest/schema"
"github.com/google/go-cmp/cmp"
)

func TestGetEnrollment(t *testing.T) {
fixtures, err := unmarshallFile[[]struct{ Name, HTML string }]("testdata/enrollment.json")
if err != nil {
t.Fatal(err)
}
scrapedAt := time.Date(2026, 8, 24, 12, 34, 56, 0, time.UTC)
expected := map[string]*schema.SectionEnrollment{
"closed": {Updated_at: &scrapedAt, Status: "CLOSED/FULL", Available_seats: new(0), Enrolled_total: new(68), Waitlist: new(0)},
"cross_listed": {Updated_at: &scrapedAt, Status: "OPEN", Available_seats: new(10), Enrolled_total: new(14), Waitlist: new(0)},
"over_enrolled": {Updated_at: &scrapedAt, Status: "CLOSED/FULL", Available_seats: new(-15), Enrolled_total: new(57), Waitlist: new(0)},
}
for _, fixture := range fixtures {
t.Run(fixture.Name, func(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(fixture.HTML))
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected[fixture.Name], getEnrollment(getRowInfo(doc), &scrapedAt)); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
if got := getEnrollment(nil, &scrapedAt); got != nil {
t.Errorf("missing enrollment row = %v, want nil", got)
}
}

func TestGetScrapedAt(t *testing.T) {
root := t.TempDir()
path := filepath.Join(root, "26f", "cp_cs", "section.html")
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if got := getScrapedAt(path); got != nil {
t.Fatalf("missing timestamp file = %v, want nil", got)
}
if err := os.WriteFile(filepath.Join(root, "26f", "scraped_at.txt"), []byte("2026-08-24T12:34:56Z\n"), 0644); err != nil {
t.Fatal(err)
}
want := time.Date(2026, 8, 24, 12, 34, 56, 0, time.UTC)
if got := getScrapedAt(path); got == nil || !got.Equal(want) || got.Location() != time.UTC {
t.Errorf("scraped_at = %v, want UTC %v", got, want)
}
}

// TestGetInternalClassAndCourseNum checks parsing of internal course identifiers.
func TestGetInternalClassAndCourseNum(t *testing.T) {
t.Parallel()
Expand Down
9 changes: 8 additions & 1 deletion parser/testdata/case_000/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,12 @@
0,
0
],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 3,
"enrolled_total": 64,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_001/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,12 @@
0,
0
],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 3,
"enrolled_total": 64,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_002/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@
0,
0
],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "CLOSED/FULL",
"available_seats": 0,
"enrolled_total": 56,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_003/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@
"core_flags": [],
"syllabus_uri": "https://dox.utdallas.edu/syl153921",
"grade_distribution": [],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 9,
"enrolled_total": 1,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_004/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@
"core_flags": [],
"syllabus_uri": "",
"grade_distribution": [],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 16,
"enrolled_total": 3,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_005/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@
"core_flags": [],
"syllabus_uri": "",
"grade_distribution": [],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 17,
"enrolled_total": 2,
"waitlist": 0
}
}
9 changes: 8 additions & 1 deletion parser/testdata/case_006/section.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,12 @@
0,
0
],
"attributes": null
"attributes": null,
"latest_enrollment": {
"updated_at": "2025-01-25T12:30:01.123Z",
"status": "OPEN",
"available_seats": 15,
"enrolled_total": 33,
"waitlist": 0
}
}
17 changes: 17 additions & 0 deletions parser/testdata/enrollment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"name": "closed",
"source": "data/26f/cp_acct/acct2301.014.26f.html",
"html": "<table class=\"courseinfo__overviewtable\"><tbody><tr class=\"courseinfo__overviewtable__tr\">\n <th class=\"courseinfo__overviewtable__th text-right\">\n Status:\n </th>\n <td class=\"courseinfo__overviewtable__td\">\n <b>Enrollment Status:</b> CLOSED/FULL &nbsp;&nbsp; <b>Available Seats:</b> 0 &nbsp;&nbsp; <b>Enrolled Total:</b> 68 &nbsp;&nbsp; <b>Waitlist:</b> 0\n\n </td>\n </tr></tbody></table>"
},
{
"name": "cross_listed",
"source": "data/26f/cp_bims/bims4v00.001.26f.html",
"html": "<table class=\"courseinfo__overviewtable\"><tbody><tr class=\"courseinfo__overviewtable__tr\">\n <th class=\"courseinfo__overviewtable__th text-right\">\n Status:\n </th>\n <td class=\"courseinfo__overviewtable__td\">\n <b>Enrollment Status:</b> OPEN &nbsp;&nbsp; <b>Available Seats:</b> 10 &nbsp;&nbsp; <b>Enrolled Total:</b> 1 (bims4v00.001.26f) + 8 (phys4v11.001.26f) + 5 (phys5v48.001.26f) &nbsp;&nbsp; <b>Waitlist:</b> 0\n\n </td>\n </tr></tbody></table>"
},
{
"name": "over_enrolled",
"source": "data/26f/cp_buan/buan4382.501.26f.html",
"html": "<table class=\"courseinfo__overviewtable\"><tbody><tr class=\"courseinfo__overviewtable__tr\">\n <th class=\"courseinfo__overviewtable__th text-right\">\n Status:\n </th>\n <td class=\"courseinfo__overviewtable__td\">\n <b>Enrollment Status:</b> CLOSED/FULL &nbsp;&nbsp; <b>Available Seats:</b> -15 &nbsp;&nbsp; <b>Enrolled Total:</b> 23 (buan4382.501.26f) + 34 (itss4382.501.26f) &nbsp;&nbsp; <b>Waitlist:</b> 0\n\n </td>\n </tr></tbody></table>"
}
]
1 change: 1 addition & 0 deletions parser/testdata/scraped_at.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2025-01-25T12:30:01.123Z
Loading
Loading