-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestattet.go
109 lines (96 loc) · 2.76 KB
/
gestattet.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/arran4/golang-ical"
)
func init() {
RegisterAdapter("gestattet", gestattetUrl, "Underground concerts in Aachen", gestattetAdapter)
}
type gestattetLocation struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
type gestattetEvent struct {
Id int64 `json:"id"`
Date string `json:"date"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Location gestattetLocation `json:"location"`
CreatedAt time.Time `json:"date_created"`
UpdatedAt time.Time `json:"date_updated"`
}
const (
gestattetUrl = "https://gestattet.es/"
gestattetApi = "https://api.gestattet.es/items/Events/?fields=id,date,title,description,image,location.id,location.name,date_created,date_updated&limit=-1"
gestattetRover = 2
gestattetLola = 4
gestattetLocalFormat = "2006-01-02T15:04:05"
)
var gestattetTZ, _ = time.LoadLocation("Europe/Berlin")
func (e gestattetEvent) StartTime() time.Time {
t, _ := time.ParseInLocation(gestattetLocalFormat, e.Date, gestattetTZ)
return t
}
func (e gestattetEvent) EndTime() time.Time {
t := e.StartTime()
if e.Location.Id == gestattetLola || e.Location.Id == gestattetRover {
// these always end at 22:00
end := time.Date(t.Year(), t.Month(), t.Day(), 22, 0, 0, 0, t.Location())
if end.After(t) {
return end
}
}
return t.Add(3 * time.Hour)
}
type gestattetData struct {
Data []gestattetEvent `json:"data"`
}
func gestattetAdapter(w io.Writer) error {
res, err := http.Get(gestattetApi)
if err != nil {
return err
}
defer res.Body.Close()
body := new(gestattetData)
err = json.NewDecoder(res.Body).Decode(body)
if err != nil {
return err
}
cal := ics.NewCalendarFor("github.com/cbix/ics")
cal.SetName("gestattet.es")
cal.SetUrl(gestattetUrl)
cal.SetTimezoneId("Europe/Berlin")
lastModified := time.Time{}
now := time.Now()
for _, ev := range body.Data {
event := cal.AddEvent(fmt.Sprintf("event-%[email protected]", ev.Id))
event.SetDtStampTime(now)
event.SetSummary(ev.Title)
event.SetDescription(ev.Description)
event.SetLocation(ev.Location.Name)
event.SetStartAt(ev.StartTime())
event.SetEndAt(ev.EndTime())
event.SetURL("https://api.gestattet.es/assets/" + ev.Image)
if !ev.CreatedAt.IsZero() {
event.SetCreatedTime(ev.CreatedAt)
if ev.CreatedAt.After(lastModified) {
lastModified = ev.CreatedAt
}
}
if !ev.UpdatedAt.IsZero() {
event.SetModifiedAt(ev.UpdatedAt)
if ev.UpdatedAt.After(lastModified) {
lastModified = ev.UpdatedAt
}
}
}
if !lastModified.IsZero() {
cal.SetLastModified(lastModified)
}
return cal.SerializeTo(w)
}