package fetch import ( "encoding/base64" "fmt" "net/http" "strings" "time" ics "github.com/arran4/golang-ical" "github.com/charmbracelet/log" cron "github.com/go-co-op/gocron/v2" ) const calendar_url = "https://frontdoor.spa.gla.ac.uk/spacett/download/uogtimetable.ics" var authorization string var Scheduler *cron.Scheduler func CollectAuth() { var ( guid string password string ) fmt.Print("Enter your GUID: ") fmt.Scan(&guid) fmt.Print("Enter your password: ") fmt.Scan(&password) b64 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", guid, password))) authorization = fmt.Sprintf("Basic %s", b64) } func StartScheduler() { // create scheduler Scheduler, err := cron.NewScheduler() if err != nil { log.Fatal(err) } j, err := Scheduler.NewJob( // every 24 hours cron.DurationJob( 24*time.Hour, ), // update the calendar cron.NewTask( update_calendar, ), ) if err != nil { log.Fatal(err) } // run job and start scheduler go j.RunNow() Scheduler.Start() } func update_calendar() { // create request req, err := http.NewRequest("GET", calendar_url, nil) if err != nil { log.Fatal(err) } req.Header.Set("Authorization", authorization) // send request resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() // check if authentication failed if resp.StatusCode == 401 { log.Fatal("authentication failed") } // parse ics file calendar, err := ics.ParseCalendar(resp.Body) if err != nil { log.Fatal(err) } for _, event := range calendar.Events() { name := event.GetProperty(ics.ComponentPropertySummary).Value location := strings.TrimSpace(strings.Replace(event.GetProperty(ics.ComponentPropertyLocation).Value, "ON CAMPUS:", "", 1)) log.Infof("%s: %s", name, location) } }