aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/schedule/service.go
blob: 70587ef9fc6d4549208ef7eb4da583947670d643 (plain)
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
package schedule

import (
	"bufio"
	"encoding/xml"
	"fmt"
	"net/http"
	"sync"
	"time"
)

type Service interface {
	GetSchedule() (*Schedule, *time.Time, error)
}

type Schedule struct {
	XMLName    xml.Name   `json:"-" xml:"schedule"`
	Conference Conference `json:"conference" xml:"conference"`
	Tracks     []Track    `json:"tracks" xml:"tracks>track"`
	Days       []Day      `json:"days" xml:"day"`
}

type Conference struct {
	Title            string `json:"title" xml:"title"`
	Venue            string `json:"venue" xml:"venue"`
	City             string `json:"city" xml:"city"`
	Start            string `json:"start" xml:"start"`
	End              string `json:"end" xml:"end"`
	Days             int    `json:"days" xml:"days"`
	DayChange        string `json:"dayChange" xml:"day_change"`
	TimeslotDuration string `json:"timeslotDuration" xml:"timeslot_duration"`
	BaseURL          string `json:"baseUrl" xml:"base_url"`
	TimeZoneName     string `json:"timeZoneName" xml:"time_zone_name"`
}

type Track struct {
	Name string `json:"name" xml:",chardata"`
}

type Day struct {
	Date  string `json:"date" xml:"date,attr"`
	Start string `json:"start" xml:"start"`
	End   string `json:"end" xml:"end"`
	Rooms []Room `json:"rooms" xml:"room"`
}

type Room struct {
	Name   string  `json:"name" xml:"name,attr"`
	Events []Event `json:"events" xml:"event"`
}

type Event struct {
	ID          int          `json:"id" xml:"id,attr"`
	GUID        string       `json:"guid" xml:"guid,attr"`
	Date        string       `json:"date" xml:"date"`
	Start       string       `json:"start" xml:"start"`
	Duration    string       `json:"duration" xml:"duration"`
	Room        string       `json:"room" xml:"room"`
	URL         string       `json:"url" xml:"url"`
	Track       string       `json:"track" xml:"track"`
	Type        string       `json:"type" xml:"type"`
	Title       string       `json:"title" xml:"title"`
	Abstract    string       `json:"abstract" xml:"abstract"`
	Persons     []Person     `json:"persons" xml:"persons>person"`
	Attachments []Attachment `json:"attachments" xml:"attachments>attachment"`
	Links       []Link       `json:"links" xml:"links>link"`
}

type Person struct {
	ID   int    `json:"id" xml:"id,attr"`
	Name string `json:"name" xml:",chardata"`
}

type Attachment struct {
	Type string `json:"string" xml:"id,attr"`
	Href string `json:"href" xml:"href,attr"`
	Name string `json:"name" xml:",chardata"`
}

type Link struct {
	Href string `json:"href" xml:"href,attr"`
	Name string `json:"name" xml:",chardata"`
}

type service struct {
	schedule     *Schedule
	pentabarfUrl string
	lastUpdated  time.Time
	lock         sync.Mutex
}

func NewService(pentabarfUrl string) (Service, error) {
	service := &service{
		pentabarfUrl: pentabarfUrl,
		lastUpdated:  time.Unix(0, 0),
	}

	err := service.updateSchedule()
	if err != nil {
		return nil, fmt.Errorf("could not read schedule from '%s' (is it a valid pentabarf XML file?): %w", pentabarfUrl, err)
	}
	return service, nil
}

func (s *service) GetSchedule() (*Schedule, *time.Time, error) {
	if s.hasScheduleExpired() {
		err := s.updateSchedule()
		if err != nil {
			return nil, nil, err
		}
	}

	return s.schedule, &s.lastUpdated, nil
}

func (s *service) hasScheduleExpired() bool {
	expire := s.lastUpdated.Add(15 * time.Minute)
	return time.Now().After(expire)
}

func (s *service) updateSchedule() error {
	s.lock.Lock()
	defer s.lock.Unlock()

	if !s.hasScheduleExpired() {
		return nil
	}

	res, err := http.Get(s.pentabarfUrl)
	if err != nil {
		return err
	}

	reader := bufio.NewReader(res.Body)

	var schedule Schedule

	decoder := xml.NewDecoder(reader)
	if err := decoder.Decode(&schedule); err != nil {
		return fmt.Errorf("failed to decode XML: %w", err)
	}

	s.schedule = &schedule
	s.lastUpdated = time.Now()
	return nil
}