-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.go
82 lines (71 loc) · 1.81 KB
/
sheet.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
package config
import (
"context"
"github.com/margostino/just/common"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
"log"
"os"
)
const (
LastDay = "r86400"
LastWeek = "r604800"
OnsiteMode = "1"
RemoteMode = "2"
HybridMode = "3"
)
func GetConfig() map[string]string {
config := make(map[string]string)
ctx := context.Background()
api, err := sheets.NewService(ctx, option.WithAPIKey(os.Getenv("GSHEET_API_KEY")))
if !common.IsError(err, "when creating new Google API Service") {
spreadsheetId := os.Getenv("SPREADSHEET_ID")
readRange := os.Getenv("SPREADSHEET_RANGE")
resp, err := api.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
if !common.IsError(err, "unable to retrieve data from sheet") && len(resp.Values) > 0 {
for _, row := range resp.Values {
if len(row) == 7 {
config["url"] = row[0].(string)
config["paginationFactor"] = row[1].(string)
config["keywords"] = row[2].(string)
config["location"] = row[3].(string)
config["calls"] = row[5].(string)
timePeriod := row[4].(string)
mode := row[6].(string)
config["timePeriod"] = GetTimePeriodParam(timePeriod)
config["mode"] = GetModeParam(mode)
} else {
log.Printf("Configuration sheet for Feed Urls is not valid. It must have 3 columns. It has %d\n", len(row))
}
}
}
}
return config
}
func GetTimePeriodParam(timePeriod string) string {
var timePeriodParam string
switch timePeriod {
case "1w":
timePeriodParam = LastWeek
break
case "1d":
timePeriodParam = LastDay
break
}
return timePeriodParam
}
func GetModeParam(mode string) string {
var modeParam string
switch mode {
case "onsite":
modeParam = OnsiteMode
break
case "remote":
modeParam = RemoteMode
break
case "hybrid":
modeParam = HybridMode
break
}
return modeParam
}