-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (178 loc) · 3.97 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/smtp"
"os"
"strconv"
"strings"
"time"
)
var (
auth smtp.Auth
logFilePath string = "./logs.txt"
configPath string = "config.json"
config map[string]string
fullUrl string
)
type Cards []map[string]interface{}
type Email struct {
from string
to []string
body string
subject string
}
type templateData struct {
CardName string
CardUrl string
CardDue string
}
func main() {
configFile, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(configFile, &config)
fullUrl = config["baseUrl"] + config["apiKey"] + config["secret"]
auth = smtp.PlainAuth("", "trellodev2020", config["password"], "smtp.gmail.com")
repeat(24*time.Second, getCards)
}
func NewEmail(body string) *Email {
return &Email{
from: "[email protected]",
to: []string{"[email protected]"},
subject: "Rappel tâches trello !",
body: body,
}
}
func getCards() {
response, err := http.Get(fullUrl)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, readError := ioutil.ReadAll(response.Body)
if readError != nil {
log.Fatal(readError)
}
var cards Cards
jsonError := json.Unmarshal(body, &cards)
if jsonError != nil {
log.Fatal(jsonError)
}
manageCards(cards)
}
func manageCards(cards Cards) {
loc, _ := time.LoadLocation("Europe/Paris")
now := time.Now().In(loc)
for _, card := range cards {
if card["dueComplete"] == true {
return
}
due, err := time.Parse(time.RFC3339, card["due"].(string))
if err != nil {
log.Fatal(err)
}
willExpireSoon := due.Sub(now).Hours() <= 24
if willExpireSoon {
var data templateData
data.CardDue = FormatTimeRemaining(now, due)
data.CardUrl = card["shortUrl"].(string)
data.CardName = card["name"].(string)
html := ParseTemplate("template.html", data)
email := NewEmail(html)
_, sentError := email.sendEmail()
if sentError != nil {
log := fmt.Sprintf("Error sending emails : %v", sentError)
cslog([]byte(log))
} else {
log := fmt.Sprintf("%v Emails sent : %v\n", now, len(email.to))
cslog([]byte(log))
}
}
}
}
func (e *Email) sendEmail() (bool, error) {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := "Subject: " + e.subject + "\n"
to := "To: " + strings.Join(e.to, ",") + "\n"
msg := []byte(to + subject + mime + "\n" + e.body)
addr := "smtp.gmail.com:587"
if err := smtp.SendMail(addr, auth, e.from, e.to, msg); err != nil {
return false, err
}
return true, nil
}
func ParseTemplate(file string, data templateData) string {
t, err := template.ParseFiles(file)
if err != nil {
log.Fatal(err)
}
buffed := new(bytes.Buffer)
if err = t.Execute(buffed, data); err != nil {
log.Fatal(err)
}
return buffed.String()
}
func FormatTimeRemaining(a, b time.Time) string {
if a.Location() != b.Location() {
b = b.In(a.Location())
}
if a.After(b) {
a, b = b, a
}
y1, M1, d1 := a.Date()
y2, M2, d2 := b.Date()
h1, m1, s1 := a.Clock()
h2, m2, s2 := b.Clock()
year := int(y2 - y1)
month := int(M2 - M1)
day := int(d2 - d1)
hour := int(h2 - h1)
min := int(m2 - m1)
sec := int(s2 - s1)
if sec < 0 {
sec += 60
min--
}
if min < 0 {
min += 60
hour--
}
if hour < 0 {
hour += 24
day--
}
if day < 0 {
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
day += 32 - t.Day()
month--
}
if month < 0 {
month += 12
year--
}
return strconv.Itoa(day) + " jours, " + strconv.Itoa(hour) + " heures, " + strconv.Itoa(min) + " minutes et " + strconv.Itoa(sec) + " secondes."
}
func repeat(duration time.Duration, function func()) {
for range time.Tick(duration) {
function()
}
}
func cslog(data []byte) {
f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}