-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (190 loc) · 5.68 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/sirupsen/logrus"
)
const (
SeqNum = 0
)
var (
hturl = url.URL{
Scheme: "https",
Host: "firestore.googleapis.com",
Path: "/v1/projects/junctor-hackertracker/databases/(default)/documents/conferences/MOCA2024:runQuery",
}
)
type StringField struct {
StringValue string `json:"stringValue,omitempty"`
}
type IntField struct {
IntegerValue string `json:"integerValue,omitempty"`
}
type Value struct {
MapValue struct {
Fields struct {
ConferenceID interface{} `json:"conference_id,omitempty"`
EventIDs ArrayField `json:"event_i_ds,omitempty"`
Name StringField `json:"name,omitempty"`
Links ArrayField `json:"links,omitempty"`
Affiliations ArrayField `json:"affiliations,omitempty"`
Media ArrayField `json:"media,omitempty"`
ID IntField `json:"id,omitempty"`
ShortName StringField `json:"short_name,omitempty"`
} `json:"fields,omitempty"`
} `json:"mapValue,omitempty"`
}
type ArrayField struct {
ArrayValue struct {
Values []Value `json:"values,omitempty"`
} `json:"arrayValue,omitempty"`
}
type MapField struct {
MapValue struct {
Fields struct {
ConferenceID IntField `json:"conference_id,omitempty"`
Conference StringField `json:"conference,omitempty"`
UpdatedAt StringField `json:"updated_at,omitempty"`
UpdatedTsz StringField `json:"updated_tsz,omitempty"`
Color StringField `json:"color,omitempty"`
Name StringField `json:"name,omitempty"`
ID IntField `json:"id,omitempty"`
} `json:"fields,omitempty"`
} `json:"mapValue,omitempty"`
}
type Item struct {
Document struct {
Name string `json:"name"`
Fields struct {
Conference StringField `json:"conference,omitempty"`
Timezone StringField `json:"timezone,omitempty"`
Link StringField `json:"link,omitempty"`
Title StringField `json:"title,omitempty"`
Description StringField `json:"description,omitempty"`
Media ArrayField `json:"media,omitempty"`
Type MapField `json:"type,omitempty"`
BeginTsz StringField `json:"begin_tsz,omitempty"`
EndTsz StringField `json:"end_tsz,omitempty"`
BeginTimestamp StringField `json:"begin_timestamp,omitempty"`
EndTimestamp StringField `json:"end_timestamp,omitempty"`
UpdatedTsz StringField `json:"updated_tsz,omitempty"`
UpdatedTimestamp StringField `json:"updated_timestamp,omitempty"`
Location Value `json:"location,omitempty"`
Speakers ArrayField `json:"speakers,omitempty"`
Links ArrayField `json:"links,omitempty"`
} `json:"fields,omitempty"`
} `json:"document"`
}
func main() {
reqData := []byte(`{
"structuredQuery": {
"from": [
{
"collectionId": "events"
}
],
"orderBy": [
{
"field": {
"fieldPath": "begin_timestamp"
},
"direction": "DESCENDING"
},
{
"field": {
"fieldPath": "__name__"
},
"direction": "DESCENDING"
}
]
}
}`)
req, err := http.NewRequest(http.MethodPost, hturl.String(), bytes.NewBuffer(reqData))
if err != nil {
logrus.Fatalf("Failed to create new request: %v", err)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
logrus.Fatalf("HTTP POST failed: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
logrus.Fatalf("Status code is not 200 OK but %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.Fatalf("Failed to read HTTP body: %v", err)
}
var items []Item
if err := json.Unmarshal(body, &items); err != nil {
logrus.Fatalf("Failed to unmarshal response: %v", err)
}
ics := fmt.Sprintf(`BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//moca/camp/schedule v1.0//IT
VTIMEZONE: Europe/Rome
`)
for _, item := range items {
eventUID := item.Document.Fields.Type.MapValue.Fields.ID.IntegerValue
location := item.Document.Fields.Location.MapValue.Fields.Name.StringValue
speakerNames := make([]string, 0, len(item.Document.Fields.Speakers.ArrayValue.Values))
for _, v := range item.Document.Fields.Speakers.ArrayValue.Values {
name := v.MapValue.Fields.Name.StringValue
if name != "" {
name = strings.Replace(name, "\n", "\\n", -1)
name = strings.Replace(name, "\"", "'", -1)
speakerNames = append(speakerNames, name)
}
}
speakers := "unknown"
if len(speakerNames) > 0 {
speakers = strings.Join(speakerNames, ", ")
}
start, err := time.Parse("2006-01-02T15:04:05Z", item.Document.Fields.BeginTsz.StringValue)
if err != nil {
logrus.Fatalf("Failed to parse start time %q: %v", item.Document.Fields.BeginTsz.StringValue, err)
}
end, err := time.Parse("2006-01-02T15:04:05Z", item.Document.Fields.EndTsz.StringValue)
if err != nil {
logrus.Fatalf("Failed to parse end time %q: %v", item.Document.Fields.EndTsz.StringValue, err)
}
summary := item.Document.Fields.Title.StringValue
description := item.Document.Fields.Description.StringValue
description = strings.Replace(description, "\n", "\\n", -1)
description = strings.Replace(description, "\r", "", -1)
ics += fmt.Sprintf(`BEGIN:VEVENT
UID:%s
SEQUENCE: %d
ORGANIZER;CN=%s:MAILTO:info@olografix.org
DTSTAMP:20240912T140000Z
DTSTART:%s
DTEND:%s
SUMMARY:%s
DESCRIPTION: %s
GEO:42.5816338;14.0901461
LOCATION: %s
END:VEVENT
`,
eventUID,
SeqNum,
speakers,
start.Format("20060102T150405Z"),
end.Format("20060102T150405Z"),
summary,
description,
location,
)
}
ics += fmt.Sprintln("END:VCALENDAR")
ics = strings.Replace(ics, "\n", "\r\n", -1)
fmt.Println(ics)
}