102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package cif
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
|
"git.fjla.uk/owlboard/timetable-mgr/nrod"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Replaces all existing CIF Data with a new download
|
|
func runCifFullDownload(cfg *helpers.Configuration) error {
|
|
log.Msg.Info("Downloading all CIF Data")
|
|
|
|
// Download CIF Data file
|
|
url, err := getUpdateUrl("full")
|
|
if err != nil {
|
|
log.Msg.Error("Error getting download URL", zap.Error(err))
|
|
}
|
|
data, err := nrod.NrodDownload(url, cfg)
|
|
if err != nil {
|
|
log.Msg.Error("Error downloading CIF data", zap.Error(err))
|
|
}
|
|
|
|
// Parse CIF file
|
|
parsed, err := parseCifData(data)
|
|
if err != nil {
|
|
log.Msg.Error("Error parsing CIF data", zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Drop timetable collection
|
|
dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database.
|
|
|
|
// Process CIF file
|
|
err = processParsedCif(parsed)
|
|
if err != nil {
|
|
log.Msg.Error("Error processing CIF data", zap.Error(err))
|
|
}
|
|
|
|
newMeta := generateMetadata(&parsed.header)
|
|
ok := dbAccess.PutCifMetadata(newMeta)
|
|
if !ok {
|
|
log.Msg.Warn("CIF Data updated, but metadata write failed")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Runs a CIF Update for up to five days
|
|
func runCifUpdateDownload(cfg *helpers.Configuration, metadata *dbAccess.CifMetadata, days []time.Time) error {
|
|
log.Msg.Info("Downloading CIF Updates")
|
|
|
|
// Loop over dates
|
|
for _, time := range days {
|
|
log.Msg.Info("Downloading CIF File", zap.Time("CIF Data from", time))
|
|
|
|
// Download CIF data file
|
|
data, err := fetchUpdate(time, cfg)
|
|
if err != nil {
|
|
log.Msg.Error("Error fetching CIF update", zap.Error(err))
|
|
return err
|
|
}
|
|
// Parse CIF file
|
|
parsed, err := parseCifData(data)
|
|
if err != nil {
|
|
log.Msg.Error("Error parsing CIF data", zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Check metadata sequence - Handle a metadata sequence error. Probably by deleting metadata so next update triggers full download
|
|
//// I need to check what the sequence looks like in a full download first.
|
|
//// Regarding metadata, I will need to replace 'metadata *dbAccess.CifMetadata' with the new metadata to ensure it is checked correctly in each iteration.
|
|
// Process CIF file
|
|
|
|
metadata = generateMetadata(&parsed.header)
|
|
}
|
|
|
|
// Write metadata
|
|
|
|
return nil
|
|
}
|
|
|
|
// Wraps nrod.NrodDownload() into a function which can handle downloading data for a given day
|
|
func fetchUpdate(t time.Time, cfg *helpers.Configuration) ([]byte, error) {
|
|
url, err := getUpdateUrl("daily")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Append day string to URL
|
|
url = url + getDayString(t)
|
|
|
|
downloadedData, err := nrod.NrodDownload(url, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return downloadedData, nil
|
|
}
|