-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
116 lines (99 loc) · 2.81 KB
/
parse.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
package main
import (
"fmt"
"strings"
"time"
"jaytaylor.com/html2text"
)
func RelativelyTime(nowTime time.Time, date time.Time, showDate ...bool) (result string) {
subDuration := date.Sub(nowTime)
ShowDate := len(showDate) > 0 && showDate[0]
// // seconds
// if subDuration.Seconds() < 60 {
// return fmt.Sprintf("%d 秒左右", int(subDuration.Seconds()))
// }
// // minutes
// if subDuration.Hours() < 1 {
// return fmt.Sprintf("%d 分鐘左右", int(subDuration.Minutes()))
// }
// // hours
// if subDuration.Hours() < 24 {
// return fmt.Sprintf("%d 小時左右", int(subDuration.Hours()))
// }
// days
dayHour := time.Hour * 24
if dateDay := date.Day(); dateDay == nowTime.Day() {
result = "今天"
} else if nextDay := nowTime.Add(dayHour); dateDay == nextDay.Day() {
result = "明天"
} else if dateDay == nextDay.Add(dayHour).Day() {
result = "後天"
} else if subDuration.Hours() < 24*7 {
result = fmt.Sprintf("%d 天後", int(subDuration.Hours()/24))
}
if result != "" {
if ShowDate {
result += fmt.Sprintf(" (%s)", date.Format("2006-01-02"))
}
return
}
// weeks
if subDuration.Hours() < 24*7*2 {
result = "下週" + TimeWeekdayString(date.Weekday())
} else {
result = fmt.Sprintf("%d 週後", int(subDuration.Hours()/(24*7)))
}
if ShowDate {
result += fmt.Sprintf(" (%s)", date.Format("2006-01-02"))
}
return
}
func RelativelyTimeSlice(fromTime time.Time, start time.Time, end time.Time, showDate ...bool) string {
arg1 := RelativelyTime(fromTime, start, showDate...)
if start.Format("2006-01-02") == end.Format("2006-01-02") { // as same day
return arg1
}
return fmt.Sprintf("%s ~ %s", arg1, RelativelyTime(fromTime, end, showDate...))
}
var longDayNames = []string{"日", "一", "二", "三", "四", "五", "六"}
func TimeWeekdayString(d time.Weekday) string {
if time.Sunday <= d && d <= time.Saturday {
return longDayNames[d]
}
return d.String()
}
func notification(fromTime time.Time, data ...CalenderV3ApiEventData) {
content := ""
for _, item := range data {
description := ""
if item.Description != "" {
text, err := html2text.FromString(item.Description)
if err != nil {
text = item.Description
}
data := strings.Split(text, "\n")
description += " >>> \n"
for _, item := range data {
description += " >> " + item + "\n"
}
}
endTime := item.EndTime()
if item.Start.Date != "" {
endTime = endTime.Add(-time.Hour * 24)
}
content += fmt.Sprintf("%s是 %s 的日子 %s\n", RelativelyTimeSlice(
fromTime, item.StartTime(),
endTime,
), item.Summary, description)
}
content = strings.TrimSuffix(content, "\n") // remove trailing newline
fmt.Println("send log: ", content)
// line notify
if ConfigData.Line.Enable {
NotifyLine(content)
}
// discord
if ConfigData.Discord.Enable {
NotifyDiscord(content)
}
}