utils

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package utils provides shared helpers for scraping, parsing, and uploading workflows.

Index

Constants

View Source
const Lverbose = 1 << 7

Lverbose enables verbose logging on Logger instances and global loggers.

View Source
const R_COURSE_CODE string = `[0-9vV]{4}`

R_COURSE_CODE matches a four-character course number like 2252 or V001. The first digit of a course code is the course level, the second digit is the number of credit hours.

View Source
const R_DATE_MDY string = `[A-z]+\s+[0-9]+,\s+[0-9]{4}`

R_DATE_MDY matches dates formatted like January 5, 2022.

View Source
const R_GRADE string = `[ABCFabcf][+-]?`

R_GRADE matches letter grades with optional modifiers, such as C-.

View Source
const R_SECTION_CODE string = `[0-9A-z]+`

R_SECTION_CODE matches section identifiers such as 101 or A1.

View Source
const R_SUBJECT string = `[A-Z]{2,4}`

R_SUBJECT matches a subject prefix such as HIST.

View Source
const R_SUBJ_COURSE string = `[A-Z]{2,4}\s*[0-9vV]{4}`

R_SUBJ_COURSE matches subject and course combinations without capturing groups.

View Source
const R_SUBJ_COURSE_CAP string = `([A-Z]{2,4})\s*([0-9vV]{4})`

R_SUBJ_COURSE_CAP captures both subject and course number components.

View Source
const R_TERM_CODE string = `[0-9]{2}[sufSUF]`

R_TERM_CODE matches term codes like 22S or 23f.

View Source
const R_TIME_AM_PM string = `[0-9]+:[0-9]+\s*(?:am|pm)`

R_TIME_AM_PM matches 12-hour times such as 5:22pm.

View Source
const R_WEEKDAY string = `(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day`

R_WEEKDAY matches full weekday names like Monday or Thursday.

View Source
const R_YEARS string = `(?:freshm[ae]n|sophomores?|juniors?|seniors?)`

R_YEARS matches class standing descriptors like freshmen or seniors.

Variables

View Source
var Headless = true

Headless toggles whether chromedp runs without a visible browser window.

Functions

func ConvertFromInterface

func ConvertFromInterface[T string | float64](value any) *T

ConvertFromInterface attempts to convert a value into the requested type and returns a pointer when successful.

func GetAllFilesWithExtension

func GetAllFilesWithExtension(inDir string, extension string) []string

GetAllFilesWithExtension recursively gathers file paths within inDir that match extension.

func GetCoursePrefixes

func GetCoursePrefixes(chromedpCtx context.Context) []string

GetCoursePrefixes retrieves all course prefix values from CourseBook.

func GetEnv

func GetEnv(name string) (string, error)

GetEnv finds an environment variable and returns an error when it is unset.

func GetMapKeys

func GetMapKeys[M ~map[K]V, K comparable, V any](m M) []K

GetMapKeys returns a slice of all map keys.

func GetMapValues

func GetMapValues[M ~map[K]V, K comparable, V any](m M) []V

GetMapValues returns a slice of all map values.

func InitChromeDp

func InitChromeDp() (chromedpCtx context.Context, cancelFnc context.CancelFunc)

InitChromeDp configures and returns a chromedp context with optional headless settings.

func RefreshAstraToken

func RefreshAstraToken(chromedpCtx context.Context) map[string][]string

RefreshAstraToken signs into Astra and returns headers containing authentication cookies.

func RefreshToken

func RefreshToken(chromedpCtx context.Context) map[string][]string

RefreshToken logs into CourseBook and returns headers containing a fresh session token.

func Regexpf

func Regexpf(format string, vars ...interface{}) *regexp.Regexp

Regexpf formats and compiles a regular expression pattern using fmt.Sprintf semantics.

func Retry

func Retry(action func() error, maxRetries int, retryCallback func(numRetries int)) error

Retry calls action until it succeeds or exceeds maxRetries, invoking retryCallback between attempts.

func TrimWhitespace

func TrimWhitespace(text string) string

TrimWhitespace removes spaces, tabs, newlines, and carriage returns from the provided string.

func VPrint

func VPrint(text string)

VPrint prints text through the package-level logger when the verbose flag is set.

func VPrintf

func VPrintf(format string, vars ...any)

VPrintf prints through the package-level logger when the verbose flag is set.

func VPrintln

func VPrintln(text string)

VPrintln prints text with a newline through the package-level logger when the verbose flag is set.

func WriteJSON

func WriteJSON(filepath string, data interface{}) error

WriteJSON encodes data as indented JSON and writes it to filepath.

Types

type Logger

type Logger struct {
	log.Logger
}

Logger extends log.Logger with helper methods that respect the verbose flag.

func NewLogger

func NewLogger(out io.Writer, prefix string, flag int) *Logger

NewLogger constructs a Logger that writes to out with the given prefix and flags.

func (*Logger) VPrint

func (logger *Logger) VPrint(text string)

VPrint prints text when the verbose flag is set.

func (*Logger) VPrintf

func (logger *Logger) VPrintf(format string, vars ...any)

VPrintf prints using fmt.Printf semantics when the verbose flag is set.

func (*Logger) VPrintln

func (logger *Logger) VPrintln(text string)

VPrintln prints text with a newline when the verbose flag is set.

type SplitWriter

type SplitWriter struct {
	// contains filtered or unexported fields
}

SplitWriter routes writes to multiple underlying writers.

func NewSplitWriter

func NewSplitWriter(writers ...io.Writer) *SplitWriter

NewSplitWriter constructs a SplitWriter that fans out writes to the provided writers.

func (*SplitWriter) Write

func (splitWriter *SplitWriter) Write(p []byte) (n int, err error)

Write copies the provided bytes to each underlying writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL