-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsheets.go
284 lines (237 loc) · 7.94 KB
/
gsheets.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
package workers
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
storage "cloud.google.com/go/storage"
"github.com/dahlke/eklhad/web/constants"
"github.com/dahlke/eklhad/web/geo"
"github.com/dahlke/eklhad/web/structs"
log "github.com/sirupsen/logrus"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func writeLocationsToGCS(locations []structs.EklhadLocation) {
if len(locations) > 0 {
ctx := context.Background()
gcsClient, err := storage.NewClient(ctx)
if err != nil {
log.Error(err)
}
bkt := gcsClient.Bucket(constants.GCSPublicBucketName)
wc := bkt.Object(constants.LocationDataGCSFilePath).NewWriter(ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-app": "eklhad-web",
"x-goog-meta-type": "data",
"x-goog-meta-dataset": "intagram",
}
fileContents, _ := json.MarshalIndent(locations, "", " ")
if _, err := wc.Write([]byte(fileContents)); err != nil {
log.Error("Unable to write location data to GCS.")
return
}
if err := wc.Close(); err != nil {
log.Error("Unable to close writer for GCS while writing location data.")
return
}
log.Info("Location data successfully written to GCS.")
} else {
log.Error("Something went wrong, the data set was size zero, so no location data was overwritten in GCS.")
}
}
func writeLinksToGCS(links []structs.EklhadLink) {
if len(links) > 0 {
ctx := context.Background()
gcsClient, err := storage.NewClient(ctx)
if err != nil {
log.Error(err)
}
bkt := gcsClient.Bucket(constants.GCSPublicBucketName)
wc := bkt.Object(constants.LinkDataGCSFilePath).NewWriter(ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-app": "eklhad-web",
"x-goog-meta-type": "data",
"x-goog-meta-dataset": "links",
}
fileContents, _ := json.MarshalIndent(links, "", " ")
if _, err := wc.Write([]byte(fileContents)); err != nil {
log.Error("Unable to write link data to GCS.")
return
}
if err := wc.Close(); err != nil {
log.Error("Unable to close writer for GCS while writing link data.")
return
}
log.Info("Link data successfully written to GCS.")
} else {
log.Error("Something went wrong, the data set was size zero, so no link data was overwritten in GCS.")
}
}
func writeBlogsToGCS(blogs []structs.EklhadBlog) {
ctx := context.Background()
gcsClient, err := storage.NewClient(ctx)
if err != nil {
log.Error(err)
}
bkt := gcsClient.Bucket(constants.GCSPublicBucketName)
wc := bkt.Object(constants.BlogDataGCSFilePath).NewWriter(ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-app": "eklhad-web",
"x-goog-meta-type": "data",
"x-goog-meta-dataset": "blogs",
}
fileContents, _ := json.MarshalIndent(blogs, "", " ")
if _, err := wc.Write([]byte(fileContents)); err != nil {
log.Error("Unable to write blog data to GCS.")
return
}
if err := wc.Close(); err != nil {
log.Error("Unable to close writer for GCS while writing blog data.")
return
}
}
// GetDataFromGSheets gets all the link and location activity logged in a specific
// format in GSheets, and writes it to the file system for usage in the frontend.
func GetDataFromGSheets(spreadSheetID string) {
googleAPIKey := os.Getenv("GOOGLE_API_KEY")
ctx := context.Background()
sheetsService, err := sheets.NewService(ctx, option.WithAPIKey(googleAPIKey))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
locationsReadRange := "locations!A2:F"
// NOTE: https://pkg.go.dev/google.golang.org/api@v0.64.0/sheets/v4?utm_source=gopls#SpreadsheetsValuesService.Get
locationsResp, err := sheetsService.Spreadsheets.Values.Get(spreadSheetID, locationsReadRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}
if len(locationsResp.Values) == 0 {
log.Info("No data found for locations.")
} else {
var eklhadLocations []structs.EklhadLocation
for i, row := range locationsResp.Values {
locationID := fmt.Sprint("location-", i)
locationCity := row[0].(string)
locationStateProviceRegion := row[1].(string)
locationCountry := row[2].(string)
locationCurrent := row[3].(string)
locationLayover := row[4].(string)
locationHome := row[5].(string)
locationConcat := fmt.Sprintf("%s, %s, %s", locationCity, locationStateProviceRegion, locationCountry)
log.Info("Processing location ", locationConcat)
// NOTE: Geocoding each location makes it this loop take longer than you would think.
lat, lng := geo.GeocodeLocation(locationConcat)
current := false
if locationCurrent == "TRUE" {
current = true
}
layover := false
if locationLayover == "TRUE" {
layover = true
}
home := false
if locationHome == "TRUE" {
home = true
}
eklhadLocation := structs.EklhadLocation{
ID: locationID,
City: locationCity,
StateProvinceRegion: locationStateProviceRegion,
Country: locationCountry,
Current: current,
Layover: layover,
Home: home,
Lat: lat,
Lng: lng,
}
eklhadLocations = append(eklhadLocations, eklhadLocation)
}
writeLocationsToGCS(eklhadLocations)
}
blogsReadRange := "blogs!A2:G"
// NOTE: https://pkg.go.dev/google.golang.org/api@v0.64.0/sheets/v4?utm_source=gopls#SpreadsheetsValuesService.Get
blogsResp, err := sheetsService.Spreadsheets.Values.Get(spreadSheetID, blogsReadRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}
if len(blogsResp.Values) == 0 {
log.Info("No data found for blogs.")
} else {
var eklhadBlogs []structs.EklhadBlog
for i, row := range blogsResp.Values {
blogID := fmt.Sprint("blog-", i)
blogName := row[0].(string)
blogDate := row[1].(string)
blogURL := row[2].(string)
blogMediumURL := row[3].(string)
blogOriginalURL := row[4].(string)
blogGistURL := row[5].(string)
blogPath := row[6].(string)
log.Info("Processing blog ", blogName)
timestamp, err := time.Parse(constants.GSheetsInputDateFmt, blogDate)
if err != nil {
log.Error(err)
}
eklhadBlog := structs.EklhadBlog{
ID: blogID,
Name: blogName,
Timestamp: timestamp.Unix(),
URL: blogURL,
MediumURL: blogMediumURL,
OriginalURL: blogOriginalURL,
GistURL: blogGistURL,
Path: blogPath,
}
eklhadBlogs = append(eklhadBlogs, eklhadBlog)
}
writeBlogsToGCS(eklhadBlogs)
}
linksReadRange := "links!A2:E"
// NOTE: https://www.any-api.com/googleapis_com/sheets/docs/spreadsheets/sheets_spreadsheets_values_get
linksResp, err := sheetsService.Spreadsheets.Values.Get(spreadSheetID, linksReadRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}
if len(linksResp.Values) == 0 {
log.Info("No data found for links.")
} else {
var eklhadLinks []structs.EklhadLink
for i, row := range linksResp.Values {
linkID := fmt.Sprint("link-", i)
linkName := row[0].(string)
linkDate := row[1].(string)
linkType := row[2].(string)
linkURL := row[3].(string)
log.Info("Processing link ", linkName)
timestamp, err := time.Parse(constants.GSheetsInputDateFmt, linkDate)
if err != nil {
log.Error(err)
}
eklhadLink := structs.EklhadLink{
ID: linkID,
Name: linkName,
Timestamp: timestamp.Unix(),
Type: linkType,
URL: linkURL,
}
eklhadLinks = append(eklhadLinks, eklhadLink)
}
writeLinksToGCS(eklhadLinks)
}
}
// ScheduleGSheetsWork schedules GetDataFromGSheets at an interval
func ScheduleGSheetsWork(numSleepMins int, spreadSheetID string) {
iterationNumber := 0
for {
log.Info(fmt.Sprintf("Starting GSheets worker scheduled task #%d...", iterationNumber))
GetDataFromGSheets(spreadSheetID)
iterationNumber++
log.Info(fmt.Sprintf("GSheets worker sleeping for %d minute(s)...", numSleepMins))
time.Sleep(time.Duration(numSleepMins) * time.Minute)
}
}