-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrz.go
167 lines (144 loc) · 3.56 KB
/
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package tasks
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/bbathe/golog/adif"
"github.com/bbathe/golog/util"
"github.com/bbathe/golog/config"
"github.com/bbathe/golog/models/qso"
)
var muxQrzUpload sync.Mutex
// QSLQrz uploads all QSOs to QRZ.com that are older than config.QSLDelay minutes old
func QSLQrz() error {
muxQrzUpload.Lock()
defer muxQrzUpload.Unlock()
qsos, err := qso.FindQSLsToSend(qso.QSLQrz, config.LogbookServices.QSLDelay)
if err != nil {
log.Printf("%+v", err)
return err
}
if len(qsos) > 0 {
err = uploadQSOsToQRZ(qsos)
if err != nil {
log.Printf("%+v", err)
return err
}
}
return nil
}
// QSLQrzFinal uploads all QSOs to QRZ.com regardless of how old they are
func QSLQrzFinal() {
muxQrzUpload.Lock()
defer muxQrzUpload.Unlock()
qsos, err := qso.FindQSLsToSend(qso.QSLQrz, 0)
if err != nil {
log.Printf("%+v", err)
return
}
if len(qsos) > 0 {
err = uploadQSOsToQRZ(qsos)
if err != nil {
log.Printf("%+v", err)
return
}
}
}
// uploadQSOsToQRZ uploads qsos to QRZ.com
func uploadQSOsToQRZ(qsos []qso.QSO) error {
// save all the qsos we are uploading to file
fname := filepath.Join(config.WorkingDirectory, "QRZ-"+time.Now().UTC().Format("2006-Jan-02_15-04-05")+".adif")
// write qsos as adif to file
err := adif.WriteToFile(qsos, fname)
if err != nil {
log.Printf("%+v", err)
return err
}
i := 0
for _, q := range qsos {
if i > 0 {
// pause between uploads
time.Sleep(1 * time.Second)
}
i++
// set the required form fields
formData := url.Values{}
s, err := adif.QSOToADIFRecord(q)
if err != nil {
log.Printf("%+v", err)
return err
}
formData.Set("ADIF", s)
formData.Set("KEY", config.LogbookServices.QRZ.APIKey)
formData.Set("ACTION", "INSERT")
formData.Set("OPTION", "REPLACE")
// setup request
reqBody := formData.Encode()
req, err := http.NewRequest("POST", "https://logbook.qrz.com/api", strings.NewReader(reqBody))
if err != nil {
log.Printf("%+v", err)
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(reqBody)))
// do POST
client := http.Client{
Timeout: 5 * time.Minute,
}
resp, err := client.Do(req)
if err != nil {
log.Printf("%+v", err)
return err
}
defer resp.Body.Close()
// get response body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("%+v", err)
return err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("returned bad statuscode")
log.Printf("%+v", err)
log.Printf("StatusCode: %d", resp.StatusCode)
log.Printf("Header: %s", resp.Header)
log.Printf("Body: %s", string(respBody))
return err
}
// parse response to see if it was really a success
m, err := url.ParseQuery(string(respBody))
if err != nil {
log.Printf("%+v", err)
return err
}
if m.Get("RESULT") == "FAIL" {
err := fmt.Errorf("api returned bad status")
log.Printf("%+v", err)
log.Printf("StatusCode: %d", resp.StatusCode)
log.Printf("Header: %s", resp.Header)
log.Printf("Return Values: %+v", m)
log.Printf("Body: %s", string(respBody))
return err
}
// set as sent in db
err = qso.UpdateQSLsToSent([]qso.QSO{q}, qso.QSLQrz)
if err != nil {
log.Printf("%+v", err)
return err
}
}
// only keep the last 5 files of ours in the working directory
err = util.DeleteHistoricalFiles(5, config.WorkingDirectory, "QRZ-", ".adif")
if err != nil {
log.Printf("%+v", err)
return err
}
return nil
}