-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
320 lines (281 loc) · 8.2 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"crypto/sha1"
"embed"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"regexp"
"sort"
"time"
"github.com/apognu/gocal"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
"github.com/godbus/dbus/v5"
)
const CACHE_FILE = "/tmp/i3-ics-cache"
//go:embed beep.wav
var embedded embed.FS
func main() {
icsURL := flag.String("ics-url", "", "The ICS-URL that will be used to retreive the events")
output := flag.String("output", "", "Defines what the output will be. Valid values are 'current' (the current event), 'current-link' (first link in current event location and description), 'next' (the next event), 'next-link' (first link in next event location and description), 'tail' (current and next event in stdout) and 'agenda' (list of all events for today)")
calCacheDuration := flag.Duration("cal-cache-duration", time.Minute*5, "Defines how long the ICS will be cached. Default is 5 Minutes")
flag.Parse()
if icsURL == nil || *icsURL == "" {
panic("ics-url-parameter is required")
}
if output == nil || *output == "" {
panic("output-parameter is required")
}
e, err := getTodaysEvents(*icsURL, *calCacheDuration)
if err != nil {
panic(err)
}
f, err := embedded.Open("beep.wav")
if err != nil {
panic(err)
}
streamer, format, err := wav.Decode(f)
if err != nil {
panic(err)
}
defer streamer.Close()
err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
if err != nil {
panic(err)
}
switch *output {
case "current":
evt := getCurrentEvent(e)
if evt != nil {
renderEventAsString(*evt)
}
return
case "current-link":
evt := getCurrentEvent(e)
if evt != nil {
renderEventLinkAsString(*evt)
}
return
case "next":
evt := getNextEvent(e)
if evt != nil {
renderEventAsString(*evt)
}
return
case "next-link":
evt := getNextEvent(e)
if evt != nil {
renderEventLinkAsString(*evt)
}
return
case "tail":
// tail will loop forever
var currentEvent *gocal.Event
var nextEvent *gocal.Event
var nextEventAnnounced1 = false
var nextEventAnnounced2 = false
for {
time.Sleep(time.Second)
ce := getCurrentEvent(e)
ne := getNextEvent(e)
if eventsEqual(ce, currentEvent) && eventsEqual(ne, nextEvent) {
if ne != nil {
if !nextEventAnnounced2 && ne.Start.Before(time.Now().Add(5*time.Minute)) {
nextEventAnnounced1 = true
nextEventAnnounced2 = true
speaker.Play(streamer)
_ = sendNotification(ne.Summary, "Upcoming event in 5 Minutes")
}
if !nextEventAnnounced1 && ne.Start.Before(time.Now().Add(15*time.Minute)) {
nextEventAnnounced1 = true
speaker.Play(streamer)
_ = sendNotification(ne.Summary, "Upcoming event in 15 Minutes")
}
}
continue
}
speaker.Play(streamer)
nextEventAnnounced1 = false
nextEventAnnounced2 = false
currentEvent = ce
nextEvent = ne
if currentEvent == nil && nextEvent == nil {
fmt.Println("No upcoming Events")
continue
}
if currentEvent != nil && nextEvent == nil {
fmt.Printf("Current: [%s - %s] %s\n", getTimeString(currentEvent.Start), getTimeString(currentEvent.End), fixedSizeString(currentEvent.Summary, 40, "..."))
continue
}
if currentEvent == nil && nextEvent != nil {
fmt.Printf("Upcoming: [%s - %s] %.40s\n", getTimeString(nextEvent.Start), getTimeString(nextEvent.End), fixedSizeString(nextEvent.Summary, 40, "..."))
continue
}
fmt.Printf("[%s - %s] %s > [%s - %s] %s\n", getTimeString(currentEvent.Start), getTimeString(currentEvent.End), fixedSizeString(currentEvent.Summary, 30, "..."), getTimeString(nextEvent.Start), getTimeString(nextEvent.End), fixedSizeString(nextEvent.Summary, 30, "..."))
}
case "agenda":
var currentEvent *gocal.Event
for _, e := range e {
if currentEvent == nil && e.Start.Before(time.Now()) && e.End.After(time.Now()) {
currentEvent = &e
renderEventAsListEntry(e, true)
} else {
renderEventAsListEntry(e, false)
}
}
}
}
func sendNotification(title string, body string) error {
con, err := dbus.SessionBus()
if err != nil {
return fmt.Errorf("Failed to open dbus-session: %w", err)
}
var d = make(chan *dbus.Call, 1)
var o = con.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
var id uint32
o.GoWithContext(context.Background(), "org.freedesktop.Notifications.Notify", 0, d, "i3-ics-Agenda", uint32(0), "", title, body, []string{}, map[string]interface{}{}, int32(0))
err = (<-d).Store(&id)
if err != nil {
return fmt.Errorf("Failed to create notification on dbus: %w", err)
}
return nil
}
func eventsEqual(e1 *gocal.Event, e2 *gocal.Event) bool {
if e1 == nil && e2 == nil {
return true
}
if e1 != nil && e2 != nil {
return e1.Uid == e2.Uid
}
return false
}
func getCurrentEvent(events []gocal.Event) *gocal.Event {
for _, e := range events {
if e.Start.Before(time.Now()) && e.End.After(time.Now()) {
return &e
}
}
return nil
}
func getNextEvent(events []gocal.Event) *gocal.Event {
for _, e := range events {
if e.Start.After(time.Now()) {
return &e
}
}
return nil
}
func loadEventsFromCache(url string, cacheDuration time.Duration) (result []gocal.Event, err error) {
filename := generateCacheFilename(url)
s, err := os.Stat(filename)
if err != nil {
// cannot access file
return nil, fmt.Errorf("cannot stat file: %w", err)
}
if s.ModTime().Before(time.Now().Add(-cacheDuration)) {
// file is too old
return nil, fmt.Errorf("cache-file is too old")
}
f, err := os.Open(filename)
if err != nil {
// cannot read file
return nil, fmt.Errorf("cannot read file: %w", err)
}
defer f.Close()
err = json.NewDecoder(f).Decode(&result)
if err != nil {
// cannot decode file
return nil, fmt.Errorf("cannot decode file-content: %w", err)
}
return result, nil
}
func loadEventsFromUrl(url string) ([]gocal.Event, error) {
r, err := http.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
y, m, d := time.Now().Date()
start := time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())
end := start.AddDate(0, 0, 1)
gocal.SetTZMapper(func(s string) (*time.Location, error) {
return time.Now().Location(), nil
})
c := gocal.NewParser(r.Body)
c.Start = &start
c.End = &end
err = c.Parse()
if err != nil {
return nil, err
}
sort.Slice(c.Events, func(i int, j int) bool {
return c.Events[i].Start.Before(*c.Events[j].Start)
})
return c.Events, nil
}
func cacheEvents(e []gocal.Event, url string) error {
filename := generateCacheFilename(url)
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return fmt.Errorf("cannot open or create file: %w", err)
}
defer f.Close()
err = json.NewEncoder(f).Encode(e)
if err != nil {
return fmt.Errorf("cannot encode events: %w", err)
}
return nil
}
func generateCacheFilename(url string) string {
urlHash := sha1.Sum([]byte(url))
return CACHE_FILE + base64.StdEncoding.EncodeToString(urlHash[:])
}
func getTodaysEvents(url string, cacheDuration time.Duration) ([]gocal.Event, error) {
e, err := loadEventsFromCache(url, cacheDuration)
if err == nil {
return e, nil
}
e, err = loadEventsFromUrl(url)
if err != nil {
return nil, fmt.Errorf("could not retrieve events from url: %w", err)
}
err = cacheEvents(e, url)
if err != nil {
return nil, fmt.Errorf("cannot cache events: %w", err)
}
return e, nil
}
func renderEventAsString(e gocal.Event) {
fmt.Printf("[%s - %s] %s\n", getTimeString(e.Start), getTimeString(e.End), e.Summary)
}
func renderEventLinkAsString(e gocal.Event) {
search := e.Location + "\n" + e.Description
r, err := regexp.Compile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`)
if err != nil {
panic(err.Error())
}
s := r.FindString(search)
fmt.Print(s)
}
func renderEventAsListEntry(e gocal.Event, current bool) {
currentVal := "FALSE"
if current {
currentVal = "TRUE"
}
fmt.Printf("%s '%s' '%s' '%s' ", currentVal, getTimeString(e.Start), getTimeString(e.End), e.Summary)
}
func getTimeString(t *time.Time) string {
h, m, _ := t.Clock()
return fmt.Sprintf("%02d:%02d", h, m)
}
func fixedSizeString(s string, maxLength int, ellipsesSymbol string) string {
if len(s) > maxLength {
return s[0:maxLength] + ellipsesSymbol
}
return s
}