From f5f59798df84e2a924b8159f6c8f63ff706f2cdc Mon Sep 17 00:00:00 2001 From: newt Date: Fri, 25 Oct 2024 12:48:44 +0100 Subject: [PATCH] feat: parse ics --- fetch/fetch.go | 79 +++++++++++++++++++++++++++----------------------- go.mod | 4 ++- go.sum | 13 +++++++++ main.go | 4 +-- tray/tray.go | 2 +- 5 files changed, 61 insertions(+), 41 deletions(-) diff --git a/fetch/fetch.go b/fetch/fetch.go index 52f11b0..e139769 100644 --- a/fetch/fetch.go +++ b/fetch/fetch.go @@ -3,19 +3,19 @@ package fetch import ( "encoding/base64" "fmt" - "io" "net/http" - "os" + "strings" "time" + ics "github.com/arran4/golang-ical" "github.com/charmbracelet/log" - "github.com/go-co-op/gocron/v2" + 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 *gocron.Scheduler +var Scheduler *cron.Scheduler func CollectAuth() { var ( @@ -32,48 +32,20 @@ func CollectAuth() { func StartScheduler() { // create scheduler - Scheduler, err := gocron.NewScheduler() + Scheduler, err := cron.NewScheduler() if err != nil { log.Fatal(err) } j, err := Scheduler.NewJob( // every 24 hours - gocron.DurationJob( + cron.DurationJob( 24*time.Hour, ), - // fetch the calendar - gocron.NewTask( - func() { - // 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) - } - - // read response - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - // check if authentication failed - if resp.StatusCode == 401 { - log.Fatal("authentication failed") - } else { - os.WriteFile("calendar.ics", body, 0644) - log.Info("Written calendar to calendar.ics") - } - }, + // update the calendar + cron.NewTask( + update_calendar, ), ) if err != nil { @@ -84,3 +56,36 @@ func StartScheduler() { 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) + } +} diff --git a/go.mod b/go.mod index 25fbeef..d1df8ea 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,12 @@ -module newty.dev/uofgcal +module git.newty.dev/uofgcal go 1.23.2 require github.com/charmbracelet/log v0.4.0 require ( + github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect + github.com/arran4/golang-ical v0.3.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/lipgloss v0.10.0 // indirect github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect diff --git a/go.sum b/go.sum index fe77dcc..5d2b26f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,12 @@ +github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 h1:N5Vqww5QISEHsWHOWDEx4PzdIay3Cg0Jp7zItq2ZAro= +github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:GnKXcK+7DYNy/8w2Ex//Uql4IgfaU82Cd5rWKb7ah00= +github.com/apognu/gocal v0.9.0 h1:2lGdZprjYs9A6l1RTEmapmpE1PiDbXNX8bUVqZt3vm4= +github.com/apognu/gocal v0.9.0/go.mod h1:ZOJfNOqpz8aasi3uqzDu+eWTT6VuEa/TvQWiYYWlb80= +github.com/arran4/golang-ical v0.3.1 h1:v13B3eQZ9VDHTAvT6M11vVzxYgcYmjyPBE2eAZl3VZk= +github.com/arran4/golang-ical v0.3.1/go.mod h1:LZWxF8ZIu/sjBVUCV0udiVPrQAgq3V0aa0RfbO99Qkk= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/channelmeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61/go.mod h1:Rp8e0DCtEKwXFOC6JPJQVTz8tuGoGvw6Xfexggh/ed0= github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= @@ -30,6 +37,7 @@ github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -55,6 +63,7 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -72,7 +81,9 @@ github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:s github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= @@ -92,8 +103,10 @@ golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/Knetic/govaluate.v3 v3.0.0/go.mod h1:csKLBORsPbafmSCGTEh3U7Ozmsuq8ZSIlKk1bcqph0E= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index c62c3fa..c5686d9 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( + "git.newty.dev/uofgcal/fetch" + "git.newty.dev/uofgcal/tray" "github.com/getlantern/systray" - "newty.dev/uofgcal/fetch" - "newty.dev/uofgcal/tray" ) func main() { diff --git a/tray/tray.go b/tray/tray.go index a51a36f..5574c82 100644 --- a/tray/tray.go +++ b/tray/tray.go @@ -3,9 +3,9 @@ package tray import ( _ "embed" + "git.newty.dev/uofgcal/fetch" "github.com/charmbracelet/log" "github.com/getlantern/systray" - "newty.dev/uofgcal/fetch" ) //go:embed uofg.ico