-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (196 loc) · 7.62 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
/*
Alerta-de-Campeonatos-WCA - A script which send an e-mail when there's a new WCA competition.
Copyright (C) 2020 Luis Felipe Santos do Nascimento
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/joho/godotenv"
"github.com/luisfelipesdn12/Alerta-de-Campeonatos-WCA/email"
"github.com/luisfelipesdn12/Alerta-de-Campeonatos-WCA/gspread"
"github.com/luisfelipesdn12/Alerta-de-Campeonatos-WCA/resume"
"github.com/luisfelipesdn12/Alerta-de-Campeonatos-WCA/wca"
)
const (
turnLogsOn bool = true
)
var (
startIn = time.Now()
// This log file variable is defined globally
// because it need to be visible for init() and
// main().
logFile, err = os.OpenFile("main.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
)
// init basically check the option turnLogsOn above,
// if this options is true, it set the `logFile` as
// output of logs; if is false, if discard the logs.
func init() {
if !(turnLogsOn) {
log.SetOutput(ioutil.Discard)
} else {
// this check the error of the var declaration
// with `os.OpenFile()`
checkError(err)
// this clear the `logFile` before starting
// to write new logs on it
err = os.Truncate("main.log", 0)
checkError(err)
log.SetOutput(logFile)
}
err := godotenv.Load()
if err != nil {
log.Println("Was not founded an .env file")
}
}
func main() {
// Close the log file at the end of the block
defer logFile.Close()
// Create a struct to allocate information about
// the runtime. It will be exported to a json file,
// but just when the runtime is complete successfully.
resumeInformation := resume.Information{}
// Using the `gspred` local package to do the
// connection with the Google SpreadSheet API,
// fetch the specific spreadsheet of the project
// and return the spreadsheet data.
spreadData, err := gspread.GetSpreadData()
checkError(err)
// Using the `gspred` local package to get the data
// of each recipient from Google SpreadSheets.
recipients, err := gspread.GetRecipientsData(spreadData)
checkError(err)
resumeInformation.UsersChecked = len(recipients)
// Using the `gspred` local package to get the
// credentials data from Google SpreadSheets. It
// will be used to send the emails below.
credentials, err := gspread.GetCredentialsData(spreadData)
checkError(err)
// Create a map to allocate the cities and the
// upcoming competitions number. It will be used
// to check if the verification already exists and
// improve the performance.
cityUpcomingCompetitionsCache := make(map[string]int)
// For each recipient object, get the current number
// of upcoming competitions, compare with the obsolete
// value, and send a an email notifying if the value
// has changed since the last verification.
for _, recipient := range recipients {
// Current date in timestamp format
recipient.CurrentVerificationDate = (time.Now().UnixNano() / int64(time.Millisecond))
// If the value of upcoming competitions to the
// recipients city already exists in the map
// `cityUpcomingCompetitionsCache`, uses this value.
// If not, uses the `wca` local package to access the
// WCA's API and put the result in the above-mentioned
// map, so it can be reused in the next times.
if result, ok := cityUpcomingCompetitionsCache[recipient.City.Value]; ok {
log.Printf(
"The cache value for %v (%v) was reused to %v\n",
recipient.City.Value,
result,
recipient.Name.Value,
)
recipient.CurrentUpcomingCompetitions = result
} else {
// Using the `wca` local package to get the number
// of upcoming competitions in the city of the recipient.
// If an error happen, it is logged and the loop goes
// to the next recipient in the slice.
result, err := wca.UpcomingCopetitions(recipient.City.Value)
resumeInformation.RequestsSended++
if err != nil {
log.Println(err)
continue
}
// If there is no errors, the result value is attributed
// to the propriertie `CurrentUpcomingCompetitions` in the
// `RecipientStruct` and goes to `cityUpcomingCompetitionsCache`
// map, so it can be reused in the next times.
recipient.CurrentUpcomingCompetitions = result
cityUpcomingCompetitionsCache[recipient.City.Value] = result
}
log.Printf(
"%v from %v has %v and now have %v upcoming competitions\n",
recipient.Name.Value,
recipient.City.Value,
recipient.UpcomingCompetitions.Value,
recipient.CurrentUpcomingCompetitions,
)
// Convert the `Upcoming Competitions` of the sheet (a string)
// to an integer value, so it can be an operating. If an error
// happen because the `recipient.UpcomingCompetitions.Value`
// is a non-convertable string (for example, when this is the
// first verification of the recipient and the value is ""),
// the value will be defined as the current upcoming
// competitions number and no emails will be sended. If an
// non-predicate error happen, it is logged and the loop goes
// to the next recipient in the slice.
upcomingCompetitionsInInteger, err := strconv.Atoi(recipient.UpcomingCompetitions.Value)
if err != nil {
if err.Error() == `strconv.Atoi: parsing "`+recipient.UpcomingCompetitions.Value+`": invalid syntax` {
upcomingCompetitionsInInteger = recipient.CurrentUpcomingCompetitions
} else {
log.Println(err)
continue
}
}
// Update the recipient cell in the spreadsheet.
// If an error happen, it is logged and the loop
// goes to the next recipient in the slice.
err = recipient.UpdateUpcomingCompetitions()
if err != nil {
log.Println(err)
continue
}
// Test if the obsolete and the current value are the
// same, if are it finishes the loop and goes to the
// next recipient in the slice, if not it goes through
// and send an email notifying the recipient.
if upcomingCompetitionsInInteger == recipient.CurrentUpcomingCompetitions {
continue
}
// Send the notification email, if an error occur it may
// turn back the upcoming competitions value in the sheet
// because if not the user will lost the notification.
err = email.SendEmail(recipient, credentials)
if err != nil {
log.Printf(
"An error occurred while send an notification email, the upcoming competitions of %v will be turned back to %v\n",
recipient.Name.Value, upcomingCompetitionsInInteger,
)
recipient.CurrentUpcomingCompetitions = upcomingCompetitionsInInteger
updateErr := recipient.UpdateUpcomingCompetitions()
if updateErr != nil {
log.Println("An error occurred while turn turn back in the sheet YOU SHOULD do it manually for now :/")
log.Fatal("The error was:", updateErr)
}
log.Fatal("The error while sending email was:", err)
}
// If the email was successfully sended, incrase in the resume.
resumeInformation.EmailsSended++
}
log.Printf("The cache was this: %v\n", cityUpcomingCompetitionsCache)
log.Printf("THE EXECUTION WAS DONE")
resumeInformation.StartIn = startIn.String()
resumeInformation.RuntimeDuration = time.Since(startIn).String()
resumeInformation.ExportResume("resume.json")
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}