Course retrieval from redis, setup ICS command data with course detailsl

This commit is contained in:
2024-01-30 15:27:56 -06:00
parent 3ec5644345
commit 4ac28bd210
2 changed files with 32 additions and 11 deletions

23
api.go
View File

@@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@@ -458,3 +459,25 @@ func ResetDataForm() {
log.Fatal().Err(err).Msg("Failed to reset data form") log.Fatal().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) (*Course, error) {
// Retrieve raw data
result, err := kv.Get(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 Course
err = json.Unmarshal([]byte(result), &course)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal course: %w", err)
}
return &course, nil
}

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"time" "time"
@@ -272,15 +273,14 @@ var IcsCommandDefinition = &discordgo.ApplicationCommand{
func IcsCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error { func IcsCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error {
crn := i.ApplicationCommandData().Options[0].IntValue() crn := i.ApplicationCommandData().Options[0].IntValue()
course, err := GetCourse(strconv.Itoa(int(crn)))
if err != nil {
return fmt.Errorf("Error retrieving course data: %w", err)
}
meetingTimes, err := GetCourseMeetingTime(202420, int(crn)) meetingTimes, err := GetCourseMeetingTime(202420, int(crn))
if err != nil { if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ return fmt.Errorf("Error requesting meeting time: %w", err)
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error getting meeting time",
},
})
return err
} }
events := []string{} events := []string{}
@@ -298,10 +298,8 @@ func IcsCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) err
endDay := meeting.EndDay() endDay := meeting.EndDay()
until := time.Date(endDay.Year(), endDay.Month(), endDay.Day(), 23, 59, 59, 0, CentralTimeLocation) until := time.Date(endDay.Year(), endDay.Month(), endDay.Day(), 23, 59, 59, 0, CentralTimeLocation)
summary := fmt.Sprintf("{Insert Classname Here} (CRN %s)", meeting.CourseReferenceNumber) summary := fmt.Sprintf("%s (CRN %s)", course.CourseTitle, meeting.CourseReferenceNumber)
description := fmt.Sprintf(`Instructor: {Insert Instructor Here} description := fmt.Sprintf("Instructor: %s\nSection: %s\nCRN: %s", course.Faculty[0].DisplayName, course.SequenceNumber, meeting.CourseReferenceNumber)
Section: {Insert Section Here}
CRN: %s`, meeting.CourseReferenceNumber)
location := meeting.PlaceString() location := meeting.PlaceString()
event := fmt.Sprintf(`BEGIN:VEVENT event := fmt.Sprintf(`BEGIN:VEVENT