/
main.go
294 lines (250 loc) · 8.18 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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"path"
"time"
"github.com/gocarina/gocsv"
"github.com/pkg/errors"
csvtypes "gomodules.xyz/encoding/csv/types"
_ "gomodules.xyz/gdrive-utils"
gdrive "gomodules.xyz/gdrive-utils"
"google.golang.org/api/docs/v1"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func handleError(err error, message string) {
if message == "" {
message = "Error making API call"
}
if err != nil {
log.Fatalf(message+": %v", err.Error())
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
//func main() {
// now := time.Now().UTC()
// fmt.Println(now.Format(time.RFC3339))
//}
func main() {
client, err := gdrive.DefaultClient(".")
handleError(err, "Error creating YouTube client")
svcSheets, err := sheets.NewService(context.TODO(), option.WithHTTPClient(client))
handleError(err, "Error creating Sheets client")
svcDrive, err := drive.NewService(context.TODO(), option.WithHTTPClient(client))
handleError(err, "Error creating Drive client")
svcDocs, err := docs.NewService(context.TODO(), option.WithHTTPClient(client))
handleError(err, "Error creating Docs client")
email := "tamal.saha@gmail.com"
configDocId := "1KB_Efi9jQcJ0_tCRF4fSLc6TR7QxaBKg05cKXAwbC9E"
//qaTemplateDocId := "16Ff6Lum3F6IeyAEy3P5Xy7R8CITIZRjdwnsRwBg9rD4"
//now := time.Now()
//cfg := QuestionConfig{
// ConfigType: ConfigTypeQuestion,
// QuestionTemplateDocId: qaTemplateDocId,
// StartDate: Date{now},
// EndDate: Date{now.Add(5 * 24 * time.Hour)}, // 3 days
// Duration: Duration{90 * time.Minute}, // 60 mins
//}
//err = SaveConfig(svcSheets, configDocId, cfg)
//handleError(err, "failed to save config")
// cfg, err := LoadConfig(svcSheets, configDocId)
// handleError(err, "failed to save config")
// printJSON(cfg)
PostPage(svcDrive, svcDocs, svcSheets, configDocId, email)
}
func main_() {
client, err := gdrive.DefaultClient(".")
handleError(err, "Error creating YouTube client")
svcDrive, err := drive.NewService(context.TODO(), option.WithHTTPClient(client))
handleError(err, "Error creating Drive client")
svcDocs, err := docs.NewService(context.TODO(), option.WithHTTPClient(client))
handleError(err, "Error creating Docs client")
//_, err = FindParentFolderId(service, fileId)
//handleError(err, "Error finding parent folder id")
email := "tamal.saha@gmail.com"
folderId, err := gdrive.GetFolderId(svcDrive, fileId, "candidates/"+email)
handleError(err, "Error finding parent folder id")
fmt.Println("parent folder id", folderId)
now := time.Now()
docId, err := gdrive.CopyDoc(svcDrive, svcDocs, fileId, folderId, "Account Interview", map[string]string{
"{{email}}": email,
"{{start-time}}": now.Format(time.RFC3339),
"{{end-time}}": now.Add(1 * time.Hour).Format(time.RFC3339),
})
fmt.Println("user file id", docId)
// AddPermission(svcDrive, docId, email, "writer")
err = gdrive.RevokePermission(svcDrive, docId, email)
handleError(err, "RevokePermission")
}
const fileId = "16Ff6Lum3F6IeyAEy3P5Xy7R8CITIZRjdwnsRwBg9rD4"
func printJSON(v interface{}) {
data, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(data))
}
type ConfigType string
const (
ConfigTypeQuestion ConfigType = "QuestionConfig"
)
type QuestionConfig struct {
ConfigType ConfigType `json:"configType" csv:"Config Type"`
QuestionTemplateDocId string `json:"questionTemplateDocId" csv:"Question Template Doc Id"`
StartDate csvtypes.Date `json:"startDate" csv:"Start Date"`
EndDate csvtypes.Date `json:"endDate" csv:"End Date"`
Duration csvtypes.Duration `json:"duration" csv:"Duration"`
}
const (
ProjectConfigSheet = "config"
ProjectTestSheet = "test"
)
func SaveConfig(svcSheets *sheets.Service, configDocId string, cfg QuestionConfig) error {
w := gdrive.NewRowWriter(svcSheets, configDocId, ProjectConfigSheet, &gdrive.Predicate{
Header: "Config Type",
By: func(column []interface{}) (int, error) {
for i, v := range column {
if v.(string) == string(cfg.ConfigType) {
return i, nil
}
}
return -1, io.EOF
},
})
data := []*QuestionConfig{
&cfg,
}
return gocsv.MarshalCSV(data, w)
}
func LoadConfig(svcSheets *sheets.Service, configDocId string) (*QuestionConfig, error) {
r, err := gdrive.NewRowReader(svcSheets, configDocId, ProjectConfigSheet, &gdrive.Predicate{
Header: "Config Type",
By: func(column []interface{}) (int, error) {
for i, v := range column {
if v.(string) == string(ConfigTypeQuestion) {
return i, nil
}
}
return -1, io.EOF
},
})
if err == io.EOF {
return nil, errors.New("Question Config not found!")
} else if err != nil {
return nil, err
}
configs := []*QuestionConfig{}
if err := gocsv.UnmarshalCSV(r, &configs); err != nil { // Load clients from file
return nil, err
}
return configs[0], nil
}
type TestAnswer struct {
Email string `json:"email" csv:"Email"`
DocId string `json:"docId" csv:"Doc Id"`
StartDate csvtypes.Timestamp `json:"startDate" csv:"Start Date"`
EndDate csvtypes.Timestamp `json:"endDate" csv:"End Date"`
}
func SaveTestAnswer(svcSheets *sheets.Service, configDocId string, ans TestAnswer) error {
w := gdrive.NewRowWriter(svcSheets, configDocId, ProjectTestSheet, &gdrive.Predicate{
Header: "Email",
By: func(column []interface{}) (int, error) {
for i, v := range column {
if v.(string) == ans.Email {
return i, nil
}
}
return -1, io.EOF
},
})
data := []*TestAnswer{
&ans,
}
return gocsv.MarshalCSV(data, w)
}
func LoadTestAnswer(svcSheets *sheets.Service, configDocId, email string) (*TestAnswer, error) {
r, err := gdrive.NewRowReader(svcSheets, configDocId, ProjectTestSheet, &gdrive.Predicate{
Header: "Email",
By: func(column []interface{}) (int, error) {
for i, v := range column {
if v.(string) == email {
return i, nil
}
}
return -1, io.EOF
},
})
//if err == io.EOF {
// return nil, errors.Errorf("%s has not started the test yet!", email)
//} else
if err != nil {
return nil, err
}
answers := []*TestAnswer{}
if err := gocsv.UnmarshalCSV(r, &answers); err != nil {
return nil, err
}
return answers[0], nil
}
func GetTestPage(svcSheets *sheets.Service, configDocId string) {
cfg, err := LoadConfig(svcSheets, configDocId)
if err != nil {
panic(err)
}
if time.Now().After(cfg.EndDate.Time) {
panic("Time passed for this test")
}
fmt.Printf("%s left to take the test!", time.Until(cfg.EndDate.Time))
}
func PostPage(svcDrive *drive.Service, svcDocs *docs.Service, svcSheets *sheets.Service, configDocId, email string) {
// already submitted
// started and x min left to finish the test, redirect, embed
// did not start, copy file, stat clock
now := time.Now()
cfg, err := LoadConfig(svcSheets, configDocId)
if err != nil {
panic(err)
}
if now.After(cfg.EndDate.Time) {
panic("Time passed for this test")
}
ans, err := LoadTestAnswer(svcSheets, configDocId, email)
if err != nil && err != io.EOF {
panic(err) // some error
}
if err == nil {
if now.After(ans.EndDate.Time) {
panic(fmt.Sprintf("%s passed after test has ended!", time.Since(ans.EndDate.Time)))
}
} else {
ans = &TestAnswer{
Email: email,
DocId: "",
StartDate: csvtypes.Timestamp{now},
EndDate: csvtypes.Timestamp{now.Add(cfg.Duration.Duration)},
}
folderId, err := gdrive.GetFolderId(svcDrive, configDocId, path.Join("candidates", email))
if err != nil {
panic(err)
}
docName := fmt.Sprintf("%s - Test %s", email, ans.StartDate.Format("2006-01-02"))
docId, err := gdrive.CopyDoc(
svcDrive, svcDocs, cfg.QuestionTemplateDocId, folderId, docName, map[string]string{
"{{email}}": email,
"{{start-time}}": ans.StartDate.Format(time.RFC3339),
"{{end-time}}": ans.EndDate.Format(time.RFC3339),
})
if err != nil {
panic(err)
}
ans.DocId = docId
err = SaveTestAnswer(svcSheets, configDocId, *ans)
if err != nil {
panic(err)
}
}
fmt.Printf("%s left to take the test!\n", time.Until(ans.EndDate.Time))
fmt.Printf("https://docs.google.com/document/d/%s/edit\n", ans.DocId)
}