-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
350 lines (322 loc) · 10 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* rastermimimi reads calendar data from various sources and displays then as html.
*/
package main
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"sort"
"text/template"
"time"
_ "time/tzdata"
)
type LocalTime struct {
time.Time
}
func (t *LocalTime) UnmarshalJSON(b []byte) error {
_, offset := time.Now().Local().Zone()
s := string(b)
// get rid of leading and trailing " from JSON"
s = s[1 : len(s)-1]
// replace " " with "T" so it works for LibreTime as well as web
s = s[0:10] + `T` + s[11:]
// add local zone to the end ssince we always work with local time
s = fmt.Sprintf("%s+%02d00", s, offset/3600)
p, err := time.Parse(`2006-01-02T15:04:05Z0700`, s)
if err != nil {
return err
}
t.Time = p
return nil
}
func (t *LocalTime) GetTime() time.Time {
return time.Time(t.Time)
}
type WebsiteEventOrganizerCalendarEvent struct {
AllDay bool `json:"allDay"`
Category []string `json:"category"`
ClassName []string `json:"className"`
Description string `json:"description"`
End LocalTime `json:"end"`
Organiser int `json:"organiser"`
Start LocalTime `json:"start"`
Tags []string `json:"tags"`
TextColor string `json:"textColor"`
Title string `json:"title"`
URL string `json:"url"`
}
type LibreTimeLiveInfoV2Show struct {
Name string `json:"name"`
Description string `json:"description"`
Genre string `json:"genre"`
ID int `json:"id"`
InstanceID int `json:"instance_id"`
Record int `json:"record"`
URL string `json:"url"`
ImagePath string `json:"image_path"`
Starts LocalTime `json:"starts"`
Ends LocalTime `json:"ends"`
}
type LibreTimeLiveInfoV2Shows struct {
Previous []interface{} `json:"previous"`
Current LibreTimeLiveInfoV2Show `json:"current"`
Next []LibreTimeLiveInfoV2Show `json:"next"`
}
type LibreTimeLiveInfoV2 struct {
Station map[string]string `json:"station"`
Tracks interface{} `json:"tracks"`
Shows LibreTimeLiveInfoV2Shows `json:"shows"`
Sources map[string]string `json:"sources"`
}
type gridSlot struct {
WebsiteEventOrganizerCalendarEvent WebsiteEventOrganizerCalendarEvent
LibreTimeLiveInfoV2Show LibreTimeLiveInfoV2Show
}
type gridError struct {
Message string
Time time.Time
Slot gridSlot
}
type gridErrors []gridError
func (g gridErrors) Len() int {
return len(g)
}
func (g gridErrors) Less(i, j int) bool {
return g[i].Time.Before(g[j].Time)
}
func (g gridErrors) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}
// string => gridSlot map
var grid map[string]gridSlot
var errors gridErrors
var websiteURL string
var libretimeURL string
var duration int
func main() {
websiteURL = "https://rabe.ch"
libretimeURL = "https://airtime.service.int.rabe.ch"
// check next 2 months
duration = 2 * 30
reloadState()
indexTpl := `<!DOCTYPE html>
<title>Raster Mimimi</title>
<style>
html, body {
font-family: sans-serif;
font-size: 0.9em;
}
article {
border: 1px solid gray;
padding: 1em;
margin-bottom: 0.5em;
}
section {
margin-left: 2em;
}
section.lead {
font-size: 1.5em;
margin-left: 0;
}
</style>
<h1>technisches Programmraster Mimimi</h1>
<nav><a href="/refresh">Refresh...</a> | <a href="#" onclick="alert('ja nid usdrucke!, so vowäge umwäut!')">Drucken...</a></nav>
<p>Dieses Tool hat in den nächsten 60 Tagen {{.|len}} Mimimis gefunden.</p>
{{$otime := "02 Jan 2006 15:04:05"}}
{{range .}}
{{$time := .Time.Format "02 Jan 2006 15:04:05"}}
{{if ne $time $otime}}
<hr>
<h2>{{$time}}</h2>
{{end}}
<article>
<section class="lead">{{.Message}}</section>
<!-- {{.}} -->
{{if .Slot.WebsiteEventOrganizerCalendarEvent.Title}}
<section>
<h3>Web: {{.Slot.WebsiteEventOrganizerCalendarEvent.Title}}</h3>
<p>{{.Slot.WebsiteEventOrganizerCalendarEvent.Start.Format "02 Jan 2006 15:04:05"}} - {{.Slot.WebsiteEventOrganizerCalendarEvent.End.Format "02 Jan 2006 15:04:05"}}</p>
<p><b>URL:</b> <code>{{.Slot.WebsiteEventOrganizerCalendarEvent.URL}}</code></p>
</section>
{{end}}
{{if .Slot.LibreTimeLiveInfoV2Show.Name}}
<section>
<h3>LibreTime: {{.Slot.LibreTimeLiveInfoV2Show.Name}}</h3>
<p>{{.Slot.LibreTimeLiveInfoV2Show.Starts.Format "02 Jan 2006 15:04:05"}} - {{.Slot.LibreTimeLiveInfoV2Show.Ends.Format "02 Jan 2006 15:04:05"}}</p>
<p><b>URL:</b> <code>{{.Slot.LibreTimeLiveInfoV2Show.URL}}</code></p>
</section>
{{end}}
</article>
{{$otime = $time}}
{{end}}
`
tmpl := template.Must(template.New("index").Parse(indexTpl))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := tmpl.Execute(w, errors)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
http.HandleFunc("/refresh", func(rw http.ResponseWriter, r *http.Request) {
reloadState()
http.Redirect(rw, r, "/", http.StatusTemporaryRedirect)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
func reloadState() {
grid = make(map[string]gridSlot)
errors = make(gridErrors, 0)
// load data from sources and write loading errors to errors
readFromWebsite(websiteURL, time.Now(), duration)
readFromLibretime(libretimeURL, duration)
// find errors, writes to "errors" as a side effect
checkForErrors()
}
func readFromWebsite(url string, start time.Time, duration int) {
startReq := start.Format("2006-01-02")
endReq := start.AddDate(0, 0, duration).Format("2006-01-02")
resp, err := http.Get(
fmt.Sprintf(
"%s/wp-admin/admin-ajax.php?action=eventorganiser-fullcal&start=%s&end=%s&timeformat=%s",
url,
startReq,
endReq,
"G%3Ai",
),
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// parse JSON
var events []WebsiteEventOrganizerCalendarEvent
err = json.Unmarshal(body, &events)
if err != nil {
log.Fatal(err)
}
for _, event := range events {
start := event.Start.String()
slot, ok := grid[start]
if ok {
if slot.WebsiteEventOrganizerCalendarEvent.Title != "" {
appendError(
fmt.Sprintf("Mehrfacheintrag auf Webseite: %s (neu: %s)",
slot.WebsiteEventOrganizerCalendarEvent.Title, event.Title),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
}
slot.WebsiteEventOrganizerCalendarEvent = event
grid[start] = slot
} else {
grid[start] = gridSlot{
WebsiteEventOrganizerCalendarEvent: event,
}
}
}
}
func readFromLibretime(url string, duration int) {
// read from libretime calendar
resp, err := http.Get(fmt.Sprintf("%s/api/live-info-v2?days=%d&shows=600000000", url, duration+1))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// parse JSON
var liveInfo LibreTimeLiveInfoV2
err = json.Unmarshal(body, &liveInfo)
if err != nil {
log.Fatal(err)
}
liveInfo.Shows.Next = append(liveInfo.Shows.Next, liveInfo.Shows.Current)
for _, ltShow := range liveInfo.Shows.Next {
start := ltShow.Starts.String()
slot, ok := grid[start]
if ok {
if slot.LibreTimeLiveInfoV2Show.Name != "" {
log.Panicf("slot %s already has a libretime title", start)
}
ltShow.Name = html.UnescapeString(ltShow.Name)
slot.LibreTimeLiveInfoV2Show = ltShow
grid[start] = slot
} else {
grid[start] = gridSlot{
LibreTimeLiveInfoV2Show: ltShow,
}
}
}
}
func checkForErrors() {
for _, slot := range grid {
// skip past events
if slot.WebsiteEventOrganizerCalendarEvent.Title != "" && slot.WebsiteEventOrganizerCalendarEvent.Start.Before(time.Now()) {
continue
}
if slot.WebsiteEventOrganizerCalendarEvent.Title != "" && slot.LibreTimeLiveInfoV2Show.Name == "" {
if slot.WebsiteEventOrganizerCalendarEvent.Title != "Klangbecken" {
appendError(fmt.Sprintf("%s fehlt in LibreTime.",
slot.WebsiteEventOrganizerCalendarEvent.Title),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
}
} else if slot.LibreTimeLiveInfoV2Show.Name != "" && slot.WebsiteEventOrganizerCalendarEvent.Title == "" {
if slot.LibreTimeLiveInfoV2Show.Name != "Klangbecken" {
appendError(
fmt.Sprintf("%s fehlt auf Web Seite.",
slot.LibreTimeLiveInfoV2Show.Name),
slot.LibreTimeLiveInfoV2Show.Starts, slot)
}
} else if slot.WebsiteEventOrganizerCalendarEvent.Title != slot.LibreTimeLiveInfoV2Show.Name {
appendError(
fmt.Sprintf("Titel auf Webseite (%s) stimmt nicht mit LibreTime (%s) überein.",
slot.WebsiteEventOrganizerCalendarEvent.Title, slot.LibreTimeLiveInfoV2Show.Name),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
}
// skip events not on both sides for further checks
if slot.WebsiteEventOrganizerCalendarEvent.Title == "" || slot.LibreTimeLiveInfoV2Show.Name == "" {
continue
}
if slot.LibreTimeLiveInfoV2Show.URL != slot.WebsiteEventOrganizerCalendarEvent.URL {
if slot.WebsiteEventOrganizerCalendarEvent.URL == "#" {
appendError(
fmt.Sprintf("Keine URL für Sendung %s auf Website hinterlegt.",
slot.WebsiteEventOrganizerCalendarEvent.Title),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
} else {
appendError(
fmt.Sprintf("URL auf Webseite (%s) stimmt nicht mit LibreTime (%s) überein.",
slot.WebsiteEventOrganizerCalendarEvent.URL, slot.LibreTimeLiveInfoV2Show.URL),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
}
}
// skip checks where we ignore Klangbecken
if slot.LibreTimeLiveInfoV2Show.Name == "Klangbecken" {
continue
}
if !slot.LibreTimeLiveInfoV2Show.Ends.Equal(slot.WebsiteEventOrganizerCalendarEvent.End.Time) {
appendError(
fmt.Sprintf("Ende auf Webseite (%s) stimmt nicht mit LibreTime (%s) überein.",
slot.WebsiteEventOrganizerCalendarEvent.End.Format("02 Jan 2006 15:04:05"),
slot.LibreTimeLiveInfoV2Show.Ends.Format("02 Jan 2006 15:04:05")),
slot.WebsiteEventOrganizerCalendarEvent.Start, slot)
}
}
sort.Sort(errors)
}
func appendError(message string, start LocalTime, slot gridSlot) {
errors = append(errors, gridError{
Message: message,
Time: start.GetTime(),
Slot: slot,
})
}