-
Notifications
You must be signed in to change notification settings - Fork 1
/
event-schedule.go
73 lines (59 loc) · 1.86 KB
/
event-schedule.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
package main
import (
"bytes"
"encoding/csv"
"io/ioutil"
"log"
"net/http"
"time"
// "os"
)
var webClient = http.Client{}
var eventCacheTime time.Time
var eventsCache EventSchedule
var timesCache []time.Time
var roomOrderCache []string
func readEventCache(sheetUrl string, cacheTimeLength int, rowLength time.Duration, timezone string) (EventSchedule, []time.Time, []string, error) {
if time.Now().After(eventCacheTime) {
err := updateEventCache(sheetUrl, cacheTimeLength, rowLength, timezone)
if err != nil {
return nil, nil, nil, err
}
}
return eventsCache, timesCache, roomOrderCache, nil
}
func updateEventCache(sheetUrl string, cacheTimeLength int, rowLength time.Duration, timezone string) error {
rawEvents, err := fetchRawEvents(sheetUrl)
if err != nil {
log.Println("Unable to update event cache due to an error during fetchRawEvents: " + err.Error())
return err
}
reader := csv.NewReader(bytes.NewReader(rawEvents))
newEvents, newTimes, newRoomOrder, err := parseSchedule(reader, rowLength, timezone)
if err != nil {
log.Println("Unable to update event cache due to an error during parseSchedule: " + err.Error())
return err
} else {
log.Printf("I got %d room\n", len(newEvents))
eventsCache = newEvents
timesCache = newTimes
roomOrderCache = newRoomOrder
eventCacheTime = time.Now().Add(time.Duration(cacheTimeLength) * time.Second)
}
return nil
}
func fetchRawEvents(sheetUrl string) ([]byte, error) {
log.Printf("Going to call out to the following sheets URL: %s\n", sheetUrl)
resp, err := webClient.Get(sheetUrl)
if err != nil {
log.Printf("Failed to fetch Events Schedule sheet with error: %s\n", err)
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read Events Schedule sheet with error: %s\n", err)
return nil, err
}
return body, nil
}