From 929de3b541b4478dc1ba9005a86b425a5bfa8324 Mon Sep 17 00:00:00 2001 From: Jan-Matthis Date: Wed, 12 Jun 2024 18:14:32 +0200 Subject: [PATCH] Created an interface for the planned events --- main.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/main.go b/main.go index a4eeac1..76ecf88 100644 --- a/main.go +++ b/main.go @@ -20,12 +20,16 @@ import ( "embed" "encoding/json" "errors" + "fmt" "io" "io/fs" "log" "net/http" "os" + "regexp" + "strconv" "strings" + "time" "github.com/gin-gonic/gin" "gopkg.in/yaml.v3" @@ -39,6 +43,12 @@ type AgentStateResult struct { } `json:"agent-state-update"` } +type Event struct { + Title string + Start int + End int +} + type DisplayConfig struct { Text string `json:"text"` Color string `json:"color"` @@ -141,6 +151,70 @@ func setupRouter() *gin.Engine { c.JSON(http.StatusOK, result.Update.State == "capturing") }) + r.GET("/calendar", func(c *gin.Context) { + client := &http.Client{} + cutoff := time.Now().UnixMilli() + int64(86400000) + url := config.Opencast.Url + "/recordings/calendar.json?agentid=" + config.Opencast.Agent + "&cutoff=" + fmt.Sprint(cutoff) + "×tamp=true" + fmt.Print("\n" + url + "\n") + req, err := http.NewRequest("GET", url, nil) + req.SetBasicAuth(config.Opencast.Username, config.Opencast.Password) + resp, err := client.Do(req) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadGateway, nil) + return + } + if resp.StatusCode != 200 { + log.Println(resp) + c.JSON(resp.StatusCode, nil) + return + } + + bodyText, err := io.ReadAll(resp.Body) + s := string([]byte(bodyText)) + start := regexp.MustCompile(`"startDate":[\d]+`) + end := regexp.MustCompile(`"endDate":[\d]+`) + t := regexp.MustCompile(`"event.title":"[^"]+"`) + + startDates := start.FindAllString(s, -1) + endDates := end.FindAllString(s, -1) + titles := t.FindAllString(s, -1) + + fmt.Println(startDates) + fmt.Println(endDates) + fmt.Println(titles) + + fmt.Println(len(titles)) + + //reg := regexp.MustCompile(`"data":{".*?"recording":{}}`) + //result := reg.FindString(s) + //fmt.Println(result) + //fmt.Println("\n\n") + //j, err := json.Marshal(result) + cutoff_title := regexp.MustCompile(`"event.title":`) + cutoff_start := regexp.MustCompile(`"startDate":`) + cutoff_end := regexp.MustCompile(`"endDate":`) + var events []Event + for i := range titles { + start_temp := cutoff_start.ReplaceAllLiteralString(startDates[i], "") + start, _ := strconv.Atoi(start_temp) + end_temp := cutoff_end.ReplaceAllLiteralString(endDates[i], "") + end, _ := strconv.Atoi(end_temp) + e := Event{Title: cutoff_title.ReplaceAllLiteralString(titles[i], ""), + Start: start, + End: end} + events = append(events, e) + } + + if len(titles) > 0 { + fmt.Println(fmt.Sprint(len(titles)) + " Events planned.") + c.JSON(http.StatusOK, events) + } else { + fmt.Println("No Events") + c.JSON(http.StatusNoContent, "[]") + } + }) + return r } @@ -151,4 +225,5 @@ func main() { } r := setupRouter() r.Run(config.Listen) + }