-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
187 lines (167 loc) · 4.83 KB
/
main.go
File metadata and controls
187 lines (167 loc) · 4.83 KB
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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"html/template"
"os"
"strconv"
"strings"
)
const TIMETABLE_TEMPLATE = `{{with index .Preamble 0}}
<h1 class="dtimer" data-sh="0" data-sm="0" data-eh="{{.Eh}}" data-em="{{.Em}}" data-es="59">Please wait...</h1>
<h2 class="dtimer" data-sh="0" data-sm="0" data-eh="{{.Eh}}" data-em="{{.Em}}" data-es="59">{{.Timeframe}}</h2>
{{end}}
{{with index .Preamble 1}}
<h3 class="dtimer" data-sh="{{.Sh}}" data-sm="{{.Sm}}" data-eh="{{.Eh}}" data-em="{{.Em}}" data-es="59">NOW PLAYING</h3>
{{end}}
{{range .Body}}
<h1 class="dtimer" data-sh="{{.Sh}}" data-sm="{{.Sm}}" data-eh="{{.Eh}}" data-em="{{.Em}}" data-es="59">{{.DjName}}</h1>
<h2 class="dtimer" data-sh="{{.Sh}}" data-sm="{{.Sm}}" data-eh="{{.Eh}}" data-em="{{.Em}}" data-es="59">{{.Timeframe}}</h2>
{{end}}
{{with .Postamble}}
<h1 class="dtimer" data-sh="{{.Sh}}" data-sm="{{.Sm}}" data-eh="23" data-em="59" data-es="59"></h1>
<h2 class="dtimer" data-sh="{{.Sh}}" data-sm="{{.Sm}}" data-eh="23" data-em="59" data-es="59">The event has closed.<br>Thank you for coming!</h2>
{{end}}`
type TimeTable struct {
Preamble [2]TableData
Body []TableData
Postamble TableData
}
type TableData struct {
Sh uint8
Sm uint8
Eh uint8
Em uint8
DjName string
Timeframe string
}
func main() {
records := read_csv_records("timetable.csv")
timetable := records_to_timetable(records)
timetable_html := to_timetable_html(timetable)
generate_html("public/index.html", "index.template", timetable_html)
generate_html("public/obs.html", "obs.template", timetable_html)
}
func read_csv_records(filepath string) [][]string {
file, err := os.Open(filepath)
defer file.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open %s\n", filepath)
panic(err)
}
records, err := csv.NewReader(bufio.NewReader(file)).ReadAll()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse %s as a CSV file\n", filepath)
panic(err)
}
return records
}
func records_to_timetable(records [][]string) TimeTable {
split_time := func(time string) (string, string) {
hhmm := strings.SplitN(time, ":", 2)
if len(hhmm) != 2 {
panic(fmt.Sprintf("%s is not a valid time string; Start and End field should be of the form HH:MM\n", time))
}
return hhmm[0], hhmm[1]
}
parse_time := func(hh_str, mm_str string) (uint8, uint8) {
hh, err := strconv.ParseUint(hh_str, 10, 8)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not convert string %s into uint8\n", hh_str)
panic(err)
}
mm, err := strconv.ParseUint(mm_str, 10, 8)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not convert string %s into uint8\n", mm_str)
panic(err)
}
return uint8(hh), uint8(mm)
}
one_min_prev := func(hh, mm uint8) (prev_hh uint8, prev_mm uint8) {
if mm == 0 {
if hh == 0 {
prev_hh = 24
} else {
prev_hh = hh - 1
}
} else {
prev_hh = hh
}
if mm == 0 {
prev_mm = 59
} else {
prev_mm = mm - 1
}
return
}
body := make([]TableData, len(records)-1)
for i := 1; i < len(records); i++ {
start, end, dj_name := records[i][0], records[i][1], records[i][2]
start_hh, start_mm := parse_time(split_time(start))
end_hh, end_mm := parse_time(split_time(end))
eh, em := one_min_prev(end_hh, end_mm)
body[i-1] = TableData{
Sh: start_hh,
Sm: start_mm,
Eh: eh,
Em: em,
DjName: dj_name,
Timeframe: fmt.Sprintf("%d:%02d - %d:%02d", start_hh, start_mm, end_hh, end_mm),
}
}
event_start := records[1][0]
event_start_hh, event_start_mm := parse_time(split_time(event_start))
event_sh, event_sm := one_min_prev(event_start_hh, event_start_mm)
event_end := records[len(records)-1][1]
event_end_hh, event_end_mm := parse_time(split_time(event_end))
event_eh, event_em := one_min_prev(event_end_hh, event_end_mm)
return TimeTable{
Preamble: [2]TableData{
{
Eh: event_sh,
Em: event_sm,
Timeframe: fmt.Sprintf("Start at %d:%02d", event_start_hh, event_start_mm),
},
{
Sh: event_start_hh,
Sm: event_start_mm,
Eh: event_eh,
Em: event_em,
},
},
Body: body,
Postamble: TableData{
Sh: event_end_hh,
Sm: event_end_mm,
},
}
}
func to_timetable_html(timetable TimeTable) string {
var timetable_html strings.Builder
timetable_tmpl, err := template.New("timetable").Parse(TIMETABLE_TEMPLATE)
if err != nil {
panic(err)
}
err = timetable_tmpl.Execute(&timetable_html, timetable)
if err != nil {
panic(err)
}
return timetable_html.String()
}
func generate_html(filepath string, tmpl_filepath string, text string) {
file, err := os.Create(filepath)
defer file.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create file %s\n", filepath)
panic(err)
}
html_tmpl, err := template.ParseFiles(tmpl_filepath)
if err != nil {
panic(err)
}
err = html_tmpl.Execute(file, template.HTML(text))
if err != nil {
panic(err)
}
}