/
course.go
193 lines (157 loc) · 4.04 KB
/
course.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
package unibo
import (
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/patrickmn/go-cache"
)
type Course struct {
AnnoAccademico string
Immatricolabile string
Codice int
Descrizione string
Url string
Campus string
Ambiti string
Tipologia string
DurataAnni int
Internazionale bool
InternazionaleTitolo string
InternazionaleLingua string
Lingue string
Accesso string
SedeDidattica string
}
type CourseId struct {
Tipologia string
Id string
}
var websiteIdCache = cache.New(cache.NoExpiration, cache.NoExpiration)
// GetCourseWebsiteId returns the [CourseWebsiteId] of the course.
//
// If the course website id is already set, it returns it,
// otherwise it scrapes it from the course website.
func (c Course) GetCourseWebsiteId() (CourseId, error) {
codeStr := strconv.Itoa(c.Codice)
// If the course website id is already in the cache, return it
websiteIdAny, found := websiteIdCache.Get(codeStr)
if found {
return websiteIdAny.(CourseId), nil
}
// Scrape the course website id and set it
websiteId, err := c.scrapeCourseWebsiteId()
if err != nil {
return CourseId{}, err
}
websiteIdCache.Set(codeStr, websiteId, cache.DefaultExpiration)
return websiteId, nil
}
var reg = regexp.MustCompile(`<a title="Sito del corso" href="https://corsi\.unibo\.it/(.+?)"`)
func (c Course) scrapeCourseWebsiteId() (CourseId, error) {
resp, err := Client.Get(c.Url)
if err != nil {
return CourseId{}, err
}
buf := new(bytes.Buffer)
// Read all body
_, err = io.Copy(buf, resp.Body)
if err != nil {
return CourseId{}, err
}
// Close body
err = resp.Body.Close()
if err != nil {
return CourseId{}, err
}
// Convert body to string
found := reg.FindStringSubmatch(buf.String())
if found == nil {
return CourseId{}, fmt.Errorf("unable to find course website")
}
// full url -> laurea/IngegneriaInformatica
id := found[1]
// laurea/IngegneriaInformatica -> IngegneriaInformatica
split := strings.Split(id, "/")
return CourseId{split[0], split[1]}, nil
}
func (c Course) GetCurricula(year int) (Curricula, error) {
id, err := c.GetCourseWebsiteId()
if err != nil {
return nil, err
}
curricula, err := FetchCurricula(id, year)
if err != nil {
return nil, err
}
return curricula, nil
}
func (c Course) GetAllCurricula() (map[int]Curricula, error) {
id, err := c.GetCourseWebsiteId()
if err != nil {
return nil, fmt.Errorf("could not get course website id: %w", err)
}
currCh := make(chan Curricula)
errCh := make(chan error)
for year := 1; year <= c.DurataAnni; year++ {
go func(year int) {
curricula, err := FetchCurricula(id, year)
if err != nil {
errCh <- err
return
}
currCh <- curricula
}(year)
}
curriculaMap := make(map[int]Curricula, c.DurataAnni)
for year := 1; year <= c.DurataAnni; year++ {
select {
case curricula := <-currCh:
curriculaMap[year] = curricula
case err := <-errCh:
return nil, err
}
}
return curriculaMap, nil
}
func (c Course) GetTimetable(year int, curriculum Curriculum, period *TimetablePeriod) (Timetable, error) {
id, err := c.GetCourseWebsiteId()
if err != nil {
return nil, err
}
timetable, err := FetchTimetable(id, curriculum, year, period)
if err != nil {
return nil, err
}
return timetable, nil
}
type CoursesMap map[int]Course
func (c CoursesMap) ToList() Courses {
courses := make([]Course, 0, len(c))
for _, course := range c {
courses = append(courses, course)
}
return courses
}
func (c CoursesMap) FindById(id int) (*Course, bool) {
course, found := c[id]
return &course, found
}
type Courses []Course
func (c Courses) Len() int {
return len(c)
}
func (c Courses) Less(i, j int) bool {
if c[i].AnnoAccademico != c[j].AnnoAccademico {
return c[i].AnnoAccademico < c[j].AnnoAccademico
}
if c[i].Tipologia != c[j].Tipologia {
return c[i].Tipologia < c[j].Tipologia
}
return c[i].Codice < c[j].Codice
}
func (c Courses) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}