This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_qrz.go
76 lines (71 loc) · 1.98 KB
/
import_qrz.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
package forester
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
ql "github.com/k0swe/qrz-logbook"
"log"
"net/http"
"time"
)
const qrzLastFetchedDate = "qrzLastFetchedDate"
// ImportQrz imports QSOs from QRZ logbook and merges them into Firestore. Called via GCP Cloud
// Functions.
func ImportQrz(w http.ResponseWriter, r *http.Request) {
const isFixCase = true
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if handleCorsOptions(w, r) {
return
}
log.Print("Starting ImportQrz")
fb, err := MakeFirebaseManager(&ctx, r)
if err != nil {
writeError(500, "Error", err, w)
return
}
_, err = fb.GetLogbookProperty(qrzLastFetchedDate)
if err != nil {
writeError(500, "Error fetching logbook properties from firestore", err, w)
return
}
secretStore := NewSecretStore(ctx)
qrzAPIKey, err := secretStore.FetchSecret(fb.logbookID, qrzLogbookAPIKey)
if err != nil {
writeError(500, "Error fetching QRZ API key from secret manager", err, w)
return
}
qrzResponse, err := ql.Fetch(ctx, &qrzAPIKey)
if err != nil {
writeError(500, "Error fetching QRZ.com data", err, w)
return
}
log.Printf("Fetched QRZ.com data, %d records", qrzResponse.Count)
qrzAdi, err := adifToProto(qrzResponse.Adif, time.Now())
if err != nil {
writeError(500, "Failed parsing QRZ.com data", err, w)
log.Printf("QRZ.com payload: %v", base64.StdEncoding.EncodeToString([]byte(qrzResponse.Adif)))
return
}
if isFixCase {
for _, qso := range qrzAdi.Qsos {
fixCase(qso)
}
}
fsContacts, err := fb.GetContacts()
if err != nil {
writeError(500, "Error fetching contacts from firestore", err, w)
return
}
created, modified, noDiff := fb.MergeQsos(fsContacts, qrzAdi)
var report = map[string]int{}
report["qrz"] = len(qrzAdi.Qsos)
report["firestore"] = len(fsContacts)
report["created"] = created
report["modified"] = modified
report["noDiff"] = noDiff
log.Printf("report: %v", report)
marshal, _ := json.Marshal(report)
_, _ = fmt.Fprint(w, string(marshal))
}