-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (64 loc) · 1.68 KB
/
main.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
package main
//go:generate go-swagger generate client -f api.yml
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"path"
"strconv"
"time"
"github.com/t-muehlberger/rio-tachograph-downloader/models"
)
func main() {
rio, cfg, err := setup()
if err != nil {
log.Fatal(err)
}
var totalCount, downloadedCount int
files, errs := rio.GetFileMetadata()
for file := range files {
totalCount++
p := getFilePath(cfg.targetDir, file)
// check if file exists
_, err := os.Stat(p)
if err == nil {
continue
} else if !errors.Is(err, os.ErrNotExist) {
log.Fatalf("failed to stat file: %v", err)
}
// download file
size, err := rio.DownloadFile(file.FileID, p)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s (%d bytes)\n", file.FileName, size)
// simple rate limiting
time.Sleep(time.Duration(1000/cfg.filesPerSec) * time.Millisecond)
downloadedCount++
}
for error := range errs {
log.Fatal(error)
}
fmt.Printf("Finished - Downloaded %d files, skipped %d existing files\n", downloadedCount, totalCount-downloadedCount)
}
func setup() (*rio, config, error) {
cfg, err := getConfig()
if err != nil {
return nil, cfg, err
}
httpClient := &http.Client{}
authenticator := NewAuthenticator(cfg.toeknUrl, cfg.clientID, cfg.clientSecret, cfg.integrationID, httpClient)
rio := NewRio(httpClient, authenticator, cfg)
return rio, cfg, nil
}
func getFilePath(targetDir string, fileMetadata *models.FileMetadataModel) string {
timeCreated := time.Time(fileMetadata.TimeCreated)
return path.Join(
targetDir,
fileMetadata.FileType,
strconv.Itoa(timeCreated.Local().Year()),
fmt.Sprintf("%02d", timeCreated.Local().Month()),
fileMetadata.FileName)
}