-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata.go
142 lines (114 loc) · 2.83 KB
/
data.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
package main
import (
"encoding/json"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/csunibo/unibo-go/opendata"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/VaiTon/unibocalendar/unibo_integ"
)
const (
coursesPathJson = "data/courses.json"
packageId = "degree-programmes"
resourceAlias = "corsi_latest_it"
)
func downloadOpenDataIfNewer() {
// Get package
pack, err := opendata.FetchPackage(packageId)
if err != nil {
log.Warn().Err(err).Msg("unable to get package")
return
}
// If no resources, return nil
if len(pack.Result.Resources) == 0 {
log.Warn().Msg("no resources found while downloading open data")
return
}
// Get wanted resource
resource, found := pack.Result.Resources.GetByAlias(resourceAlias)
if !found {
log.Warn().Msgf("unable to find resource '%s'", resourceAlias)
return
}
// Get last modified resource
lastMod := resource.LastMod
// Parse last modified time
lastModTime, err := time.Parse("2006-01-02T15:04:05.999999999", lastMod)
if err != nil {
log.Panic().Err(err).Msg("Unable to parse last modified time")
}
old := false
// Get file last modified time, if file does not exist return lastMod.Url
stat, err := os.Stat(coursesPathJson)
if err != nil {
if !os.IsNotExist(err) {
log.Panic().Err(err).Msg("Unable to get file stat")
} else {
old = true
}
}
if !old && stat.ModTime().After(lastModTime) {
log.Info().Msg("Opendata file is up to date")
return
}
courses, err := unibo_integ.DownloadResource(resource)
if err != nil {
log.Panic().Err(err).Msg("Unable to download courses")
}
actualYear := time.Now().Year()
// Filter courses by actual year
courses = lo.Filter(courses, func(c unibo_integ.Course, _ int) bool {
return strings.Contains(c.AnnoAccademico, strconv.Itoa(actualYear))
})
err = saveData(courses)
if err != nil {
log.Panic().Err(err).Msg("Unable to save courses")
}
log.Info().Msg("Opendata file downloaded")
}
func saveData(courses []unibo_integ.Course) error {
err := createDataFolder()
if err != nil {
return err
}
jsonFile, err := os.Create(coursesPathJson)
if err != nil {
return err
}
err = json.NewEncoder(jsonFile).Encode(courses)
if err != nil {
return err
}
return nil
}
func createDataFolder() error {
return os.MkdirAll(path.Dir(coursesPathJson), os.ModePerm)
}
func openData() (unibo_integ.CoursesMap, error) {
// Open file
file, err := os.Open(coursesPathJson)
if err != nil {
return nil, err
}
// Decode json
courses := make([]unibo_integ.Course, 0)
err = json.NewDecoder(file).Decode(&courses)
if err != nil {
return nil, err
}
// Close file
err = file.Close()
if err != nil {
return nil, err
}
// Create the map
courseMap := make(unibo_integ.CoursesMap, len(courses))
for _, course := range courses {
courseMap[course.Codice] = course
}
return courseMap, nil
}