locationcode/downloader.go
Ask Bjørn Hansen ce01df8b4a
All checks were successful
continuous-integration/drone/push Build is passing
Add error handling to file downloads
2023-09-06 00:14:37 -07:00

50 lines
1.2 KiB
Go

package main
import (
"errors"
"fmt"
"path"
"time"
alphafoxtrot "github.com/grumpypixel/go-airport-finder"
"github.com/grumpypixel/go-webget"
)
// Download csv files from OurAirports.com
func DownloadDatabase(targetDir string) error {
files := make([]string, 0)
for _, filename := range alphafoxtrot.OurAirportsFiles {
files = append(files, alphafoxtrot.OurAirportsBaseURL+filename)
}
var errs []error
for _, url := range files {
options := webget.Options{
ProgressHandler: MyProgress{},
Timeout: time.Second * 60,
CreateTargetDir: true,
}
err := webget.DownloadToFile(url, targetDir, "", &options)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
type MyProgress struct{}
func (p MyProgress) Start(sourceURL string) {
// fmt.Println()
}
func (p MyProgress) Update(sourceURL string, percentage float64, bytesRead, contentLength int64) {
if percentage > 0 {
fmt.Printf("\rDownloading %s: %v bytes [%.2f%%]", path.Base(sourceURL), bytesRead, percentage)
}
fmt.Printf("\rDownloading %s: %v bytes [done]", path.Base(sourceURL), bytesRead)
}
func (p MyProgress) Done(sourceURL string) {
fmt.Println()
}