This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (132 loc) · 4.42 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
package main
import (
"bufio"
"io"
"log"
"os"
"strconv"
"strings"
"time"
)
const icsDateLayout = "20060102T150405Z"
const triggerExpression = "-P0DT6H0M0S" // 1 day before at 6 pm (Google format)
const outputFilename = "enhanced.ics"
func main() {
filename := getFilenameFromArgs()
log.Println("processing ICS file")
icsFileLines, eventCount := parseEvents(filename)
log.Printf("processed %v events", eventCount)
writeOutputFile(icsFileLines)
log.Printf("finished, written %v events to enhanced ICS file", eventCount+1)
}
func parseEvents(filename string) ([]string, int) {
readFile, err := os.Open(filename)
if err != nil {
log.Fatalf("failed to open file: %s", err)
}
defer readFile.Close()
var icsFileLines []string
var lastEventDate time.Time
var eventCount int
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
if isEventStartLine(line) {
eventCount += 1
} else if isStartDateLine(line) {
lastEventDate = parseEventDate(line)
} else if isEventEndLine(line) {
icsFileLines = appendTrigger(icsFileLines)
} else if isCalendarEndLine(line) {
icsFileLines = appendDownloadNewCalendarFileEvent(icsFileLines, lastEventDate)
}
icsFileLines = append(icsFileLines, line)
}
return icsFileLines, eventCount
}
func writeOutputFile(icsFileLines []string) {
file, err := os.Create(outputFilename)
if err != nil {
log.Fatalf("failed to create file: %s", err)
}
defer file.Close()
joinedString := strings.Join(icsFileLines, "\n")
_, err = io.WriteString(file, joinedString)
if err != nil {
log.Fatalf("failed to write file content: %s", err)
}
err = file.Sync()
if err != nil {
log.Fatalf("failed to write enhanced file: %s", err)
}
}
func appendTrigger(icsFileLines []string) []string {
icsFileLines = append(icsFileLines, "BEGIN:VALARM")
icsFileLines = append(icsFileLines, "ACTION:DISPLAY")
icsFileLines = append(icsFileLines, "DESCRIPTION:This is an event reminder")
icsFileLines = append(icsFileLines, "TRIGGER:"+triggerExpression)
icsFileLines = append(icsFileLines, "END:VALARM")
return icsFileLines
}
func appendDownloadNewCalendarFileEvent(icsFileLines []string, eventDate time.Time) []string {
log.Println("adding reminder for downloading new calendar file")
startDate := eventDate.Add(time.Hour * -(12 + 24))
endDate := startDate.Add(time.Hour)
startDateString := startDate.Format(icsDateLayout)
endDateString := endDate.Format(icsDateLayout)
timestamp := time.Now().Format(icsDateLayout)
uid := strconv.FormatInt(startDate.Unix(), 10)
icsFileLines = append(icsFileLines, "BEGIN:VEVENT")
icsFileLines = append(icsFileLines, "UID:"+uid)
icsFileLines = append(icsFileLines, "DTSTART:"+startDateString)
icsFileLines = append(icsFileLines, "SEQUENCE:0")
icsFileLines = append(icsFileLines, "TRANSP:OPAQUE")
icsFileLines = append(icsFileLines, "DTEND:"+endDateString)
icsFileLines = append(icsFileLines, "SUMMARY:Neuen Abfallkalender herunterladen")
icsFileLines = append(icsFileLines, "CLASS:PUBLIC")
icsFileLines = append(icsFileLines, "DTSTAMP:"+timestamp)
icsFileLines = append(icsFileLines, "BEGIN:VALARM")
icsFileLines = append(icsFileLines, "ACTION:DISPLAY")
icsFileLines = append(icsFileLines, "DESCRIPTION:This is an event reminder")
icsFileLines = append(icsFileLines, "TRIGGER:"+triggerExpression)
icsFileLines = append(icsFileLines, "END:VALARM")
icsFileLines = append(icsFileLines, "END:VEVENT")
return icsFileLines
}
func parseEventDate(line string) time.Time {
lineTokens := strings.Split(line, ":")
dateString := lineTokens[1]
return parseDate(dateString)
}
func parseSummary(line string) string {
lineTokens := strings.Split(line, ":")
return lineTokens[1]
}
func parseDate(dateString string) time.Time {
date, err := time.Parse(icsDateLayout, dateString)
if err != nil {
log.Fatalf("failed to parse date: %s", err)
}
return date
}
func getFilenameFromArgs() string {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) < 1 {
log.Fatal("filename is missing")
}
filename := argsWithoutProg[0]
return filename
}
func isCalendarEndLine(line string) bool {
return strings.EqualFold(line, "END:VCALENDAR")
}
func isEventEndLine(line string) bool {
return strings.EqualFold(line, "END:VEVENT")
}
func isStartDateLine(line string) bool {
return strings.HasPrefix(line, "DTSTART")
}
func isEventStartLine(line string) bool {
return strings.HasPrefix(line, "BEGIN:VEVENT")
}