-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
227 lines (211 loc) · 5.6 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"sort"
"strings"
"text/template"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
// TODO: add header to html output with background, timezone, github repo
type eventDetails struct {
TitleHTML string
TitleText string
TitleLink string
SpeakersHTML string
RoomHTML string
Start time.Time
End time.Time
AttachmentsHTML string
VideoHTML string
ID string // unique id for each talk, use title url
}
func (e eventDetails) StartAsHTML() string {
return e.Start.Format("15:04")
}
func (e eventDetails) EndAsHTML() string {
return e.End.Format("15:04")
}
func main() {
list := getSchedule()
// Sort by datetime start and duration
sort.Slice(list, func(i int, j int) bool {
if list[i].Start.Before(list[j].Start) {
return true
}
if list[i].Start.After(list[j].Start) {
return false
}
return list[i].End.Before(list[j].End)
})
writeHTML("fosdem_schedules.html", list)
// writeMD("fosdem_schedules.md", list)
// writeCSV("fosdem_schedules.csv", list)
}
func writeHTML(fn string, list []eventDetails) {
f, err := os.Create(fn)
if err != nil {
log.Fatalf("Unable to create '%s' because %s", fn, err)
}
defer f.Close()
t := template.Must(template.New("").Parse(htmlTemplate))
if err := t.Execute(f, &list); err != nil {
log.Fatal(err)
}
}
func writeCSV(fn string, list []eventDetails) {
// experimental
// TODO: how to write a variable number of text/link combinations in a useful way?
f, err := os.Create(fn)
if err != nil {
log.Fatalf("Unable to create '%s' because %s", fn, err)
}
defer f.Close()
for i := range list {
fmt.Fprintf(f, "\"%s\"", list[i].TitleText)
fmt.Fprintf(f, ",\"%s\"", list[i].TitleLink)
fmt.Fprintf(f, ",\"%s\"", list[i].RoomHTML)
fmt.Fprintf(f, ",\"%s\"", list[i].Start.Format("2006-01-02 15:04"))
fmt.Fprintf(f, ",\"%s\"", list[i].End.Format("2006-01-02 15:04"))
fmt.Fprintf(f, "\n")
}
}
func writeMD(fn string, list []eventDetails) {
// experimental
// TODO: local preview does not show contents as a table?
f, err := os.Create(fn)
if err != nil {
log.Fatalf("Unable to create '%s' because %s", fn, err)
}
defer f.Close()
converter := md.NewConverter("", true, nil)
for i := range list {
list[i].TitleHTML, _ = converter.ConvertString(list[i].TitleHTML)
list[i].SpeakersHTML, _ = converter.ConvertString(list[i].SpeakersHTML)
list[i].RoomHTML, _ = converter.ConvertString(list[i].RoomHTML)
list[i].AttachmentsHTML, _ = converter.ConvertString(list[i].AttachmentsHTML)
list[i].VideoHTML, _ = converter.ConvertString(list[i].VideoHTML)
}
t := template.Must(template.New("").Parse(mdTemplate))
if err := t.Execute(f, &list); err != nil {
log.Fatal(err)
}
}
func getSchedule() (list []eventDetails) {
res, err := http.Get("https://fosdem.org/2022/schedule/events/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the records with events
tr := doc.Find("div#main table.table.table-striped.table-bordered.table-condensed tbody").Children()
tr.Each(func(i int, s *goquery.Selection) {
// Get the data
dt := s.Children()
html, err := dt.First().Html()
// Skip records having room name
if err != nil || strings.HasPrefix(html, "<h4>") {
return
}
event := eventDetails{}
dt.Each(func(j int, e *goquery.Selection) {
// Title
if j == 0 {
event.TitleHTML = htmlWithFullUrls(e)
event.ID, _ = e.Find("a").Attr("href")
t, l := splitHTML(e)
event.TitleText = first(t)
event.TitleLink = first(l)
}
// Speakers
if j == 1 {
event.SpeakersHTML = htmlWithFullUrls(e)
}
// Room
if j == 2 {
event.RoomHTML = htmlWithFullUrls(e)
}
// Day
if j == 3 {
v := "2021-02-06 WET" // Saturday
if e.Text() == "Sunday" {
v = "2021-02-07 WET"
}
event.Start, _ = time.Parse("2006-01-02 MST", v)
event.End = event.Start
}
// Start
if j == 4 {
d := e.Text()
if len(d) == 5 {
d = strings.Replace(d, ":", "h", -1)
d += "m"
dd, _ := time.ParseDuration(d)
event.Start = event.Start.Add(dd)
}
}
// End
if j == 5 {
d := e.Text()
if len(d) == 5 {
d = strings.Replace(d, ":", "h", -1)
d += "m"
dd, _ := time.ParseDuration(d)
event.End = event.End.Add(dd)
}
}
// Attachments
if j == 6 {
event.AttachmentsHTML = htmlWithFullUrls(e)
}
// Video
if j == 7 {
event.VideoHTML = htmlWithFullUrls(e)
}
})
list = append(list, event)
})
return
}
func first(s []string) string {
if len(s) == 0 {
return ""
}
return s[0]
}
func splitHTML(s *goquery.Selection) (texts []string, links []string) {
s.Children().Each(func(i int, c *goquery.Selection) {
texts = append(texts, c.Text())
link, _ := c.Attr("href")
if !strings.HasPrefix(link, "https://fosdem.org") {
link = "https://fosdem.org" + link
}
links = append(links, link)
})
return
}
func htmlWithFullUrls(s *goquery.Selection) string {
html, err := s.Html()
if err != nil {
return ""
}
// Assumption: if one link without the domain then no links have a domain (because attachments seem to have a full link)
if !strings.Contains(html, "https://fosdem.org") {
html = strings.ReplaceAll(html, "href=\"", "href=\"https://fosdem.org")
}
// Open all links in new tab
html = strings.ReplaceAll(html, "href=\"", "target=\"_blank\" href=\"")
return html
}