Skip to content

Commit 91d1a00

Browse files
committed
Make it more aligned with practices of other scrapers
1 parent b7a2eb7 commit 91d1a00

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

utils/methods.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/chromedp/cdproto/cdp"
2222
"github.com/chromedp/cdproto/network"
23+
"github.com/chromedp/cdproto/runtime"
2324
"github.com/chromedp/chromedp"
2425
)
2526

@@ -100,6 +101,9 @@ func RefreshToken(chromedpCtx context.Context) map[string][]string {
100101
chromedp.Navigate(`https://coursebook.utdallas.edu/`),
101102
chromedp.ActionFunc(func(ctx context.Context) error {
102103
cookies, err := network.GetCookies().Do(ctx)
104+
if err != nil {
105+
return err
106+
}
103107
cookieStrs = make([]string, len(cookies))
104108
gotToken := false
105109
for i, cookie := range cookies {
@@ -112,7 +116,7 @@ func RefreshToken(chromedpCtx context.Context) map[string][]string {
112116
if !gotToken {
113117
return errors.New("failed to get a new token")
114118
}
115-
return err
119+
return nil
116120
}),
117121
)
118122
if r != nil && r.Status != 200 {
@@ -287,42 +291,35 @@ func Retry(action func() error, maxRetries int, retryCallback func(numRetries in
287291

288292
// Get all the available course prefixes
289293
func GetCoursePrefixes(chromedpCtx context.Context) []string {
290-
// Refresh the token
294+
// Might need to refresh the token every time we get new course prefixes in the future
291295
// refreshToken(chromedpCtx)
292296

293297
log.Printf("Finding course prefix nodes...")
294298

295299
var coursePrefixes []string
296-
var coursePrefixNodes []*cdp.Node
300+
log.Println("Finding course prefixes...")
297301

298302
// Get option elements for course prefix dropdown
299-
err := chromedp.Run(chromedpCtx,
303+
_, err := chromedp.RunResponse(chromedpCtx,
300304
chromedp.Navigate("https://coursebook.utdallas.edu"),
301-
chromedp.Nodes("select#combobox_cp option", &coursePrefixNodes, chromedp.ByQueryAll),
305+
chromedp.QueryAfter("select#combobox_cp option",
306+
func(ctx context.Context, _ runtime.ExecutionContextID, nodes ...*cdp.Node) error {
307+
for _, node := range nodes[1:] {
308+
coursePrefixes = append(coursePrefixes, node.AttributeValue("value"))
309+
}
310+
return nil
311+
},
312+
),
302313
)
303-
304314
if err != nil {
305315
log.Panic(err)
306316
}
307-
308-
log.Println("Found the course prefix nodes!")
309-
310-
log.Println("Finding course prefixes...")
311-
312-
// Remove the first option due to it being empty
313-
coursePrefixNodes = coursePrefixNodes[1:]
314-
315-
// Get the value of each option and append to coursePrefixes
316-
for _, node := range coursePrefixNodes {
317-
coursePrefixes = append(coursePrefixes, node.AttributeValue("value"))
318-
}
319-
320-
log.Println("Found the course prefixes!")
321-
317+
log.Printf("Found the %d course prefixes!\n", len(coursePrefixes))
322318
return coursePrefixes
323319
}
324320

325-
func ConvertFromInterface[T string | float64](value interface{}) *T {
321+
// Convert the value of any type to either string or float64
322+
func ConvertFromInterface[T string | float64](value any) *T {
326323
if parsed, ok := value.(T); ok {
327324
return &parsed
328325
}

0 commit comments

Comments
 (0)