mirror of
https://github.com/Xevion/banner.git
synced 2025-12-17 14:11:18 -06:00
refactor: complete refactor into cmd/ & internal/ submodules
This commit is contained in:
640
internal/api/api.go
Normal file
640
internal/api/api.go
Normal file
@@ -0,0 +1,640 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"banner/internal/models"
|
||||
"banner/internal/utils"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var (
|
||||
latestSession string = ""
|
||||
sessionTime time.Time
|
||||
expiryTime time.Duration = 25 * time.Minute
|
||||
)
|
||||
|
||||
// ResetSessionTimer resets the session timer to the current time.
|
||||
// This is only used by the DoRequest handler when Banner API calls are detected, which would reset the session timer.
|
||||
func ResetSessionTimer() {
|
||||
// Only reset the session time if the session is still valid
|
||||
if time.Since(sessionTime) <= expiryTime {
|
||||
sessionTime = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateSession generates a new session ID (nonce) for use with the Banner API.
|
||||
// Don't use this function directly, use GetSession instead.
|
||||
func GenerateSession() string {
|
||||
return utils.RandomString(5) + utils.Nonce()
|
||||
}
|
||||
|
||||
// DoRequest performs & logs the request, logging and returning the response
|
||||
func DoRequest(req *http.Request) (*http.Response, error) {
|
||||
headerSize := 0
|
||||
for key, values := range req.Header {
|
||||
for _, value := range values {
|
||||
headerSize += len(key)
|
||||
headerSize += len(value)
|
||||
}
|
||||
}
|
||||
|
||||
bodySize := int64(0)
|
||||
if req.Body != nil {
|
||||
bodySize, _ = io.Copy(io.Discard, req.Body)
|
||||
}
|
||||
|
||||
size := zerolog.Dict().Int64("body", bodySize).Int("header", headerSize).Int("url", len(req.URL.String()))
|
||||
|
||||
log.Debug().
|
||||
Dict("size", size).
|
||||
Str("method", strings.TrimRight(req.Method, " ")).
|
||||
Str("url", req.URL.String()).
|
||||
Str("query", req.URL.RawQuery).
|
||||
Str("content-type", req.Header.Get("Content-Type")).
|
||||
Msg("Request")
|
||||
|
||||
res, err := config.Client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
log.Err(err).Stack().Str("method", req.Method).Msg("Request Failed")
|
||||
} else {
|
||||
contentLengthHeader := res.Header.Get("Content-Length")
|
||||
contentLength := int64(-1)
|
||||
|
||||
// If this request was a Banner API request, reset the session timer
|
||||
if strings.HasPrefix(req.URL.Path, "StudentRegistrationSsb/ssb/classSearch/") {
|
||||
ResetSessionTimer()
|
||||
}
|
||||
|
||||
// Get the content length
|
||||
if contentLengthHeader != "" {
|
||||
contentLength, err = strconv.ParseInt(contentLengthHeader, 10, 64)
|
||||
if err != nil {
|
||||
contentLength = -1
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Int("status", res.StatusCode).Int64("content-length", contentLength).Strs("content-type", res.Header["Content-Type"]).Msg("Response")
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
var terms []BannerTerm
|
||||
var lastTermUpdate time.Time
|
||||
|
||||
// TryReloadTerms attempts to reload the terms if they are not loaded or the last update was more than 24 hours ago
|
||||
func TryReloadTerms() error {
|
||||
if len(terms) > 0 && time.Since(lastTermUpdate) < 24*time.Hour {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load the terms
|
||||
var err error
|
||||
terms, err = GetTerms("", 1, 100)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load terms: %w", err)
|
||||
}
|
||||
|
||||
lastTermUpdate = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsTermArchived checks if the given term is archived
|
||||
// TODO: Add error, switch missing term logic to error
|
||||
func IsTermArchived(term string) bool {
|
||||
// Ensure the terms are loaded
|
||||
err := TryReloadTerms()
|
||||
if err != nil {
|
||||
log.Err(err).Stack().Msg("Failed to reload terms")
|
||||
return true
|
||||
}
|
||||
|
||||
// Check if the term is in the list of terms
|
||||
bannerTerm, exists := lo.Find(terms, func(t BannerTerm) bool {
|
||||
return t.Code == term
|
||||
})
|
||||
|
||||
if !exists {
|
||||
log.Warn().Str("term", term).Msg("Term does not exist")
|
||||
return true
|
||||
}
|
||||
|
||||
return bannerTerm.Archived()
|
||||
}
|
||||
|
||||
// GetSession retrieves the current session ID if it's still valid.
|
||||
// If the session ID is invalid or has expired, a new one is generated and returned.
|
||||
// SessionIDs are valid for 30 minutes, but we'll be conservative and regenerate every 25 minutes.
|
||||
func GetSession() string {
|
||||
// Check if a reset is required
|
||||
if latestSession == "" || time.Since(sessionTime) >= expiryTime {
|
||||
// Generate a new session identifier
|
||||
latestSession = GenerateSession()
|
||||
|
||||
// Select the current term
|
||||
term := utils.Default(time.Now()).ToString()
|
||||
log.Info().Str("term", term).Str("sessionID", latestSession).Msg("Setting selected term")
|
||||
err := SelectTerm(term, latestSession)
|
||||
if err != nil {
|
||||
log.Fatal().Stack().Err(err).Msg("Failed to select term while generating session ID")
|
||||
}
|
||||
|
||||
sessionTime = time.Now()
|
||||
}
|
||||
|
||||
return latestSession
|
||||
}
|
||||
|
||||
type Pair struct {
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type BannerTerm Pair
|
||||
type Instructor Pair
|
||||
|
||||
// Archived returns true if the term is in it's archival state (view only)
|
||||
func (term BannerTerm) Archived() bool {
|
||||
return strings.Contains(term.Description, "View Only")
|
||||
}
|
||||
|
||||
// GetTerms retrieves and parses the term information for a given search term.
|
||||
// Page number must be at least 1.
|
||||
func GetTerms(search string, page int, max int) ([]BannerTerm, error) {
|
||||
// Ensure offset is valid
|
||||
if page <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/getTerms", map[string]string{
|
||||
"searchTerm": search,
|
||||
// Page vs Offset is not a mistake here, the API uses "offset" as the page number
|
||||
"offset": strconv.Itoa(page),
|
||||
"max": strconv.Itoa(max),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
if page <= 0 {
|
||||
return nil, errors.New("Offset must be greater than 0")
|
||||
}
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get terms: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if contentType := res.Header.Get("Content-Type"); !strings.Contains(contentType, models.JsonContentType) {
|
||||
return nil, &utils.UnexpectedContentTypeError{
|
||||
Expected: models.JsonContentType,
|
||||
Actual: contentType,
|
||||
}
|
||||
}
|
||||
|
||||
// print the response body
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
terms := make([]BannerTerm, 0, 10)
|
||||
json.Unmarshal(body, &terms)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse terms: %w", err)
|
||||
}
|
||||
|
||||
return terms, nil
|
||||
}
|
||||
|
||||
// SelectTerm selects the given term in the Banner system.
|
||||
// This function completes the initial term selection process, which is required before any other API calls can be made with the session ID.
|
||||
func SelectTerm(term string, sessionId string) error {
|
||||
form := url.Values{
|
||||
"term": {term},
|
||||
"studyPath": {""},
|
||||
"studyPathText": {""},
|
||||
"startDatepicker": {""},
|
||||
"endDatepicker": {""},
|
||||
"uniqueSessionId": {sessionId},
|
||||
}
|
||||
|
||||
params := map[string]string{
|
||||
"mode": "search",
|
||||
}
|
||||
|
||||
req := utils.BuildRequestWithBody("POST", "/term/search", params, bytes.NewBufferString(form.Encode()))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to select term: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
return fmt.Errorf("response was not JSON: %w", res.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
// Acquire fwdUrl
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var redirectResponse struct {
|
||||
FwdUrl string `json:"fwdUrl"`
|
||||
}
|
||||
json.Unmarshal(body, &redirectResponse)
|
||||
|
||||
// Make a GET request to the fwdUrl
|
||||
req = utils.BuildRequest("GET", redirectResponse.FwdUrl, nil)
|
||||
res, err = DoRequest(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to follow redirect: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is OK (200)
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("redirect response was not 200: %w", res.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPartOfTerms retrieves and parses the part of term information for a given term.
|
||||
// Ensure that the offset is greater than 0.
|
||||
func GetPartOfTerms(search string, term int, offset int, max int) ([]BannerTerm, error) {
|
||||
// Ensure offset is valid
|
||||
if offset <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/get_partOfTerm", map[string]string{
|
||||
"searchTerm": search,
|
||||
"term": strconv.Itoa(term),
|
||||
"offset": strconv.Itoa(offset),
|
||||
"max": strconv.Itoa(max),
|
||||
"uniqueSessionId": GetSession(),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get part of terms: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Panic().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
terms := make([]BannerTerm, 0, 10)
|
||||
err = json.Unmarshal(body, &terms)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse part of terms: %w", err)
|
||||
}
|
||||
|
||||
return terms, nil
|
||||
}
|
||||
|
||||
// GetInstructors retrieves and parses the instructor information for a given search term.
|
||||
// In my opinion, it is unclear what providing the term does, as the results should be the same regardless of the term.
|
||||
// This function is included for completeness, but probably isn't useful.
|
||||
// Ensure that the offset is greater than 0.
|
||||
func GetInstructors(search string, term string, offset int, max int) ([]Instructor, error) {
|
||||
// Ensure offset is valid
|
||||
if offset <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/get_instructor", map[string]string{
|
||||
"searchTerm": search,
|
||||
"term": term,
|
||||
"offset": strconv.Itoa(offset),
|
||||
"max": strconv.Itoa(max),
|
||||
"uniqueSessionId": GetSession(),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get instructors: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
instructors := make([]Instructor, 0, 10)
|
||||
err = json.Unmarshal(body, &instructors)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse instructors: %w", err)
|
||||
}
|
||||
|
||||
return instructors, nil
|
||||
}
|
||||
|
||||
// TODO: Finish this struct & function
|
||||
// ClassDetails represents
|
||||
type ClassDetails struct {
|
||||
}
|
||||
|
||||
func GetCourseDetails(term int, crn int) *ClassDetails {
|
||||
body, err := json.Marshal(map[string]string{
|
||||
"term": strconv.Itoa(term),
|
||||
"courseReferenceNumber": strconv.Itoa(crn),
|
||||
"first": "first", // TODO: What is this?
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Stack().Err(err).Msg("Failed to marshal body")
|
||||
}
|
||||
req := utils.BuildRequestWithBody("GET", "/searchResults/getClassDetails", nil, bytes.NewBuffer(body))
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
return &ClassDetails{}
|
||||
}
|
||||
|
||||
// Search invokes a search on the Banner system with the given query and returns the results.
|
||||
func Search(query *Query, sort string, sortDescending bool) (*models.SearchResult, error) {
|
||||
ResetDataForm()
|
||||
|
||||
params := query.Paramify()
|
||||
|
||||
params["txt_term"] = "202510" // TODO: Make this automatic but dynamically specifiable
|
||||
params["uniqueSessionId"] = GetSession()
|
||||
params["sortColumn"] = sort
|
||||
params["sortDirection"] = "asc"
|
||||
|
||||
// These dates are not available for usage anywhere in the UI, but are included in every query
|
||||
params["startDatepicker"] = ""
|
||||
params["endDatepicker"] = ""
|
||||
|
||||
req := utils.BuildRequest("GET", "/searchResults/searchResults", params)
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("search failed with status code: %d", res.StatusCode)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
// for server 500 errors, parse for the error with '#dialog-message > div.message'
|
||||
log.Error().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var result models.SearchResult
|
||||
err = json.Unmarshal(body, &result)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse search results: %w", err)
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetSubjects retrieves and parses the subject information for a given search term.
|
||||
// The results of this response shouldn't change much, but technically could as new majors are developed, or old ones are removed.
|
||||
// Ensure that the offset is greater than 0.
|
||||
func GetSubjects(search string, term string, offset int, max int) ([]Pair, error) {
|
||||
// Ensure offset is valid
|
||||
if offset <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/get_subject", map[string]string{
|
||||
"searchTerm": search,
|
||||
"term": term,
|
||||
"offset": strconv.Itoa(offset),
|
||||
"max": strconv.Itoa(max),
|
||||
"uniqueSessionId": GetSession(),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get subjects: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
subjects := make([]Pair, 0, 10)
|
||||
err = json.Unmarshal(body, &subjects)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse subjects: %w", err)
|
||||
}
|
||||
|
||||
return subjects, nil
|
||||
}
|
||||
|
||||
// GetCampuses retrieves and parses the campus information for a given search term.
|
||||
// In my opinion, it is unclear what providing the term does, as the results should be the same regardless of the term.
|
||||
// This function is included for completeness, but probably isn't useful.
|
||||
// Ensure that the offset is greater than 0.
|
||||
func GetCampuses(search string, term int, offset int, max int) ([]Pair, error) {
|
||||
// Ensure offset is valid
|
||||
if offset <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/get_campus", map[string]string{
|
||||
"searchTerm": search,
|
||||
"term": strconv.Itoa(term),
|
||||
"offset": strconv.Itoa(offset),
|
||||
"max": strconv.Itoa(max),
|
||||
"uniqueSessionId": GetSession(),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get campuses: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
campuses := make([]Pair, 0, 10)
|
||||
err = json.Unmarshal(body, &campuses)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse campuses: %w", err)
|
||||
}
|
||||
|
||||
return campuses, nil
|
||||
}
|
||||
|
||||
// GetInstructionalMethods retrieves and parses the instructional method information for a given search term.
|
||||
// In my opinion, it is unclear what providing the term does, as the results should be the same regardless of the term.
|
||||
// This function is included for completeness, but probably isn't useful.
|
||||
// Ensure that the offset is greater than 0.
|
||||
func GetInstructionalMethods(search string, term string, offset int, max int) ([]Pair, error) {
|
||||
// Ensure offset is valid
|
||||
if offset <= 0 {
|
||||
return nil, errors.New("offset must be greater than 0")
|
||||
}
|
||||
|
||||
req := utils.BuildRequest("GET", "/classSearch/get_instructionalMethod", map[string]string{
|
||||
"searchTerm": search,
|
||||
"term": term,
|
||||
"offset": strconv.Itoa(offset),
|
||||
"max": strconv.Itoa(max),
|
||||
"uniqueSessionId": GetSession(),
|
||||
"_": utils.Nonce(),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get instructional methods: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
|
||||
methods := make([]Pair, 0, 10)
|
||||
err = json.Unmarshal(body, &methods)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse instructional methods: %w", err)
|
||||
}
|
||||
|
||||
return methods, nil
|
||||
}
|
||||
|
||||
// GetCourseMeetingTime retrieves the meeting time information for a course based on the given term and course reference number (CRN).
|
||||
// It makes an HTTP GET request to the appropriate API endpoint and parses the response to extract the meeting time data.
|
||||
// The function returns a MeetingTimeResponse struct containing the extracted information.
|
||||
func GetCourseMeetingTime(term int, crn int) ([]models.MeetingTimeResponse, error) {
|
||||
req := utils.BuildRequest("GET", "/searchResults/getFacultyMeetingTimes", map[string]string{
|
||||
"term": strconv.Itoa(term),
|
||||
"courseReferenceNumber": strconv.Itoa(crn),
|
||||
})
|
||||
|
||||
res, err := DoRequest(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get meeting time: %w", err)
|
||||
}
|
||||
|
||||
// Assert that the response is JSON
|
||||
if !utils.ContentTypeMatch(res, "application/json") {
|
||||
log.Fatal().Stack().Str("content-type", res.Header.Get("Content-Type")).Msg("Response was not JSON")
|
||||
}
|
||||
|
||||
// Read the response body into JSON
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
// Parse the JSON into a MeetingTimeResponse struct
|
||||
var meetingTime struct {
|
||||
Inner []models.MeetingTimeResponse `json:"fmt"`
|
||||
}
|
||||
err = json.Unmarshal(body, &meetingTime)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse meeting time: %w", err)
|
||||
}
|
||||
|
||||
return meetingTime.Inner, nil
|
||||
}
|
||||
|
||||
// ResetDataForm makes a POST request that needs to be made upon before new search requests can be made.
|
||||
func ResetDataForm() {
|
||||
req := utils.BuildRequest("POST", "/classSearch/resetDataForm", nil)
|
||||
_, err := DoRequest(req)
|
||||
if err != nil {
|
||||
log.Fatal().Stack().Err(err).Msg("Failed to reset data form")
|
||||
}
|
||||
}
|
||||
|
||||
// GetCourse retrieves the course information.
|
||||
// This course does not retrieve directly from the API, but rather uses scraped data stored in Redis.
|
||||
func GetCourse(crn string) (*models.Course, error) {
|
||||
// Retrieve raw data
|
||||
result, err := config.KV.Get(config.Ctx, fmt.Sprintf("class:%s", crn)).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return nil, fmt.Errorf("course not found: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get course: %w", err)
|
||||
}
|
||||
|
||||
// Unmarshal the raw data
|
||||
var course models.Course
|
||||
err = json.Unmarshal([]byte(result), &course)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal course: %w", err)
|
||||
}
|
||||
|
||||
return &course, nil
|
||||
}
|
||||
222
internal/api/scrape.go
Normal file
222
internal/api/scrape.go
Normal file
@@ -0,0 +1,222 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"banner/internal/models"
|
||||
"banner/internal/utils"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxPageSize = 500
|
||||
)
|
||||
|
||||
var (
|
||||
// PriorityMajors is a list of majors that are considered to be high priority for scraping. This list is used to determine which majors to scrape first/most often.
|
||||
PriorityMajors = []string{"CS", "CPE", "MAT", "EE", "IS"}
|
||||
// AncillaryMajors is a list of majors that are considered to be low priority for scraping. This list will not contain any majors that are in PriorityMajors.
|
||||
AncillaryMajors []string
|
||||
// AllMajors is a list of all majors that are available in the Banner system.
|
||||
AllMajors []string
|
||||
)
|
||||
|
||||
// Scrape is the general scraping invocation (best called within/as a goroutine) that should be called regularly to initiate scraping of the Banner system.
|
||||
func Scrape() error {
|
||||
// Populate AllMajors if it is empty
|
||||
if len(AncillaryMajors) == 0 {
|
||||
term := utils.Default(time.Now()).ToString()
|
||||
subjects, err := GetSubjects("", term, 1, 99)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get subjects: %w", err)
|
||||
}
|
||||
|
||||
// Ensure subjects were found
|
||||
if len(subjects) == 0 {
|
||||
return fmt.Errorf("no subjects found")
|
||||
}
|
||||
|
||||
// Extract major code name
|
||||
for _, subject := range subjects {
|
||||
// Add to AncillaryMajors if not in PriorityMajors
|
||||
if !lo.Contains(PriorityMajors, subject.Code) {
|
||||
AncillaryMajors = append(AncillaryMajors, subject.Code)
|
||||
}
|
||||
}
|
||||
|
||||
AllMajors = lo.Flatten([][]string{PriorityMajors, AncillaryMajors})
|
||||
}
|
||||
|
||||
expiredSubjects, err := GetExpiredSubjects()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get scrapable majors: %w", err)
|
||||
}
|
||||
|
||||
log.Info().Strs("majors", expiredSubjects).Msg("Scraping majors")
|
||||
for _, subject := range expiredSubjects {
|
||||
err := ScrapeMajor(subject)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to scrape major %s: %w", subject, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetExpiredSubjects returns a list of subjects that are expired and should be scraped.
|
||||
func GetExpiredSubjects() ([]string, error) {
|
||||
term := utils.Default(time.Now()).ToString()
|
||||
subjects := make([]string, 0)
|
||||
|
||||
// Get all subjects
|
||||
values, err := config.KV.MGet(config.Ctx, lo.Map(AllMajors, func(major string, _ int) string {
|
||||
return fmt.Sprintf("scraped:%s:%s", major, term)
|
||||
})...).Result()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get all subjects: %w", err)
|
||||
}
|
||||
|
||||
// Extract expired subjects
|
||||
for i, value := range values {
|
||||
subject := AllMajors[i]
|
||||
|
||||
// If the value is nil or "0", then the subject is expired
|
||||
if value == nil || value == "0" {
|
||||
subjects = append(subjects, subject)
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Strs("majors", subjects).Msg("Expired Subjects")
|
||||
|
||||
return subjects, nil
|
||||
}
|
||||
|
||||
// ScrapeMajor is the scraping invocation for a specific major.
|
||||
// This function does not check whether scraping is required at this time, it is assumed that the caller has already done so.
|
||||
func ScrapeMajor(subject string) error {
|
||||
offset := 0
|
||||
totalClassCount := 0
|
||||
|
||||
for {
|
||||
// Build & execute the query
|
||||
query := NewQuery().Offset(offset).MaxResults(MaxPageSize * 2).Subject(subject)
|
||||
result, err := Search(query, "subjectDescription", false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("search failed: %w (%s)", err, query.String())
|
||||
}
|
||||
|
||||
// Isn't it bullshit that they decided not to leave an actual 'reason' field for the failure?
|
||||
if !result.Success {
|
||||
return fmt.Errorf("result marked unsuccessful when searching for classes (%s)", query.String())
|
||||
}
|
||||
|
||||
classCount := len(result.Data)
|
||||
totalClassCount += classCount
|
||||
log.Debug().Str("subject", subject).Int("count", classCount).Int("offset", offset).Msg("Placing classes in Redis")
|
||||
|
||||
// Process each class and store it in Redis
|
||||
for _, course := range result.Data {
|
||||
// Store class in Redis
|
||||
err := IntakeCourse(course)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to store class in Redis")
|
||||
}
|
||||
}
|
||||
|
||||
// Increment and continue if the results are full
|
||||
if classCount >= MaxPageSize {
|
||||
// This is unlikely to happen, but log it just in case
|
||||
if classCount > MaxPageSize {
|
||||
log.Warn().Int("page", offset).Int("count", classCount).Msg("Results exceed MaxPageSize")
|
||||
}
|
||||
|
||||
offset += MaxPageSize
|
||||
|
||||
// TODO: Replace sleep with smarter rate limiting
|
||||
log.Debug().Str("subject", subject).Int("nextOffset", offset).Msg("Sleeping before next page")
|
||||
time.Sleep(time.Second * 3)
|
||||
continue
|
||||
} else {
|
||||
// Log the number of classes scraped
|
||||
log.Info().Str("subject", subject).Int("total", totalClassCount).Msgf("Subject %s Scraped", subject)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
term := utils.Default(time.Now()).ToString()
|
||||
|
||||
// Calculate the expiry time for the scrape (1 hour for every 200 classes, random +-15%) with a minimum of 1 hour
|
||||
var scrapeExpiry time.Duration
|
||||
if totalClassCount == 0 {
|
||||
scrapeExpiry = time.Hour * 12
|
||||
} else {
|
||||
scrapeExpiry = CalculateExpiry(term, totalClassCount, lo.Contains(PriorityMajors, subject))
|
||||
}
|
||||
|
||||
// Mark the major as scraped
|
||||
if totalClassCount == 0 {
|
||||
totalClassCount = -1
|
||||
}
|
||||
err := config.KV.Set(config.Ctx, fmt.Sprintf("scraped:%s:%s", subject, term), totalClassCount, scrapeExpiry).Err()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to mark major as scraped")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CalculateExpiry calculates the expiry time until the next scrape for a major.
|
||||
// term is the term for which the relevant course is occurring within.
|
||||
// count is the number of courses that were scraped.
|
||||
// priority is a boolean indicating whether the major is a priority major.
|
||||
func CalculateExpiry(term string, count int, priority bool) time.Duration {
|
||||
// An hour for every 100 classes
|
||||
baseExpiry := time.Hour * time.Duration(count/100)
|
||||
|
||||
// Subjects with less than 50 classes have a reversed expiry (less classes, longer interval)
|
||||
// 1 class => 12 hours, 49 classes => 1 hour
|
||||
if count < 50 {
|
||||
hours := utils.Slope(utils.Point{1, 12}, utils.Point{49, 1}, float64(count)).Y
|
||||
baseExpiry = time.Duration(hours * float64(time.Hour))
|
||||
}
|
||||
|
||||
// If the subject is a priority, then the expiry is halved without variance
|
||||
if priority {
|
||||
return baseExpiry / 3
|
||||
}
|
||||
|
||||
// If the term is considered "view only" or "archived", then the expiry is multiplied by 5
|
||||
var expiry = baseExpiry
|
||||
if IsTermArchived(term) {
|
||||
expiry *= 5
|
||||
}
|
||||
|
||||
// Add minor variance to the expiry
|
||||
expiryVariance := baseExpiry.Seconds() * (rand.Float64() * 0.15) // Between 0 and 15% of the total
|
||||
if rand.Intn(2) == 0 {
|
||||
expiry -= time.Duration(expiryVariance) * time.Second
|
||||
} else {
|
||||
expiry += time.Duration(expiryVariance) * time.Second
|
||||
}
|
||||
|
||||
// Ensure the expiry is at least 1 hour with up to 15 extra minutes
|
||||
if expiry < time.Hour {
|
||||
baseExpiry = time.Hour + time.Duration(rand.Intn(60*15))*time.Second
|
||||
}
|
||||
|
||||
return baseExpiry
|
||||
}
|
||||
|
||||
// IntakeCourse stores a course in Redis.
|
||||
// This function is mostly a stub for now, but will be used to handle change identification, notifications, and SQLite upserts in the future.
|
||||
func IntakeCourse(course models.Course) error {
|
||||
err := config.KV.Set(config.Ctx, fmt.Sprintf("class:%s", course.CourseReferenceNumber), course, 0).Err()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to store class in Redis: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
336
internal/api/search.go
Normal file
336
internal/api/search.go
Normal file
@@ -0,0 +1,336 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const (
|
||||
paramSubject = "txt_subject"
|
||||
paramTitle = "txt_courseTitle"
|
||||
paramKeywords = "txt_keywordlike"
|
||||
paramOpenOnly = "chk_open_only"
|
||||
paramTermPart = "txt_partOfTerm"
|
||||
paramCampus = "txt_campus"
|
||||
paramAttributes = "txt_attribute"
|
||||
paramInstructor = "txt_instructor"
|
||||
paramStartTimeHour = "select_start_hour"
|
||||
paramStartTimeMinute = "select_start_min"
|
||||
paramStartTimeMeridiem = "select_start_ampm"
|
||||
paramEndTimeHour = "select_end_hour"
|
||||
paramEndTimeMinute = "select_end_min"
|
||||
paramEndTimeMeridiem = "select_end_ampm"
|
||||
paramMinCredits = "txt_credithourlow"
|
||||
paramMaxCredits = "txt_credithourhigh"
|
||||
paramCourseNumberLow = "txt_course_number_range"
|
||||
paramCourseNumberHigh = "txt_course_number_range_to"
|
||||
paramOffset = "pageOffset"
|
||||
paramMaxResults = "pageMaxSize"
|
||||
)
|
||||
|
||||
type Query struct {
|
||||
subject *string
|
||||
title *string
|
||||
keywords *[]string
|
||||
openOnly *bool
|
||||
termPart *[]string // e.g. [1, B6, 8, J]
|
||||
campus *[]string // e.g. [9, 1DT, 1LR]
|
||||
instructionalMethod *[]string // e.g. [HB]
|
||||
attributes *[]string // e.g. [060, 010]
|
||||
instructor *[]uint64 // e.g. [27957, 27961]
|
||||
startTime *time.Duration
|
||||
endTime *time.Duration
|
||||
minCredits *int
|
||||
maxCredits *int
|
||||
offset int
|
||||
maxResults int
|
||||
courseNumberRange *Range
|
||||
}
|
||||
|
||||
func NewQuery() *Query {
|
||||
return &Query{maxResults: 8, offset: 0}
|
||||
}
|
||||
|
||||
// Subject sets the subject for the query
|
||||
func (q *Query) Subject(subject string) *Query {
|
||||
q.subject = &subject
|
||||
return q
|
||||
}
|
||||
|
||||
// Title sets the title for the query
|
||||
func (q *Query) Title(title string) *Query {
|
||||
q.title = &title
|
||||
return q
|
||||
}
|
||||
|
||||
// Keywords sets the keywords for the query
|
||||
func (q *Query) Keywords(keywords []string) *Query {
|
||||
q.keywords = &keywords
|
||||
return q
|
||||
}
|
||||
|
||||
// Keyword adds a keyword to the query
|
||||
func (q *Query) Keyword(keyword string) *Query {
|
||||
if q.keywords == nil {
|
||||
q.keywords = &[]string{keyword}
|
||||
} else {
|
||||
*q.keywords = append(*q.keywords, keyword)
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
// OpenOnly sets the open only flag for the query
|
||||
func (q *Query) OpenOnly(openOnly bool) *Query {
|
||||
q.openOnly = &openOnly
|
||||
return q
|
||||
}
|
||||
|
||||
// TermPart sets the term part for the query
|
||||
func (q *Query) TermPart(termPart []string) *Query {
|
||||
q.termPart = &termPart
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) Campus(campus []string) *Query {
|
||||
q.campus = &campus
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) InstructionalMethod(instructionalMethod []string) *Query {
|
||||
q.instructionalMethod = &instructionalMethod
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) Attributes(attributes []string) *Query {
|
||||
q.attributes = &attributes
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) Instructor(instructor []uint64) *Query {
|
||||
q.instructor = &instructor
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) StartTime(startTime time.Duration) *Query {
|
||||
q.startTime = &startTime
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) EndTime(endTime time.Duration) *Query {
|
||||
q.endTime = &endTime
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) Credits(low int, high int) *Query {
|
||||
q.minCredits = &low
|
||||
q.maxCredits = &high
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) MinCredits(value int) *Query {
|
||||
q.minCredits = &value
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) MaxCredits(value int) *Query {
|
||||
q.maxCredits = &value
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Query) CourseNumbers(low int, high int) *Query {
|
||||
q.courseNumberRange = &Range{low, high}
|
||||
return q
|
||||
}
|
||||
|
||||
// Offset sets the offset for the query, allowing for pagination
|
||||
func (q *Query) Offset(offset int) *Query {
|
||||
q.offset = offset
|
||||
return q
|
||||
}
|
||||
|
||||
// MaxResults sets the maximum number of results for the query
|
||||
func (q *Query) MaxResults(maxResults int) *Query {
|
||||
q.maxResults = maxResults
|
||||
return q
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
Low int
|
||||
High int
|
||||
}
|
||||
|
||||
// FormatTimeParameter formats a time.Duration into a tuple of strings
|
||||
// This is mostly a private helper to keep the parameter formatting for both the start and end time consistent together
|
||||
func FormatTimeParameter(d time.Duration) (string, string, string) {
|
||||
hourParameter, minuteParameter, meridiemParameter := "", "", ""
|
||||
|
||||
hours := int64(d.Hours())
|
||||
minutes := int64(d.Minutes()) % 60
|
||||
|
||||
minuteParameter = strconv.FormatInt(minutes, 10)
|
||||
|
||||
if hours >= 12 {
|
||||
hourParameter = "PM"
|
||||
|
||||
// Exceptional case: 12PM = 12, 1PM = 1, 2PM = 2
|
||||
if hours >= 13 {
|
||||
hourParameter = strconv.FormatInt(hours-12, 10) // 13 - 12 = 1, 14 - 12 = 2
|
||||
} else {
|
||||
hourParameter = strconv.FormatInt(hours, 10)
|
||||
}
|
||||
} else {
|
||||
meridiemParameter = "AM"
|
||||
hourParameter = strconv.FormatInt(hours, 10)
|
||||
}
|
||||
|
||||
return hourParameter, minuteParameter, meridiemParameter
|
||||
}
|
||||
|
||||
// Paramify converts a Query into a map of parameters that can be used in a POST request
|
||||
// This function assumes each query key only appears once.
|
||||
func (q *Query) Paramify() map[string]string {
|
||||
params := map[string]string{}
|
||||
|
||||
if q.subject != nil {
|
||||
params[paramSubject] = *q.subject
|
||||
}
|
||||
|
||||
if q.title != nil {
|
||||
// Whitespace can prevent valid queries from succeeding
|
||||
params[paramTitle] = strings.TrimSpace(*q.title)
|
||||
}
|
||||
|
||||
if q.keywords != nil {
|
||||
params[paramKeywords] = strings.Join(*q.keywords, " ")
|
||||
}
|
||||
|
||||
if q.openOnly != nil {
|
||||
params[paramOpenOnly] = "true"
|
||||
}
|
||||
|
||||
if q.termPart != nil {
|
||||
params[paramTermPart] = strings.Join(*q.termPart, ",")
|
||||
}
|
||||
|
||||
if q.campus != nil {
|
||||
params[paramCampus] = strings.Join(*q.campus, ",")
|
||||
}
|
||||
|
||||
if q.attributes != nil {
|
||||
params[paramAttributes] = strings.Join(*q.attributes, ",")
|
||||
}
|
||||
|
||||
if q.instructor != nil {
|
||||
params[paramInstructor] = strings.Join(lo.Map(*q.instructor, func(i uint64, _ int) string {
|
||||
return strconv.FormatUint(i, 10)
|
||||
}), ",")
|
||||
}
|
||||
|
||||
if q.startTime != nil {
|
||||
hour, minute, meridiem := FormatTimeParameter(*q.startTime)
|
||||
params[paramStartTimeHour] = hour
|
||||
params[paramStartTimeMinute] = minute
|
||||
params[paramStartTimeMeridiem] = meridiem
|
||||
}
|
||||
|
||||
if q.endTime != nil {
|
||||
hour, minute, meridiem := FormatTimeParameter(*q.endTime)
|
||||
params[paramEndTimeHour] = hour
|
||||
params[paramEndTimeMinute] = minute
|
||||
params[paramEndTimeMeridiem] = meridiem
|
||||
}
|
||||
|
||||
if q.minCredits != nil {
|
||||
params[paramMinCredits] = strconv.Itoa(*q.minCredits)
|
||||
}
|
||||
|
||||
if q.maxCredits != nil {
|
||||
params[paramMaxCredits] = strconv.Itoa(*q.maxCredits)
|
||||
}
|
||||
|
||||
if q.courseNumberRange != nil {
|
||||
params[paramCourseNumberLow] = strconv.Itoa(q.courseNumberRange.Low)
|
||||
params[paramCourseNumberHigh] = strconv.Itoa(q.courseNumberRange.High)
|
||||
}
|
||||
|
||||
params[paramOffset] = strconv.Itoa(q.offset)
|
||||
params[paramMaxResults] = strconv.Itoa(q.maxResults)
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
// String returns a string representation of the query, ideal for debugging & logging.
|
||||
func (q *Query) String() string {
|
||||
var sb strings.Builder
|
||||
|
||||
if q.subject != nil {
|
||||
fmt.Fprintf(&sb, "subject=%s, ", *q.subject)
|
||||
}
|
||||
|
||||
if q.title != nil {
|
||||
// Whitespace can prevent valid queries from succeeding
|
||||
fmt.Fprintf(&sb, "title=%s, ", strings.TrimSpace(*q.title))
|
||||
}
|
||||
|
||||
if q.keywords != nil {
|
||||
fmt.Fprintf(&sb, "keywords=%s, ", strings.Join(*q.keywords, " "))
|
||||
}
|
||||
|
||||
if q.openOnly != nil {
|
||||
fmt.Fprintf(&sb, "openOnly=%t, ", *q.openOnly)
|
||||
}
|
||||
|
||||
if q.termPart != nil {
|
||||
fmt.Fprintf(&sb, "termPart=%s, ", strings.Join(*q.termPart, ","))
|
||||
}
|
||||
|
||||
if q.campus != nil {
|
||||
fmt.Fprintf(&sb, "campus=%s, ", strings.Join(*q.campus, ","))
|
||||
}
|
||||
|
||||
if q.attributes != nil {
|
||||
fmt.Fprintf(&sb, "attributes=%s, ", strings.Join(*q.attributes, ","))
|
||||
}
|
||||
|
||||
if q.instructor != nil {
|
||||
fmt.Fprintf(&sb, "instructor=%s, ", strings.Join(lo.Map(*q.instructor, func(i uint64, _ int) string {
|
||||
return strconv.FormatUint(i, 10)
|
||||
}), ","))
|
||||
}
|
||||
|
||||
if q.startTime != nil {
|
||||
hour, minute, meridiem := FormatTimeParameter(*q.startTime)
|
||||
fmt.Fprintf(&sb, "startTime=%s:%s%s, ", hour, minute, meridiem)
|
||||
}
|
||||
|
||||
if q.endTime != nil {
|
||||
hour, minute, meridiem := FormatTimeParameter(*q.endTime)
|
||||
fmt.Fprintf(&sb, "endTime=%s:%s%s, ", hour, minute, meridiem)
|
||||
}
|
||||
|
||||
if q.minCredits != nil {
|
||||
fmt.Fprintf(&sb, "minCredits=%d, ", *q.minCredits)
|
||||
}
|
||||
|
||||
if q.maxCredits != nil {
|
||||
fmt.Fprintf(&sb, "maxCredits=%d, ", *q.maxCredits)
|
||||
}
|
||||
|
||||
if q.courseNumberRange != nil {
|
||||
fmt.Fprintf(&sb, "courseNumberRange=%d-%d, ", q.courseNumberRange.Low, q.courseNumberRange.High)
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sb, "offset=%d, ", q.offset)
|
||||
fmt.Fprintf(&sb, "maxResults=%d", q.maxResults)
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// Dict returns a map representation of the query, ideal for debugging & logging.
|
||||
// This dict is represented with zerolog's Event type.
|
||||
// func (q *Query) Dict() *zerolog.Event {
|
||||
// }
|
||||
54
internal/api/session.go
Normal file
54
internal/api/session.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"banner/internal/utils"
|
||||
"net/url"
|
||||
|
||||
log "github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func Setup() {
|
||||
// Makes the initial requests that sets up the session cookies for the rest of the application
|
||||
log.Info().Msg("Setting up session...")
|
||||
|
||||
request_queue := []string{
|
||||
"/registration/registration",
|
||||
"/selfServiceMenu/data",
|
||||
}
|
||||
|
||||
for _, path := range request_queue {
|
||||
req := utils.BuildRequest("GET", path, nil)
|
||||
DoRequest(req)
|
||||
}
|
||||
|
||||
// Validate that cookies were set
|
||||
baseUrlParsed, err := url.Parse(config.BaseURL)
|
||||
if err != nil {
|
||||
log.Fatal().Stack().Str("baseURL", config.BaseURL).Err(err).Msg("Failed to parse baseURL")
|
||||
}
|
||||
|
||||
current_cookies := config.Client.Jar.Cookies(baseUrlParsed)
|
||||
required_cookies := map[string]bool{
|
||||
"JSESSIONID": false,
|
||||
"SSB_COOKIE": false,
|
||||
}
|
||||
|
||||
for _, cookie := range current_cookies {
|
||||
_, present := required_cookies[cookie.Name]
|
||||
// Check if this cookie is required
|
||||
if present {
|
||||
required_cookies[cookie.Name] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Check if all required cookies were set
|
||||
for cookieName, cookie_set := range required_cookies {
|
||||
if !cookie_set {
|
||||
log.Warn().Str("cookieName", cookieName).Msg("Required cookie not set")
|
||||
}
|
||||
}
|
||||
log.Debug().Msg("All required cookies set, session setup complete")
|
||||
|
||||
// TODO: Validate that the session allows access to termSelection
|
||||
}
|
||||
510
internal/bot/commands.go
Normal file
510
internal/bot/commands.go
Normal file
@@ -0,0 +1,510 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"banner/internal/api"
|
||||
"banner/internal/config"
|
||||
"banner/internal/models"
|
||||
"banner/internal/utils"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var (
|
||||
CommandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition, SearchCommandDefinition, IcsCommandDefinition}
|
||||
CommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) error{
|
||||
TimeCommandDefinition.Name: TimeCommandHandler,
|
||||
TermCommandDefinition.Name: TermCommandHandler,
|
||||
SearchCommandDefinition.Name: SearchCommandHandler,
|
||||
IcsCommandDefinition.Name: IcsCommandHandler,
|
||||
}
|
||||
)
|
||||
|
||||
var SearchCommandDefinition = &discordgo.ApplicationCommand{
|
||||
Name: "search",
|
||||
Description: "Search for a course",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
MinLength: utils.GetIntPointer(0),
|
||||
MaxLength: 48,
|
||||
Name: "title",
|
||||
Description: "Course Title (exact, use autocomplete)",
|
||||
Required: false,
|
||||
Autocomplete: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "code",
|
||||
MinLength: utils.GetIntPointer(4),
|
||||
Description: "Course Code (e.g. 3743, 3000-3999, 3xxx, 3000-)",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "max",
|
||||
Description: "Maximum number of results",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "keywords",
|
||||
Description: "Keywords in Title or Description (space separated)",
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "instructor",
|
||||
Description: "Instructor Name",
|
||||
Required: false,
|
||||
Autocomplete: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "subject",
|
||||
Description: "Subject (e.g. Computer Science/CS, Mathematics/MAT)",
|
||||
Required: false,
|
||||
Autocomplete: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) error {
|
||||
data := interaction.ApplicationCommandData()
|
||||
query := api.NewQuery().Credits(3, 6)
|
||||
|
||||
for _, option := range data.Options {
|
||||
switch option.Name {
|
||||
case "title":
|
||||
query.Title(option.StringValue())
|
||||
case "code":
|
||||
var (
|
||||
low = -1
|
||||
high = -1
|
||||
)
|
||||
var err error
|
||||
valueRaw := strings.TrimSpace(option.StringValue())
|
||||
|
||||
// Partially/fully specified range
|
||||
if strings.Contains(valueRaw, "-") {
|
||||
match := regexp.MustCompile(`(\d{1,4})-(\d{1,4})?`).FindSubmatch([]byte(valueRaw))
|
||||
|
||||
if match == nil {
|
||||
return fmt.Errorf("invalid range format: %s", valueRaw)
|
||||
}
|
||||
|
||||
// If not 2 or 3 matches, it's invalid
|
||||
if len(match) != 3 && len(match) != 4 {
|
||||
return fmt.Errorf("invalid range format: %s", match[0])
|
||||
}
|
||||
|
||||
low, err = strconv.Atoi(string(match[1]))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing course code (low)")
|
||||
}
|
||||
|
||||
// If there's not a high value, set it to max (open ended)
|
||||
if len(match) == 2 || len(match[2]) == 0 {
|
||||
high = 9999
|
||||
} else {
|
||||
high, err = strconv.Atoi(string(match[2]))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing course code (high)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #xxx, ##xx, ###x format (34xx -> 3400-3499)
|
||||
if strings.Contains(valueRaw, "x") {
|
||||
if len(valueRaw) != 4 {
|
||||
return fmt.Errorf("code range format invalid: must be 1 or more digits followed by x's (%s)", valueRaw)
|
||||
}
|
||||
|
||||
match := regexp.MustCompile(`\d{1,}([xX]{1,3})`).Match([]byte(valueRaw))
|
||||
if !match {
|
||||
return fmt.Errorf("code range format invalid: must be 1 or more digits followed by x's (%s)", valueRaw)
|
||||
}
|
||||
|
||||
// Replace x's with 0's
|
||||
low, err = strconv.Atoi(strings.Replace(valueRaw, "x", "0", -1))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing implied course code (low)")
|
||||
}
|
||||
|
||||
// Replace x's with 9's
|
||||
high, err = strconv.Atoi(strings.Replace(valueRaw, "x", "9", -1))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing implied course code (high)")
|
||||
}
|
||||
} else if len(valueRaw) == 4 {
|
||||
// 4 digit code
|
||||
low, err = strconv.Atoi(valueRaw)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing course code")
|
||||
}
|
||||
|
||||
high = low
|
||||
}
|
||||
|
||||
if low == -1 || high == -1 {
|
||||
return fmt.Errorf("course code range invalid (%s)", valueRaw)
|
||||
}
|
||||
|
||||
if low > high {
|
||||
return fmt.Errorf("course code range is invalid: low is greater than high (%d > %d)", low, high)
|
||||
}
|
||||
|
||||
if low < 1000 || high < 1000 || low > 9999 || high > 9999 {
|
||||
return fmt.Errorf("course code range is invalid: must be 1000-9999 (%d-%d)", low, high)
|
||||
}
|
||||
|
||||
query.CourseNumbers(low, high)
|
||||
case "keywords":
|
||||
query.Keywords(
|
||||
strings.Split(option.StringValue(), " "),
|
||||
)
|
||||
case "max":
|
||||
query.MaxResults(
|
||||
min(8, int(option.IntValue())),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
courses, err := api.Search(query, "", false)
|
||||
if err != nil {
|
||||
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error searching for courses",
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
fetch_time := time.Now()
|
||||
fields := []*discordgo.MessageEmbedField{}
|
||||
|
||||
for _, course := range courses.Data {
|
||||
displayName := course.Faculty[0].DisplayName
|
||||
categoryLink := fmt.Sprintf("[%s](https://catalog.utsa.edu/undergraduate/coursedescriptions/%s/)", course.Subject, strings.ToLower(course.Subject))
|
||||
classLink := fmt.Sprintf("[%s-%s](https://catalog.utsa.edu/search/?P=%s%%20%s)", course.CourseNumber, course.SequenceNumber, course.Subject, course.CourseNumber)
|
||||
professorLink := fmt.Sprintf("[%s](https://www.ratemyprofessors.com/search/professors/1516?q=%s)", displayName, url.QueryEscape(displayName))
|
||||
|
||||
identifierText := fmt.Sprintf("%s %s (CRN %s)\n%s", categoryLink, classLink, course.CourseReferenceNumber, professorLink)
|
||||
meetings := course.MeetingsFaculty[0]
|
||||
|
||||
fields = append(fields, &discordgo.MessageEmbedField{
|
||||
Name: "Identifier",
|
||||
Value: identifierText,
|
||||
Inline: true,
|
||||
}, &discordgo.MessageEmbedField{
|
||||
Name: "Name",
|
||||
Value: course.CourseTitle,
|
||||
Inline: true,
|
||||
}, &discordgo.MessageEmbedField{
|
||||
Name: "Meeting Time",
|
||||
Value: meetings.String(),
|
||||
Inline: true,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Blue if there are results, orange if there are none
|
||||
color := 0x0073FF
|
||||
if courses.TotalCount == 0 {
|
||||
color = 0xFF6500
|
||||
}
|
||||
|
||||
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Footer: utils.GetFetchedFooter(fetch_time),
|
||||
Description: fmt.Sprintf("%d Class%s", courses.TotalCount, utils.Plural(courses.TotalCount)),
|
||||
Fields: fields[:min(25, len(fields))],
|
||||
Color: color,
|
||||
},
|
||||
},
|
||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var TermCommandDefinition = &discordgo.ApplicationCommand{
|
||||
Name: "terms",
|
||||
Description: "Guess the current term, or search for a specific term",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
MinLength: utils.GetIntPointer(0),
|
||||
MaxLength: 8,
|
||||
Name: "search",
|
||||
Description: "Term to search for",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "page",
|
||||
Description: "Page Number",
|
||||
Required: false,
|
||||
MinValue: utils.GetFloatPointer(1),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TermCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) error {
|
||||
data := interaction.ApplicationCommandData()
|
||||
|
||||
searchTerm := ""
|
||||
pageNumber := 1
|
||||
|
||||
for _, option := range data.Options {
|
||||
switch option.Name {
|
||||
case "search":
|
||||
searchTerm = option.StringValue()
|
||||
case "page":
|
||||
pageNumber = int(option.IntValue())
|
||||
default:
|
||||
log.Warn().Str("option", option.Name).Msg("Unexpected option in term command")
|
||||
}
|
||||
}
|
||||
|
||||
termResult, err := api.GetTerms(searchTerm, pageNumber, 25)
|
||||
|
||||
if err != nil {
|
||||
utils.RespondError(session, interaction.Interaction, "Error while fetching terms", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fields := []*discordgo.MessageEmbedField{}
|
||||
|
||||
for _, t := range termResult {
|
||||
fields = append(fields, &discordgo.MessageEmbedField{
|
||||
Name: t.Description,
|
||||
Value: t.Code,
|
||||
Inline: true,
|
||||
})
|
||||
}
|
||||
|
||||
fetch_time := time.Now()
|
||||
|
||||
if len(fields) > 25 {
|
||||
log.Warn().Int("count", len(fields)).Msg("Too many fields in term command (trimmed)")
|
||||
}
|
||||
|
||||
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Footer: utils.GetFetchedFooter(fetch_time),
|
||||
Description: fmt.Sprintf("%d term%s (page %d)", len(termResult), utils.Plural(len(termResult)), pageNumber),
|
||||
Fields: fields[:min(25, len(fields))],
|
||||
},
|
||||
},
|
||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var TimeCommandDefinition = &discordgo.ApplicationCommand{
|
||||
Name: "time",
|
||||
Description: "Get Class Meeting Time",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "crn",
|
||||
Description: "Course Reference Number",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error {
|
||||
fetch_time := time.Now()
|
||||
crn := i.ApplicationCommandData().Options[0].IntValue()
|
||||
|
||||
// Fix static term
|
||||
meetingTimes, err := api.GetCourseMeetingTime(202510, int(crn))
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Error getting meeting time",
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
meetingTime := meetingTimes[0]
|
||||
duration := meetingTime.EndTime().Sub(meetingTime.StartTime())
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Footer: utils.GetFetchedFooter(fetch_time),
|
||||
Description: "",
|
||||
Fields: []*discordgo.MessageEmbedField{
|
||||
{
|
||||
Name: "Start Date",
|
||||
Value: meetingTime.StartDay().Format("Monday, January 2, 2006"),
|
||||
},
|
||||
{
|
||||
Name: "End Date",
|
||||
Value: meetingTime.EndDay().Format("Monday, January 2, 2006"),
|
||||
},
|
||||
{
|
||||
Name: "Start/End Time",
|
||||
Value: fmt.Sprintf("%s - %s (%d min)", meetingTime.StartTime().String(), meetingTime.EndTime().String(), int64(duration.Minutes())),
|
||||
},
|
||||
{
|
||||
Name: "Days of Week",
|
||||
Value: utils.WeekdaysToString(meetingTime.Days()),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
var IcsCommandDefinition = &discordgo.ApplicationCommand{
|
||||
Name: "ics",
|
||||
Description: "Generate an ICS file for a course",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionInteger,
|
||||
Name: "crn",
|
||||
Description: "Course Reference Number",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func IcsCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error {
|
||||
crn := i.ApplicationCommandData().Options[0].IntValue()
|
||||
|
||||
course, err := api.GetCourse(strconv.Itoa(int(crn)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error retrieving course data: %w", err)
|
||||
}
|
||||
|
||||
// Fix static term
|
||||
meetingTimes, err := api.GetCourseMeetingTime(202510, int(crn))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error requesting meeting time: %w", err)
|
||||
}
|
||||
|
||||
if len(meetingTimes) == 0 {
|
||||
return fmt.Errorf("unexpected - no meeting time data found for course")
|
||||
}
|
||||
|
||||
// Check if the course has any meeting times
|
||||
_, exists := lo.Find(meetingTimes, func(mt models.MeetingTimeResponse) bool {
|
||||
switch mt.MeetingTime.MeetingType {
|
||||
case "ID", "OA":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
if !exists {
|
||||
log.Warn().Str("crn", course.CourseReferenceNumber).Msg("Non-meeting course requested for ICS file")
|
||||
utils.RespondError(s, i.Interaction, "The course requested does not meet at a defined moment in time.", nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
events := []string{}
|
||||
for _, meeting := range meetingTimes {
|
||||
now := time.Now().In(config.CentralTimeLocation)
|
||||
uid := fmt.Sprintf("%d-%s@ical.banner.xevion.dev", now.Unix(), meeting.CourseReferenceNumber)
|
||||
|
||||
startDay := meeting.StartDay()
|
||||
startTime := meeting.StartTime()
|
||||
endTime := meeting.EndTime()
|
||||
dtStart := time.Date(startDay.Year(), startDay.Month(), startDay.Day(), int(startTime.Hours), int(startTime.Minutes), 0, 0, config.CentralTimeLocation)
|
||||
dtEnd := time.Date(startDay.Year(), startDay.Month(), startDay.Day(), int(endTime.Hours), int(endTime.Minutes), 0, 0, config.CentralTimeLocation)
|
||||
|
||||
endDay := meeting.EndDay()
|
||||
until := time.Date(endDay.Year(), endDay.Month(), endDay.Day(), 23, 59, 59, 0, config.CentralTimeLocation)
|
||||
|
||||
summary := fmt.Sprintf("%s %s %s", course.Subject, course.CourseNumber, course.CourseTitle)
|
||||
description := fmt.Sprintf("Instructor: %s\nSection: %s\nCRN: %s", course.Faculty[0].DisplayName, course.SequenceNumber, meeting.CourseReferenceNumber)
|
||||
location := meeting.PlaceString()
|
||||
|
||||
event := fmt.Sprintf(`BEGIN:VEVENT
|
||||
DTSTAMP:%s
|
||||
UID:%s
|
||||
DTSTART;TZID=America/Chicago:%s
|
||||
RRULE:FREQ=WEEKLY;BYDAY=%s;UNTIL=%s
|
||||
DTEND;TZID=America/Chicago:%s
|
||||
SUMMARY:%s
|
||||
DESCRIPTION:%s
|
||||
LOCATION:%s
|
||||
END:VEVENT`, now.Format(config.ICalTimestampFormatLocal), uid, dtStart.Format(config.ICalTimestampFormatLocal), meeting.ByDay(), until.Format(config.ICalTimestampFormatLocal), dtEnd.Format(config.ICalTimestampFormatLocal), summary, strings.Replace(description, "\n", `\n`, -1), location)
|
||||
|
||||
events = append(events, event)
|
||||
}
|
||||
|
||||
// TODO: Make this dynamically requested, parsed & cached from tzurl.org
|
||||
vTimezone := `BEGIN:VTIMEZONE
|
||||
TZID:America/Chicago
|
||||
LAST-MODIFIED:20231222T233358Z
|
||||
TZURL:https://www.tzurl.org/zoneinfo-outlook/America/Chicago
|
||||
X-LIC-LOCATION:America/Chicago
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CDT
|
||||
TZOFFSETFROM:-0600
|
||||
TZOFFSETTO:-0500
|
||||
DTSTART:19700308T020000
|
||||
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CST
|
||||
TZOFFSETFROM:-0500
|
||||
TZOFFSETTO:-0600
|
||||
DTSTART:19701101T020000
|
||||
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
|
||||
END:STANDARD
|
||||
END:VTIMEZONE`
|
||||
|
||||
ics := fmt.Sprintf(`BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
PRODID:-//xevion//Banner Discord Bot//EN
|
||||
CALSCALE:GREGORIAN
|
||||
%s
|
||||
%s
|
||||
END:VCALENDAR`, vTimezone, strings.Join(events, "\n"))
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Files: []*discordgo.File{
|
||||
{
|
||||
Name: fmt.Sprintf("%s-%s-%s_%s.ics", course.Subject, course.CourseNumber, course.SequenceNumber, course.CourseReferenceNumber),
|
||||
ContentType: "text/calendar",
|
||||
Reader: strings.NewReader(ics),
|
||||
},
|
||||
},
|
||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
64
internal/config/config.go
Normal file
64
internal/config/config.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
// Global variables that need to be accessible across packages
|
||||
Ctx context.Context
|
||||
KV *redis.Client
|
||||
Client http.Client
|
||||
Cookies http.CookieJar
|
||||
IsDevelopment bool
|
||||
BaseURL string
|
||||
Environment string
|
||||
CentralTimeLocation *time.Location
|
||||
IsClosing bool = false
|
||||
)
|
||||
|
||||
const (
|
||||
ICalTimestampFormatUtc = "20060102T150405Z"
|
||||
ICalTimestampFormatLocal = "20060102T150405"
|
||||
CentralTimezoneName = "America/Chicago"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Ctx = context.Background()
|
||||
|
||||
var err error
|
||||
CentralTimeLocation, err = time.LoadLocation(CentralTimezoneName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// SetBaseURL sets the base URL for API requests
|
||||
func SetBaseURL(url string) {
|
||||
BaseURL = url
|
||||
}
|
||||
|
||||
// SetEnvironment sets the environment
|
||||
func SetEnvironment(env string) {
|
||||
Environment = env
|
||||
IsDevelopment = env == "development"
|
||||
}
|
||||
|
||||
// SetClient sets the HTTP client
|
||||
func SetClient(c http.Client) {
|
||||
Client = c
|
||||
}
|
||||
|
||||
// SetCookies sets the cookie jar
|
||||
func SetCookies(cj http.CookieJar) {
|
||||
Cookies = cj
|
||||
}
|
||||
|
||||
// SetRedis sets the Redis client
|
||||
func SetRedis(r *redis.Client) {
|
||||
KV = r
|
||||
}
|
||||
322
internal/models/types.go
Normal file
322
internal/models/types.go
Normal file
@@ -0,0 +1,322 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"banner/internal/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const JsonContentType = "application/json"
|
||||
|
||||
type FacultyItem struct {
|
||||
BannerId string `json:"bannerId"`
|
||||
Category *string `json:"category"`
|
||||
Class string `json:"class"`
|
||||
CourseReferenceNumber string `json:"courseReferenceNumber"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Email string `json:"emailAddress"`
|
||||
Primary bool `json:"primaryIndicator"`
|
||||
Term string `json:"term"`
|
||||
}
|
||||
|
||||
type MeetingTimeResponse struct {
|
||||
Category *string `json:"category"`
|
||||
Class string `json:"class"`
|
||||
CourseReferenceNumber string `json:"courseReferenceNumber"`
|
||||
Faculty []FacultyItem
|
||||
MeetingTime struct {
|
||||
Category string `json:"category"`
|
||||
// Some sort of metadata used internally by Banner (net.hedtech.banner.student.schedule.SectionSessionDecorator)
|
||||
Class string `json:"class"`
|
||||
// The start date of the meeting time in MM/DD/YYYY format (e.g. 01/16/2024)
|
||||
StartDate string `json:"startDate"`
|
||||
// The end date of the meeting time in MM/DD/YYYY format (e.g. 05/10/2024)
|
||||
EndDate string `json:"endDate"`
|
||||
// The start time of the meeting time in 24-hour format, hours & minutes, digits only (e.g. 1630)
|
||||
BeginTime string `json:"beginTime"`
|
||||
// The end time of the meeting time in 24-hour format, hours & minutes, digits only (e.g. 1745)
|
||||
EndTime string `json:"endTime"`
|
||||
// The room number within the building this course takes place at (e.g. 3.01.08, 200A)
|
||||
Room string `json:"room"`
|
||||
// The internal identifier for the term this course takes place in (e.g. 202420)
|
||||
Term string `json:"term"`
|
||||
// The internal identifier for the building this course takes place at (e.g. SP1)
|
||||
Building string `json:"building"`
|
||||
// The long name of the building this course takes place at (e.g. San Pedro I - Data Science)
|
||||
BuildingDescription string `json:"buildingDescription"`
|
||||
// The internal identifier for the campus this course takes place at (e.g. 1DT)
|
||||
Campus string `json:"campus"`
|
||||
// The long name of the campus this course takes place at (e.g. Main Campus, Downtown Campus)
|
||||
CampusDescription string `json:"campusDescription"`
|
||||
CourseReferenceNumber string `json:"courseReferenceNumber"`
|
||||
// The number of credit hours this class is worth (assumably)
|
||||
CreditHourSession float64 `json:"creditHourSession"`
|
||||
// The number of hours per week this class meets (e.g. 2.5)
|
||||
HoursWeek float64 `json:"hoursWeek"`
|
||||
// Unknown meaning - e.g. AFF, AIN, AHB, FFF, AFF, EFF, DFF, IFF, EHB, JFF, KFF, BFF, BIN
|
||||
MeetingScheduleType string `json:"meetingScheduleType"`
|
||||
// The short identifier for the meeting type (e.g. FF, HB, OS, OA)
|
||||
MeetingType string `json:"meetingType"`
|
||||
// The long name of the meeting type (e.g. Traditional in-person)
|
||||
MeetingTypeDescription string `json:"meetingTypeDescription"`
|
||||
// A boolean indicating if the class will meet on each Monday of the term
|
||||
Monday bool `json:"monday"`
|
||||
// A boolean indicating if the class will meet on each Tuesday of the term
|
||||
Tuesday bool `json:"tuesday"`
|
||||
// A boolean indicating if the class will meet on each Wednesday of the term
|
||||
Wednesday bool `json:"wednesday"`
|
||||
// A boolean indicating if the class will meet on each Thursday of the term
|
||||
Thursday bool `json:"thursday"`
|
||||
// A boolean indicating if the class will meet on each Friday of the term
|
||||
Friday bool `json:"friday"`
|
||||
// A boolean indicating if the class will meet on each Saturday of the term
|
||||
Saturday bool `json:"saturday"`
|
||||
// A boolean indicating if the class will meet on each Sunday of the term
|
||||
Sunday bool `json:"sunday"`
|
||||
} `json:"meetingTime"`
|
||||
Term string `json:"term"`
|
||||
}
|
||||
|
||||
func (m *MeetingTimeResponse) String() string {
|
||||
switch m.MeetingTime.MeetingType {
|
||||
case "HB":
|
||||
return fmt.Sprintf("%s\nHybrid %s", m.TimeString(), m.PlaceString())
|
||||
case "H2":
|
||||
return fmt.Sprintf("%s\nHybrid %s", m.TimeString(), m.PlaceString())
|
||||
case "H1":
|
||||
return fmt.Sprintf("%s\nHybrid %s", m.TimeString(), m.PlaceString())
|
||||
case "OS":
|
||||
return fmt.Sprintf("%s\nOnline Only", m.TimeString())
|
||||
case "OA":
|
||||
return "No Time\nOnline Asynchronous"
|
||||
case "OH":
|
||||
return fmt.Sprintf("%s\nOnline Partial", m.TimeString())
|
||||
case "ID":
|
||||
return "To Be Arranged"
|
||||
case "FF":
|
||||
return fmt.Sprintf("%s\n%s", m.TimeString(), m.PlaceString())
|
||||
}
|
||||
|
||||
// TODO: Add error log
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
func (m *MeetingTimeResponse) TimeString() string {
|
||||
startTime := m.StartTime()
|
||||
endTime := m.EndTime()
|
||||
|
||||
if startTime == nil || endTime == nil {
|
||||
return "???"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s %s-%s", utils.WeekdaysToString(m.Days()), m.StartTime().String(), m.EndTime().String())
|
||||
}
|
||||
|
||||
// PlaceString returns a formatted string best representing the place of the meeting time
|
||||
func (m *MeetingTimeResponse) PlaceString() string {
|
||||
mt := m.MeetingTime
|
||||
|
||||
// TODO: ADd format case for partial online classes
|
||||
if mt.Room == "" {
|
||||
return "Online"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s | %s | %s %s", mt.CampusDescription, mt.BuildingDescription, mt.Building, mt.Room)
|
||||
}
|
||||
|
||||
func (m *MeetingTimeResponse) Days() map[time.Weekday]bool {
|
||||
days := map[time.Weekday]bool{}
|
||||
|
||||
days[time.Monday] = m.MeetingTime.Monday
|
||||
days[time.Tuesday] = m.MeetingTime.Tuesday
|
||||
days[time.Wednesday] = m.MeetingTime.Wednesday
|
||||
days[time.Thursday] = m.MeetingTime.Thursday
|
||||
days[time.Friday] = m.MeetingTime.Friday
|
||||
days[time.Saturday] = m.MeetingTime.Saturday
|
||||
|
||||
return days
|
||||
}
|
||||
|
||||
// Returns the BYDAY value for the iCalendar RRule format
|
||||
func (m *MeetingTimeResponse) ByDay() string {
|
||||
days := []string{}
|
||||
|
||||
if m.MeetingTime.Sunday {
|
||||
days = append(days, "SU")
|
||||
}
|
||||
if m.MeetingTime.Monday {
|
||||
days = append(days, "MO")
|
||||
}
|
||||
if m.MeetingTime.Tuesday {
|
||||
days = append(days, "TU")
|
||||
}
|
||||
if m.MeetingTime.Wednesday {
|
||||
days = append(days, "WE")
|
||||
}
|
||||
if m.MeetingTime.Thursday {
|
||||
days = append(days, "TH")
|
||||
}
|
||||
if m.MeetingTime.Friday {
|
||||
days = append(days, "FR")
|
||||
}
|
||||
if m.MeetingTime.Saturday {
|
||||
days = append(days, "SA")
|
||||
}
|
||||
|
||||
return strings.Join(days, ",")
|
||||
}
|
||||
|
||||
const layout = "01/02/2006"
|
||||
|
||||
// StartDay returns the start date of the meeting time as a time.Time object
|
||||
// This is not cached and is parsed on each invocation. It may also panic without handling.
|
||||
func (m *MeetingTimeResponse) StartDay() time.Time {
|
||||
t, err := time.Parse(layout, m.MeetingTime.StartDate)
|
||||
if err != nil {
|
||||
log.Panic().Stack().Err(err).Str("raw", m.MeetingTime.StartDate).Msg("Cannot parse start date")
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// EndDay returns the end date of the meeting time as a time.Time object.
|
||||
// This is not cached and is parsed on each invocation. It may also panic without handling.
|
||||
func (m *MeetingTimeResponse) EndDay() time.Time {
|
||||
t, err := time.Parse(layout, m.MeetingTime.EndDate)
|
||||
if err != nil {
|
||||
log.Panic().Stack().Err(err).Str("raw", m.MeetingTime.EndDate).Msg("Cannot parse end date")
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// StartTime returns the start time of the meeting time as a NaiveTime object
|
||||
// This is not cached and is parsed on each invocation. It may also panic without handling.
|
||||
func (m *MeetingTimeResponse) StartTime() *utils.NaiveTime {
|
||||
raw := m.MeetingTime.BeginTime
|
||||
if raw == "" {
|
||||
log.Panic().Stack().Msg("Start time is empty")
|
||||
}
|
||||
|
||||
value, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
log.Panic().Stack().Err(err).Str("raw", raw).Msg("Cannot parse start time integer")
|
||||
}
|
||||
|
||||
return utils.ParseNaiveTime(value)
|
||||
}
|
||||
|
||||
// EndTime returns the end time of the meeting time as a NaiveTime object
|
||||
// This is not cached and is parsed on each invocation. It may also panic without handling.
|
||||
func (m *MeetingTimeResponse) EndTime() *utils.NaiveTime {
|
||||
raw := m.MeetingTime.EndTime
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
log.Panic().Stack().Err(err).Str("raw", raw).Msg("Cannot parse end time integer")
|
||||
}
|
||||
|
||||
return utils.ParseNaiveTime(value)
|
||||
}
|
||||
|
||||
// Converts the meeting time to a string that satisfies the iCalendar RRule format
|
||||
func (m *MeetingTimeResponse) RRule() string {
|
||||
sb := strings.Builder{}
|
||||
|
||||
sb.WriteString("FREQ=WEEKLY;")
|
||||
sb.WriteString(fmt.Sprintf("UNTIL=%s;", m.EndDay().UTC().Format(config.ICalTimestampFormatUtc)))
|
||||
sb.WriteString(fmt.Sprintf("BYDAY=%s;", m.ByDay()))
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
type SearchResult struct {
|
||||
Success bool `json:"success"`
|
||||
TotalCount int `json:"totalCount"`
|
||||
PageOffset int `json:"pageOffset"`
|
||||
PageMaxSize int `json:"pageMaxSize"`
|
||||
PathMode string `json:"pathMode"`
|
||||
SearchResultsConfig []struct {
|
||||
Config string `json:"config"`
|
||||
Display string `json:"display"`
|
||||
} `json:"searchResultsConfig"`
|
||||
Data []Course `json:"data"`
|
||||
}
|
||||
|
||||
type Course struct {
|
||||
// A internal identifier not used outside of the Banner system
|
||||
Id int `json:"id"`
|
||||
// The internal identifier for the term this class is in (e.g. 202420)
|
||||
Term string `json:"term"`
|
||||
// The human-readable name of the term this class is in (e.g. Fall 2021)
|
||||
TermDesc string `json:"termDesc"`
|
||||
// The specific identifier that describes this individual course. CRNs are unique to a term. (TODO: Verify this is true)
|
||||
CourseReferenceNumber string `json:"courseReferenceNumber"`
|
||||
// A rarely used identifier that species which part of the given term this course is in. By default, this is "1" to encompass the entire term. (e.g. B6, B5)
|
||||
PartOfTerm string `json:"partOfTerm"`
|
||||
// The 4-digit course code that defines this course (e.g. 3743, 0120, 4855, 7339), but not the specific instance (see CourseReferenceNumber)
|
||||
CourseNumber string `json:"courseNumber"`
|
||||
// The short acronym of the course subject (e.g. CS, AEPI)
|
||||
Subject string `json:"subject"`
|
||||
// The full name of the course subject (e.g. Computer Science, Academic English Program-Intl.)
|
||||
SubjectDescription string `json:"subjectDescription"`
|
||||
// The specific section of the course (e.g. 001, 002)
|
||||
SequenceNumber string `json:"sequenceNumber"`
|
||||
// The long name of the campus this course takes place at (e.g. Main Campus, Downtown Campus)
|
||||
CampusDescription string `json:"campusDescription"`
|
||||
// e.g. Lecture, Seminar, Dissertation, Internship, Independent Study, Thesis, Self-paced, Laboratory
|
||||
ScheduleTypeDescription string `json:"scheduleTypeDescription"`
|
||||
// The long name of the course (generally)
|
||||
CourseTitle string `json:"courseTitle"`
|
||||
CreditHours int `json:"creditHours"`
|
||||
// The maximum number of students that can enroll in this course
|
||||
MaximumEnrollment int `json:"maximumEnrollment"`
|
||||
// The number of students currently enrolled in this course
|
||||
Enrollment int `json:"enrollment"`
|
||||
// The number of seats available in this course (MaximumEnrollment - Enrollment)
|
||||
SeatsAvailable int `json:"seatsAvailable"`
|
||||
// The number of students that could waitlist for this course
|
||||
WaitCapacity int `json:"waitCapacity"`
|
||||
// The number of students currently on the waitlist for this course
|
||||
WaitCount int `json:"waitCount"`
|
||||
CrossList *string `json:"crossList"`
|
||||
CrossListCapacity *int `json:"crossListCapacity"`
|
||||
CrossListCount *int `json:"crossListCount"`
|
||||
CrossListAvailable *int `json:"crossListAvailable"`
|
||||
CreditHourHigh *int `json:"creditHourHigh"`
|
||||
CreditHourLow *int `json:"creditHourLow"`
|
||||
CreditHourIndicator *string `json:"creditHourIndicator"`
|
||||
OpenSection bool `json:"openSection"`
|
||||
LinkIdentifier *string `json:"linkIdentifier"`
|
||||
IsSectionLinked bool `json:"isSectionLinked"`
|
||||
// A combination of the subject and course number (e.g. subject=CS, courseNumber=3443 => "CS3443")
|
||||
SubjectCourse string `json:"subjectCourse"`
|
||||
ReservedSeatSummary *string `json:"reservedSeatSummary"`
|
||||
InstructionalMethod string `json:"instructionalMethod"`
|
||||
InstructionalMethodDescription string `json:"instructionalMethodDescription"`
|
||||
SectionAttributes []struct {
|
||||
// A internal API class identifier used by Banner
|
||||
Class string `json:"class"`
|
||||
CourseReferenceNumber string `json:"courseReferenceNumber"`
|
||||
// UPPR, ZIEP, AIS, LEWR, ZZSL, 090, GRAD, ZZTL, 020, BU, CLEP
|
||||
Code string `json:"code"`
|
||||
// Seems to be the fully qualified meaning of the Code (Upper, Intensive English Program...)
|
||||
Description string `json:"description"`
|
||||
TermCode string `json:"termCode"`
|
||||
// Unknown; always false
|
||||
IsZtcAttribute bool `json:"isZTCAttribute"`
|
||||
} `json:"sectionAttributes"`
|
||||
Faculty []FacultyItem `json:"faculty"`
|
||||
MeetingsFaculty []MeetingTimeResponse `json:"meetingsFaculty"`
|
||||
}
|
||||
|
||||
func (course Course) MarshalBinary() ([]byte, error) {
|
||||
return json.Marshal(course)
|
||||
}
|
||||
12
internal/utils/errors.go
Normal file
12
internal/utils/errors.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
type UnexpectedContentTypeError struct {
|
||||
Expected string
|
||||
Actual string
|
||||
}
|
||||
|
||||
func (e *UnexpectedContentTypeError) Error() string {
|
||||
return fmt.Sprintf("Expected content type '%s', received '%s'", e.Expected, e.Actual)
|
||||
}
|
||||
376
internal/utils/helpers.go
Normal file
376
internal/utils/helpers.go
Normal file
@@ -0,0 +1,376 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/rs/zerolog"
|
||||
log "github.com/rs/zerolog/log"
|
||||
|
||||
"banner/internal/config"
|
||||
)
|
||||
|
||||
// BuildRequestWithBody builds a request with the given method, path, parameters, and body
|
||||
func BuildRequestWithBody(method string, path string, params map[string]string, body io.Reader) *http.Request {
|
||||
// Builds a URL for the given path and parameters
|
||||
requestUrl := config.BaseURL + path
|
||||
|
||||
if params != nil {
|
||||
takenFirst := false
|
||||
for key, value := range params {
|
||||
paramChar := "&"
|
||||
if !takenFirst {
|
||||
paramChar = "?"
|
||||
takenFirst = true
|
||||
}
|
||||
|
||||
requestUrl += paramChar + url.QueryEscape(key) + "=" + url.QueryEscape(value)
|
||||
}
|
||||
}
|
||||
|
||||
request, _ := http.NewRequest(method, requestUrl, body)
|
||||
AddUserAgent(request)
|
||||
return request
|
||||
}
|
||||
|
||||
// BuildRequest builds a request with the given method, path, and parameters and an empty body
|
||||
func BuildRequest(method string, path string, params map[string]string) *http.Request {
|
||||
return BuildRequestWithBody(method, path, params, nil)
|
||||
}
|
||||
|
||||
// AddUserAgent adds a false but consistent user agent to the request
|
||||
func AddUserAgent(req *http.Request) {
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
|
||||
}
|
||||
|
||||
// ContentTypeMatch checks if the response has the given content type
|
||||
func ContentTypeMatch(response *http.Response, search string) bool {
|
||||
contentType := response.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
return search == "application/octect-stream"
|
||||
}
|
||||
|
||||
return strings.HasPrefix(contentType, search)
|
||||
}
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
// RandomString returns a random string of length n using the letterBytes constant
|
||||
// The constant used is specifically chosen to mimic Ellucian's banner session ID generation.
|
||||
func RandomString(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// DiscordGoLogger is a specialized helper function that implements discordgo's global logging interface.
|
||||
// It directs all logs to the zerolog implementation.
|
||||
func DiscordGoLogger(msgL, caller int, format string, a ...interface{}) {
|
||||
pc, file, line, _ := runtime.Caller(caller)
|
||||
|
||||
files := strings.Split(file, "/")
|
||||
file = files[len(files)-1]
|
||||
|
||||
name := runtime.FuncForPC(pc).Name()
|
||||
fns := strings.Split(name, ".")
|
||||
name = fns[len(fns)-1]
|
||||
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
|
||||
var event *zerolog.Event
|
||||
switch msgL {
|
||||
case 0:
|
||||
event = log.Debug()
|
||||
case 1:
|
||||
event = log.Info()
|
||||
case 2:
|
||||
event = log.Warn()
|
||||
case 3:
|
||||
event = log.Error()
|
||||
default:
|
||||
event = log.Info()
|
||||
}
|
||||
|
||||
event.Str("file", file).Int("line", line).Str("function", name).Msg(msg)
|
||||
}
|
||||
|
||||
// Nonce returns a string made up of the current time in milliseconds, Unix epoch/UTC
|
||||
// This is typically used as a query parameter to prevent request caching in the browser.
|
||||
func Nonce() string {
|
||||
return strconv.Itoa(int(time.Now().UnixMilli()))
|
||||
}
|
||||
|
||||
// Plural is a simple helper function that returns an empty string if n is 1, and "s" otherwise.
|
||||
func Plural(n int) string {
|
||||
if n == 1 {
|
||||
return ""
|
||||
}
|
||||
return "s"
|
||||
}
|
||||
|
||||
// Plurale is a simple helper function that returns an empty string if n is 1, and "ess" otherwise.
|
||||
// This is for words that end in "es" when plural.
|
||||
func Plurale(n int) string {
|
||||
if n == 1 {
|
||||
return ""
|
||||
}
|
||||
return "es"
|
||||
}
|
||||
|
||||
func WeekdaysToString(days map[time.Weekday]bool) string {
|
||||
// If no days are present
|
||||
numDays := len(days)
|
||||
if numDays == 0 {
|
||||
return "None"
|
||||
}
|
||||
|
||||
// If all days are present
|
||||
if numDays == 7 {
|
||||
return "Everyday"
|
||||
}
|
||||
|
||||
str := ""
|
||||
|
||||
if days[time.Monday] {
|
||||
str += "M"
|
||||
}
|
||||
|
||||
if days[time.Tuesday] {
|
||||
str += "Tu"
|
||||
}
|
||||
|
||||
if days[time.Wednesday] {
|
||||
str += "W"
|
||||
}
|
||||
|
||||
if days[time.Thursday] {
|
||||
str += "Th"
|
||||
}
|
||||
|
||||
if days[time.Friday] {
|
||||
str += "F"
|
||||
}
|
||||
|
||||
if days[time.Saturday] {
|
||||
str += "Sa"
|
||||
}
|
||||
|
||||
if days[time.Sunday] {
|
||||
str += "Su"
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
type NaiveTime struct {
|
||||
Hours uint
|
||||
Minutes uint
|
||||
}
|
||||
|
||||
func (nt *NaiveTime) Sub(other *NaiveTime) time.Duration {
|
||||
return time.Hour*time.Duration(nt.Hours-other.Hours) + time.Minute*time.Duration(nt.Minutes-other.Minutes)
|
||||
}
|
||||
|
||||
func ParseNaiveTime(integer uint64) *NaiveTime {
|
||||
minutes := uint(integer % 100)
|
||||
hours := uint(integer / 100)
|
||||
|
||||
return &NaiveTime{Hours: hours, Minutes: minutes}
|
||||
}
|
||||
|
||||
func (nt NaiveTime) String() string {
|
||||
meridiem := "AM"
|
||||
hour := nt.Hours
|
||||
if nt.Hours >= 12 {
|
||||
meridiem = "PM"
|
||||
if nt.Hours > 12 {
|
||||
hour -= 12
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%d:%02d%s", hour, nt.Minutes, meridiem)
|
||||
}
|
||||
|
||||
func GetFirstEnv(key ...string) string {
|
||||
for _, k := range key {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetIntPointer returns a pointer to the given value.
|
||||
// This function is useful for discordgo, which inexplicably requires pointers to integers for minLength arguments.
|
||||
func GetIntPointer(value int) *int {
|
||||
return &value
|
||||
}
|
||||
|
||||
// GetFloatPointer returns a pointer to the given value.
|
||||
// This function is useful for discordgo, which inexplicably requires pointers to floats for minLength arguments.
|
||||
func GetFloatPointer(value float64) *float64 {
|
||||
return &value
|
||||
}
|
||||
|
||||
var extensionMap = map[string]string{
|
||||
"text/plain": "txt",
|
||||
"application/json": "json",
|
||||
"text/html": "html",
|
||||
"text/css": "css",
|
||||
"text/csv": "csv",
|
||||
"text/calendar": "ics",
|
||||
"text/markdown": "md",
|
||||
"text/xml": "xml",
|
||||
"text/yaml": "yaml",
|
||||
"text/javascript": "js",
|
||||
"text/vtt": "vtt",
|
||||
"image/jpeg": "jpg",
|
||||
"image/png": "png",
|
||||
"image/gif": "gif",
|
||||
"image/webp": "webp",
|
||||
"image/tiff": "tiff",
|
||||
"image/svg+xml": "svg",
|
||||
"image/bmp": "bmp",
|
||||
"image/vnd.microsoft.icon": "ico",
|
||||
"image/x-icon": "ico",
|
||||
"image/x-xbitmap": "xbm",
|
||||
"image/x-xpixmap": "xpm",
|
||||
"image/x-xwindowdump": "xwd",
|
||||
"image/avif": "avif",
|
||||
"image/apng": "apng",
|
||||
"image/jxl": "jxl",
|
||||
}
|
||||
|
||||
func GuessExtension(contentType string) string {
|
||||
ext, ok := extensionMap[strings.ToLower(contentType)]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
// DumpResponse dumps a response body to a file for debugging purposes
|
||||
func DumpResponse(res *http.Response) {
|
||||
contentType := res.Header.Get("Content-Type")
|
||||
ext := GuessExtension(contentType)
|
||||
|
||||
// Use current time as filename + /dumps/ prefix
|
||||
filename := fmt.Sprintf("dumps/%d.%s", time.Now().Unix(), ext)
|
||||
file, err := os.Create(filename)
|
||||
|
||||
if err != nil {
|
||||
log.Err(err).Stack().Msg("Error creating file")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(file, res.Body)
|
||||
if err != nil {
|
||||
log.Err(err).Stack().Msg("Error copying response body")
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Str("filename", filename).Str("content-type", contentType).Msg("Dumped response body")
|
||||
}
|
||||
|
||||
// ResponseError responds to an interaction with an error message
|
||||
// TODO: Improve with a proper embed and colors
|
||||
func RespondError(session *discordgo.Session, interaction *discordgo.Interaction, message string, err error) error {
|
||||
// Optional: log the error
|
||||
if err != nil {
|
||||
log.Err(err).Stack().Msg(message)
|
||||
}
|
||||
|
||||
return session.InteractionRespond(interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{
|
||||
{
|
||||
Footer: &discordgo.MessageEmbedFooter{
|
||||
Text: fmt.Sprintf("Occurred at %s", time.Now().Format("Monday, January 2, 2006 at 3:04:05PM")),
|
||||
},
|
||||
Description: message,
|
||||
Color: 0xff0000,
|
||||
},
|
||||
},
|
||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func GetFetchedFooter(time time.Time) *discordgo.MessageEmbedFooter {
|
||||
return &discordgo.MessageEmbedFooter{
|
||||
Text: fmt.Sprintf("Fetched at %s", time.In(config.CentralTimeLocation).Format("Monday, January 2, 2006 at 3:04:05PM")),
|
||||
}
|
||||
}
|
||||
|
||||
// GetUser returns the user from the interaction.
|
||||
// This helper method is useful as depending on where the message was sent (guild or DM), the user is in a different field.
|
||||
func GetUser(interaction *discordgo.InteractionCreate) *discordgo.User {
|
||||
// If the interaction is in a guild, the user is kept in the Member field
|
||||
if interaction.Member != nil {
|
||||
return interaction.Member.User
|
||||
}
|
||||
|
||||
// If the interaction is in a DM, the user is kept in the User field
|
||||
return interaction.User
|
||||
}
|
||||
|
||||
// Encode encodes the values into “URL encoded” form
|
||||
// ("bar=baz&foo=quux") sorted by key.
|
||||
func EncodeParams(params map[string]*[]string) string {
|
||||
// Escape hatch for nil
|
||||
if params == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Sort the keys
|
||||
keys := make([]string, 0, len(params))
|
||||
for k := range params {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
var buf strings.Builder
|
||||
for _, k := range keys {
|
||||
// Multiple values are allowed, so extract the slice & prepare the key
|
||||
values := params[k]
|
||||
keyEscaped := url.QueryEscape(k)
|
||||
|
||||
for _, v := range *values {
|
||||
// If any parameters have been written, add the ampersand
|
||||
if buf.Len() > 0 {
|
||||
buf.WriteByte('&')
|
||||
}
|
||||
|
||||
// Write the key and value
|
||||
buf.WriteString(keyEscaped)
|
||||
buf.WriteByte('=')
|
||||
buf.WriteString(url.QueryEscape(v))
|
||||
}
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Point represents a point in 2D space
|
||||
type Point struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
func Slope(p1 Point, p2 Point, x float64) Point {
|
||||
slope := (p2.Y - p1.Y) / (p2.X - p1.X)
|
||||
newY := slope*(x-p1.X) + p1.Y
|
||||
return Point{X: x, Y: newY}
|
||||
}
|
||||
35
internal/utils/logs.go
Normal file
35
internal/utils/logs.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
const timeFormat = "2006-01-02 15:04:05"
|
||||
|
||||
var (
|
||||
stdConsole = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: timeFormat}
|
||||
errConsole = zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: timeFormat}
|
||||
)
|
||||
|
||||
// logSplitter implements zerolog.LevelWriter
|
||||
type LogSplitter struct {
|
||||
Std io.Writer
|
||||
Err io.Writer
|
||||
}
|
||||
|
||||
// Write should not be called
|
||||
func (l LogSplitter) Write(p []byte) (n int, err error) {
|
||||
return l.Std.Write(p)
|
||||
}
|
||||
|
||||
// WriteLevel write to the appropriate output
|
||||
func (l LogSplitter) WriteLevel(level zerolog.Level, p []byte) (n int, err error) {
|
||||
if level <= zerolog.WarnLevel {
|
||||
return l.Std.Write(p)
|
||||
} else {
|
||||
return l.Err.Write(p)
|
||||
}
|
||||
}
|
||||
78
internal/utils/meta.go
Normal file
78
internal/utils/meta.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/redis/go-redis/v9"
|
||||
log "github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// GetGuildName returns the name of the guild with the given ID, utilizing Redis to cache the value
|
||||
func GetGuildName(session *discordgo.Session, guildID string) string {
|
||||
// Check Redis for the guild name
|
||||
guildName, err := config.KV.Get(config.Ctx, "guild:"+guildID+":name").Result()
|
||||
if err != nil && err != redis.Nil {
|
||||
log.Error().Stack().Err(err).Msg("Error getting guild name from Redis")
|
||||
return "err"
|
||||
}
|
||||
|
||||
// If the guild name is invalid (1 character long), then return "unknown"
|
||||
if len(guildName) == 1 {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// If the guild name isn't in Redis, get it from Discord and cache it
|
||||
guild, err := session.Guild(guildID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("Error getting guild name")
|
||||
|
||||
// Store an invalid value in Redis so we don't keep trying to get the guild name
|
||||
_, err := config.KV.Set(config.Ctx, "guild:"+guildID+":name", "x", time.Minute*5).Result()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("Error setting false guild name in Redis")
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Cache the guild name in Redis
|
||||
config.KV.Set(config.Ctx, "guild:"+guildID+":name", guild.Name, time.Hour*3)
|
||||
|
||||
return guild.Name
|
||||
}
|
||||
|
||||
// GetChannelName returns the name of the channel with the given ID, utilizing Redis to cache the value
|
||||
func GetChannelName(session *discordgo.Session, channelID string) string {
|
||||
// Check Redis for the channel name
|
||||
channelName, err := config.KV.Get(config.Ctx, "channel:"+channelID+":name").Result()
|
||||
if err != nil && err != redis.Nil {
|
||||
log.Error().Stack().Err(err).Msg("Error getting channel name from Redis")
|
||||
return "err"
|
||||
}
|
||||
|
||||
// If the channel name is invalid (1 character long), then return "unknown"
|
||||
if len(channelName) == 1 {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// If the channel name isn't in Redis, get it from Discord and cache it
|
||||
channel, err := session.Channel(channelID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("Error getting channel name")
|
||||
|
||||
// Store an invalid value in Redis so we don't keep trying to get the channel name
|
||||
_, err := config.KV.Set(config.Ctx, "channel:"+channelID+":name", "x", time.Minute*5).Result()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("Error setting false channel name in Redis")
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Cache the channel name in Redis
|
||||
config.KV.Set(config.Ctx, "channel:"+channelID+":name", channel.Name, time.Hour*3)
|
||||
|
||||
return channel.Name
|
||||
}
|
||||
146
internal/utils/term.go
Normal file
146
internal/utils/term.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"banner/internal/config"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Term selection should yield smart results based on the current time, as well as the input provided.
|
||||
// Fall 2024, "spring" => Spring 2025
|
||||
// Fall 2024, "fall" => Fall 2025
|
||||
// Summer 2024, "fall" => Fall 2024
|
||||
|
||||
const (
|
||||
Spring = iota
|
||||
Summer
|
||||
Fall
|
||||
)
|
||||
|
||||
type Term struct {
|
||||
Year uint16
|
||||
Season uint8
|
||||
}
|
||||
|
||||
var (
|
||||
SpringRange, SummerRange, FallRange YearDayRange
|
||||
)
|
||||
|
||||
func init() {
|
||||
SpringRange, SummerRange, FallRange = GetYearDayRange(uint16(time.Now().Year()))
|
||||
|
||||
currentTerm, nextTerm := GetCurrentTerm(time.Now())
|
||||
log.Debug().Str("CurrentTerm", fmt.Sprintf("%+v", currentTerm)).Str("NextTerm", fmt.Sprintf("%+v", nextTerm)).Msg("GetCurrentTerm")
|
||||
}
|
||||
|
||||
type YearDayRange struct {
|
||||
Start uint16
|
||||
End uint16
|
||||
}
|
||||
|
||||
// GetYearDayRange returns the start and end day of each term for the given year.
|
||||
// This could technically introduce race conditions, but it's more likely confusion from UTC will be a greater issue.
|
||||
// Spring: January 14th to May
|
||||
// Summer: May 25th - August 15th
|
||||
// Fall: August 18th - December 10th
|
||||
func GetYearDayRange(year uint16) (YearDayRange, YearDayRange, YearDayRange) {
|
||||
springStart := time.Date(int(year), time.January, 14, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
springEnd := time.Date(int(year), time.May, 1, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
summerStart := time.Date(int(year), time.May, 25, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
summerEnd := time.Date(int(year), time.August, 15, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
fallStart := time.Date(int(year), time.August, 18, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
fallEnd := time.Date(int(year), time.December, 10, 0, 0, 0, 0, config.CentralTimeLocation).YearDay()
|
||||
|
||||
return YearDayRange{
|
||||
Start: uint16(springStart),
|
||||
End: uint16(springEnd),
|
||||
}, YearDayRange{
|
||||
Start: uint16(summerStart),
|
||||
End: uint16(summerEnd),
|
||||
}, YearDayRange{
|
||||
Start: uint16(fallStart),
|
||||
End: uint16(fallEnd),
|
||||
}
|
||||
}
|
||||
|
||||
// GetCurrentTerm returns the current term, and the next term. Only the first term is nillable.
|
||||
// YearDay ranges are inclusive of the start, and exclusive of the end.
|
||||
func GetCurrentTerm(now time.Time) (*Term, *Term) {
|
||||
year := uint16(now.Year())
|
||||
dayOfYear := uint16(now.YearDay())
|
||||
|
||||
// Fall of 2024 => 202410
|
||||
// Spring of 2024 => 202420
|
||||
// Fall of 2025 => 202510
|
||||
// Summer of 2025 => 202530
|
||||
|
||||
if (dayOfYear < SpringRange.Start) || (dayOfYear >= FallRange.End) {
|
||||
// Fall over, Spring not yet begun
|
||||
return nil, &Term{Year: year + 1, Season: Spring}
|
||||
} else if (dayOfYear >= SpringRange.Start) && (dayOfYear < SpringRange.End) {
|
||||
// Spring
|
||||
return &Term{Year: year, Season: Spring}, &Term{Year: year, Season: Summer}
|
||||
} else if dayOfYear < SummerRange.Start {
|
||||
// Spring over, Summer not yet begun
|
||||
return nil, &Term{Year: year, Season: Summer}
|
||||
} else if (dayOfYear >= SummerRange.Start) && (dayOfYear < SummerRange.End) {
|
||||
// Summer
|
||||
return &Term{Year: year, Season: Summer}, &Term{Year: year, Season: Fall}
|
||||
} else if dayOfYear < FallRange.Start {
|
||||
// Summer over, Fall not yet begun
|
||||
return nil, &Term{Year: year + 1, Season: Fall}
|
||||
} else if (dayOfYear >= FallRange.Start) && (dayOfYear < FallRange.End) {
|
||||
// Fall
|
||||
return &Term{Year: year + 1, Season: Fall}, nil
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("Impossible Code Reached (dayOfYear: %d)", dayOfYear))
|
||||
}
|
||||
|
||||
// ParseTerm converts a Banner term code to a Term struct
|
||||
func ParseTerm(code string) Term {
|
||||
year, _ := strconv.ParseUint(code[0:4], 10, 16)
|
||||
|
||||
var season uint8
|
||||
termCode := code[4:6]
|
||||
switch termCode {
|
||||
case "10":
|
||||
season = Fall
|
||||
case "20":
|
||||
season = Spring
|
||||
case "30":
|
||||
season = Summer
|
||||
}
|
||||
|
||||
return Term{
|
||||
Year: uint16(year),
|
||||
Season: season,
|
||||
}
|
||||
}
|
||||
|
||||
// TermToBannerTerm converts a Term struct to a Banner term code
|
||||
func (term Term) ToString() string {
|
||||
var season string
|
||||
switch term.Season {
|
||||
case Fall:
|
||||
season = "10"
|
||||
case Spring:
|
||||
season = "20"
|
||||
case Summer:
|
||||
season = "30"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%d%s", term.Year, season)
|
||||
}
|
||||
|
||||
// Default chooses the default term, which is the current term if it exists, otherwise the next term.
|
||||
func Default(t time.Time) Term {
|
||||
currentTerm, nextTerm := GetCurrentTerm(t)
|
||||
if currentTerm == nil {
|
||||
return *nextTerm
|
||||
}
|
||||
return *currentTerm
|
||||
}
|
||||
Reference in New Issue
Block a user