-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (128 loc) · 3.7 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
const (
UA = "notifyGoogleCalendar (https://github.com/a3510377, 1.0.0) Golang/1.19.4"
DiscordMessageAPIUrl = "https://discord.com/api/channels/%d/messages"
LineMessageAPIUrl = "https://notify-api.line.me/api/notify"
)
func getLocation() *time.Location {
env := os.Getenv("LOC")
loc, err := time.LoadLocation(env)
if err == nil && env != "" {
return loc
}
return time.Local
}
func getNowTime() time.Time { return time.Now().In(getLocation()) }
func main() {
godotenv.Load()
CALENDAR_ID := os.Getenv("CALENDAR_ID")
if len(os.Args) > 1 {
CALENDAR_ID = os.Args[1]
} else if CALENDAR_ID == "" {
CALENDAR_ID = ConfigData.CALENDAR_ID
}
if CALENDAR_ID == "" {
panic("CALENDAR_ID is empty")
}
main := func(checkTimes ...time.Time) {
checkTime := getNowTime().AddDate(0, 0, 1)
if len(checkTimes) > 0 {
checkTime = checkTimes[0]
}
log.Println("check", checkTime.Format("2006-01-02"))
if checkTime.Format("2006-01-02") == GetTmpDate() {
log.Println("Today already send notification, skip check")
return
}
retryCount := 0
for ; retryCount < 3; retryCount++ {
if err := checkAndNotification(CALENDAR_ID, checkTime, nil); err != nil {
time.Sleep(time.Second * 5) // retry after 5 seconds
continue
}
WriteTmpDate(checkTime)
break
}
if retryCount >= 3 {
log.Println("Retry 3 times, skip check")
}
if ConfigData.Options.AdvanceReminder {
if err := checkAndNotification(CALENDAR_ID, checkTime.AddDate(0, 0,
ConfigData.Options.AdvanceReminderDays,
), func(item CalenderV3ApiEventData) bool {
// TODO
return false
}); err != nil {
log.Printf("Advance reminder error %s\n", err)
}
}
}
/* for test */
// for i := 4; i < 30*4; i++ {
// main(time.Now().AddDate(0, 0, i))
// time.Sleep(time.Second)
// }
// return
const specTime = "50 14 * * *"
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
s, _ := parser.Parse(specTime)
fmt.Println(getLocation())
if now := getNowTime(); s.Next(now).In(getLocation()).Day() != now.Day() {
main()
}
c := cron.New(
cron.WithLogger(cron.VerbosePrintfLogger(log.Default())),
cron.WithLocation(getLocation()),
)
c.AddFunc(specTime, func() { main() })
c.Run() // loop start
}
func checkAndNotification(CALENDAR_ID string, nowTime time.Time, callback func(item CalenderV3ApiEventData) bool) error {
resp, err := http.Get(NewCalendarV3ApiRequest(nowTime, CALENDAR_ID).BaseURL().String())
if err != nil {
log.Println("Error getting calendar data: ", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
log.Println("Error getting calendar data: ", resp.Status)
return errors.New("Error getting calendar data: " + resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading calendar data: ", err)
return err
}
data := CalenderV3ApiResponse{}
json.Unmarshal(body, &data)
log.Println("Calendar data: ", data)
notifications := map[string][]CalenderV3ApiEventData{}
for _, item := range data.Items {
if callback == nil {
// check if the item start time is before the current time and the status is confirmed
if !item.IsSameStartDay(nowTime) || item.Status != "confirmed" {
continue
}
} else if !callback(item) {
continue
}
key := item.StartTimeString() + "-" + item.EndTimeString()
notifications[key] = append(notifications[key], item)
}
for _, item := range notifications {
notification(nowTime.Add(-time.Hour*24), item...)
}
return nil
}