locationcode/downloader.go
Ask Bjørn Hansen 8ff74a7ca3
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Update download base url
2025-02-02 04:39:25 -08:00

56 lines
1.4 KiB
Go

package main
import (
"errors"
"fmt"
"path"
"time"
alphafoxtrot "github.com/grumpypixel/go-airport-finder"
"github.com/grumpypixel/go-webget"
)
// const downloadBaseURL = alphafoxtrot.OurAirportsBaseURL
// const downloadBaseURL = "https://tmp.askask.com/os/ourairports/"
// https://github.com/davidmegginson/ourairports-data
const downloadBaseURL = "https://davidmegginson.github.io/ourairports-data/"
// Download csv files from OurAirports.com
func DownloadDatabase(targetDir string) error {
files := make([]string, 0)
for _, filename := range alphafoxtrot.OurAirportsFiles {
files = append(files, downloadBaseURL+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()
}