-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical.go
59 lines (46 loc) · 954 Bytes
/
ical.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"net/http"
"time"
log "github.com/Sirupsen/logrus"
"github.com/luxifer/ical"
)
func ProcessIcal(icalURL string) {
FetchIcal(icalURL, time.Now())
ticker := time.NewTicker(time.Hour) // update ical every hour
go func() {
for t := range ticker.C {
FetchIcal(icalURL, t)
}
}()
}
func FetchIcal(icalURL string, tick time.Time) error {
res, err := http.DefaultClient.Get(icalURL)
if err != nil {
log.Warn("Unable to fetch iCal")
return err
}
calendar, err := ical.Parse(res.Body, nil)
if err != nil {
log.Warn("Unable to parse iCal")
return err
}
for _, event := range calendar.Events {
u, err := GetUserByName(event.Summary)
if err != nil {
return err
}
ts := &TimeSlot{
UID: event.UID,
StartDate: event.StartDate,
EndDate: event.EndDate,
User: u,
}
err = UpdateTimeSlot(ts)
if err != nil {
return err
}
}
log.Info("Ical updated")
return nil
}