timetable-mgr/stations/run.go

94 lines
1.7 KiB
Go
Raw Normal View History

2024-06-29 21:54:32 +01:00
package stations
import (
"archive/zip"
"bytes"
"fmt"
"io"
"net/http"
)
const (
// URL to RDG XML file
url string = "https://internal.nationalrail.co.uk/4.0/stations.zip"
// URL to additional XML file
add string = "https://git.fjla.uk/OwlBoard/data/raw/branch/main/knowledgebase/additional.xml"
)
func download() ([]byte, []byte, error) {
data1, err := downloadAndExtractZip(url)
if err != nil {
return nil, nil, err
}
data2, err := downloadUrl(add)
if err != nil {
return nil, nil, err
}
return data1, data2, nil
}
func downloadUrl(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download from %s: status code: %d", url, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func downloadAndExtractZip(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download from %s: status code %d", url, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Read zip
reader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return nil, err
}
// Check zip not empty
if len(reader.File) == 0 {
return nil, fmt.Errorf("no files found in the zip archive")
}
// Read first file
file := reader.File[0]
rc, err := file.Open()
if err != nil {
return nil, err
}
defer rc.Close()
extractedData, err := io.ReadAll(rc)
if err != nil {
return nil, err
}
return extractedData, nil
}