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
|
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/arran4/golang-ical"
"io"
"net/http"
"strings"
"sync"
"time"
)
const (
API_URL = "https://api.events.ccc.de/congress/2024"
)
var (
SCHEDULE_MUTEX sync.Mutex
SCHEDULE []Event
ROOMS []Room
)
type Token struct {
Token string `json:"token"`
}
type Auth struct {
Username string `json:"username"`
Password string `json:"password"`
}
type Event struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
Slug string `json:"slug"`
URL string `json:"url"`
Track string `json:"track"`
Assembly string `json:"assembly"`
Room string `json:"room"`
Location string `json:"location"`
Language string `json:"language"`
Description string `json:"description"`
ScheduleStart time.Time `json:"schedule_start"`
ScheduleDuration string `json:"schedule_duration"`
ScheduleEnd time.Time `json:"schedule_end"`
}
// Link represents the structure of the "links" array in the JSON
type Link struct {
Type string `json:"type"`
Name string `json:"name"`
URI string `json:"uri"`
URL string `json:"url"`
}
// Room represents the structure of the main JSON object
type Room struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
RoomType string `json:"room_type"`
Capacity int `json:"capacity"`
Links []Link `json:"links"`
Assembly string `json:"assembly"`
PublicURL string `json:"public_url"`
}
func scrape_hub() ([]Event, error) {
println("scrape hub")
resp, err := http.Get(fmt.Sprintf("%s/events", API_URL))
if err != nil {
fmt.Println("Error making GET request:", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil, err
}
var events []Event
if err := json.Unmarshal(body, &events); err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return nil, err
}
// Use the events slice here
fmt.Println("Number of events:", len(events))
return events, nil
}
func cron_fetch() {
for {
fmt.Println("starting cron...")
schedule, err := scrape_hub()
if err != nil {
fmt.Println("Cron failed; retrying in a minute")
time.Sleep(1 * time.Minute)
continue
}
SCHEDULE_MUTEX.Lock()
SCHEDULE = schedule
SCHEDULE_MUTEX.Unlock()
time.Sleep(10 * time.Minute)
}
}
func get_rooms() ([]Room, error) {
println("get rooms")
resp, err := http.Get(fmt.Sprintf("%s/rooms", API_URL))
if err != nil {
fmt.Println("Error making GET request:", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil, err
}
var rooms []Room
if err := json.Unmarshal(body, &rooms); err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return nil, err
}
// Use the events slice here
fmt.Println("Number of rooms:", len(rooms))
return rooms, nil
}
func get_token(username string, password string) (string, error) {
println("get token")
jsonBytes, err := json.Marshal(Auth{Username: username, Password: password})
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return "", err
}
resp, err := http.Post(
fmt.Sprintf("%s/auth/get-token", API_URL),
"application/json",
bytes.NewBuffer(jsonBytes),
)
if err != nil {
fmt.Println("Error getting Token:", err)
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return "", err
}
var token Token
if err := json.Unmarshal(body, &token); err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return "", err
}
println(token.Token)
return token.Token, nil
}
func get_me_events(token string) ([]string, error) {
println("get personal events")
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/me/events", API_URL),
nil,
)
req.Header.Set("Authorization", fmt.Sprintf("Token %s", token))
resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Println("Error fetching personal events:", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil, err
}
println(string(body))
var uuids []string
if err := json.Unmarshal(body, &uuids); err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return nil, err
}
return uuids, nil
}
func get_schedule(w http.ResponseWriter, r *http.Request) {
println("/event.ics")
auth_header := r.Header.Get("Authorization")
println(auth_header)
if auth_header == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
credentials_encoded := auth_header[len("Basic "):]
credentials, err := base64.StdEncoding.DecodeString(credentials_encoded)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
auth := strings.SplitN(string(credentials), ":", 2)
if len(auth) != 2 {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
username := auth[0]
password := auth[1]
println(username, password)
token, err := get_token(username, password)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
ids, err := get_me_events(token)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
SCHEDULE_MUTEX.Lock()
defer SCHEDULE_MUTEX.Unlock()
var my_events []Event
for _, event := range SCHEDULE {
for _, id := range ids {
if event.ID == id {
my_events = append(my_events, event)
break
}
}
}
cal := ics.NewCalendar()
cal.SetMethod(ics.MethodRequest)
for _, event := range my_events {
var location = ""
for _, room := range ROOMS {
if event.Room == room.ID {
location = room.Name
}
}
cal_event := cal.AddEvent(event.ID)
cal_event.SetStartAt(event.ScheduleStart)
cal_event.SetEndAt(event.ScheduleEnd)
cal_event.SetSummary(event.Name)
cal_event.SetLocation(location)
cal_event.SetDescription(ics.ToText(strings.ReplaceAll(event.Description, "\r\n", "\n")))
cal_event.SetURL(event.URL)
cal_event.SetOrganizer(event.Assembly)
}
ics := cal.Serialize()
w.Header().Set("Content-Type", "text/calendar; charset=utf-8")
w.Write([]byte(ics))
}
func main() {
rooms, err := get_rooms()
if err != nil {
return
}
ROOMS = rooms
schedule, err := scrape_hub()
if err != nil {
return
}
SCHEDULE = schedule
go cron_fetch()
println("Stating Server")
// Start a web server to serve the iCal file
http.HandleFunc("/events.ics", get_schedule)
err = http.ListenAndServe("127.0.0.1:8080", nil)
if err != nil {
println(err)
}
}
|