2024-10-24 14:54:51 +00:00
|
|
|
package fetch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-10-25 11:48:44 +00:00
|
|
|
"strings"
|
2024-10-24 14:54:51 +00:00
|
|
|
"time"
|
|
|
|
|
2024-10-25 11:48:44 +00:00
|
|
|
ics "github.com/arran4/golang-ical"
|
2024-10-24 14:54:51 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2024-10-25 11:48:44 +00:00
|
|
|
cron "github.com/go-co-op/gocron/v2"
|
2024-10-24 14:54:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const calendar_url = "https://frontdoor.spa.gla.ac.uk/spacett/download/uogtimetable.ics"
|
|
|
|
|
|
|
|
var authorization string
|
2024-10-25 11:48:44 +00:00
|
|
|
var Scheduler *cron.Scheduler
|
2024-10-24 14:54:51 +00:00
|
|
|
|
|
|
|
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
|
2024-10-25 11:48:44 +00:00
|
|
|
Scheduler, err := cron.NewScheduler()
|
2024-10-24 14:54:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := Scheduler.NewJob(
|
|
|
|
// every 24 hours
|
2024-10-25 11:48:44 +00:00
|
|
|
cron.DurationJob(
|
2024-10-24 14:54:51 +00:00
|
|
|
24*time.Hour,
|
|
|
|
),
|
|
|
|
|
2024-10-25 11:48:44 +00:00
|
|
|
// update the calendar
|
|
|
|
cron.NewTask(
|
|
|
|
update_calendar,
|
2024-10-24 14:54:51 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run job and start scheduler
|
|
|
|
go j.RunNow()
|
|
|
|
Scheduler.Start()
|
|
|
|
}
|
2024-10-25 11:48:44 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|