uofgcal/main.go
2024-10-24 15:31:10 +01:00

66 lines
1.3 KiB
Go

package main
import (
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/charmbracelet/log"
)
const calendar_url = "https://frontdoor.spa.gla.ac.uk/spacett/download/uogtimetable.ics"
var authorization string
func fetch_calendar() ([]byte, error) {
// create request
req, err := http.NewRequest("GET", calendar_url, nil)
if err != nil {
return []byte{}, err
}
req.Header.Set("Authorization", authorization)
// send request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return []byte{}, err
}
// read response
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
// check if authentication failed
if resp.StatusCode == 401 {
return []byte{}, errors.New("authentication failed")
} else {
return body, nil
}
}
func main() {
// collect guid and password
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)
// fetch calendar
calendar, err := fetch_calendar()
if err != nil {
log.Fatal(err)
}
os.WriteFile("calendar.ics", calendar, 0644)
}