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
/
fill_from_qrz.go
115 lines (106 loc) · 2.95 KB
/
fill_from_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package forester
import (
"cloud.google.com/go/firestore"
"cloud.google.com/go/pubsub"
"context"
"encoding/json"
"fmt"
adifpb "github.com/k0swe/adif-json-protobuf/go"
"github.com/k0swe/qrz-api"
"log"
"strconv"
"strings"
)
// FillNewQsoFromQrz listens to Pub/Sub for new contacts in Firestore, and fills
// in missing QSO details for the contacted station from QRZ.com.
func FillNewQsoFromQrz(ctx context.Context, m pubsub.Message) error {
var psMap map[string]string
err := json.Unmarshal(m.Data, &psMap)
if err != nil {
return err
}
logbookID := psMap["logbookId"]
contactID := psMap["contactId"]
firebasePath := fmt.Sprintf("logbooks/%s/contacts/%s", logbookID, contactID)
log.Printf("Got a new Firebase QSO at path %s", firebasePath)
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
return err
}
defer client.Close()
doc := client.Doc(firebasePath)
snapshot, err := doc.Get(ctx)
if err != nil {
return err
}
qso, err := ParseFirestoreQso(snapshot)
if err != nil {
return err
}
contactedStationCall := qso.qsopb.ContactedStation.StationCall
if contactedStationCall == "T3ST" {
log.Printf("Contacted station is special value T3ST; aborting lookup")
return nil
}
qrzUser, qrzPass, err := getQrzCreds(ctx, logbookID)
if err != nil {
return err
}
log.Printf("Querying QRZ.com for %v", contactedStationCall)
lookupResp, err := qrz.Lookup(ctx, &qrzUser, &qrzPass, &contactedStationCall)
if err != nil {
return err
}
log.Printf("QRZ.com lookup: %v is %v %v",
lookupResp.Callsign.Call, lookupResp.Callsign.Fname, lookupResp.Callsign.Name)
station := qrzLookupToStation(lookupResp.Callsign)
q := adifpb.Qso{ContactedStation: &station, LoggingStation: &adifpb.Station{}}
fixCase(&q)
mergeQso(qso.qsopb, &q)
j, err := qsoToJSON(qso.qsopb)
if err != nil {
return err
}
_, err = doc.Set(ctx, j)
if err != nil {
return err
}
log.Printf("Updated contact with QRZ.com details")
return nil
}
func getQrzCreds(ctx context.Context, logbookID string) (string, string, error) {
secretStore := NewSecretStore(ctx)
username, err := secretStore.FetchSecret(logbookID, qrzUsername)
if err != nil {
return "", "", err
}
password, err := secretStore.FetchSecret(logbookID, qrzPassword)
if err != nil {
return "", "", err
}
return username, password, nil
}
func qrzLookupToStation(c qrz.Callsign) adifpb.Station {
dxcc, _ := strconv.ParseUint(c.Dxcc, 10, 32)
cq, _ := strconv.ParseUint(c.Cqzone, 10, 32)
itu, _ := strconv.ParseUint(c.Ituzone, 10, 32)
return adifpb.Station{
StationCall: c.Call,
OpName: strings.TrimSpace(c.Fname + " " + c.Name),
GridSquare: c.Grid,
Latitude: c.Lat,
Longitude: c.Lon,
QslVia: c.Qslmgr,
Street: c.Addr1,
City: c.Addr2,
PostalCode: c.Zip,
County: c.County,
State: c.State,
Country: c.Country,
Dxcc: uint32(dxcc),
Email: c.Email,
CqZone: uint32(cq),
ItuZone: uint32(itu),
Iota: c.Iota,
}
}