-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
261 lines (230 loc) · 6.4 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright (C) 2022 Michael Ablassmeier <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"strconv"
"sync"
"github.com/jessevdk/go-flags"
"github.com/sirupsen/logrus"
)
type jsonLogin struct {
User string `json:"uid"`
Pass string `json:"pwd"`
}
type Flights struct {
Data []FlightInfo `json:"data"`
Success bool `json:"success"`
Message string `json:"message"`
}
type FlightInfo struct {
ID string `json:"idflight"`
DATE string `json:"FlightDate"`
TakeOff string `json:"takeofflocation"`
}
var Api = struct {
url string
token string
login string
flights string
}{
url: "https://de.dhv-xc.de/api/",
token: "xc/login/status",
login: "xc/login/login",
flights: "fli/flights?mine=1",
}
var Method = struct {
GET string
POST string
}{
GET: "GET",
POST: "POST",
}
type Options struct {
User string `short:"u" long:"user" description:"DHV-XC User name" required:"true"`
Pass string `short:"p" long:"pass" description:"DHV-XC User Password" required:"true"`
Dir string `short:"d" long:"dir" description:"Target directory" required:"true"`
List bool `short:"l" long:"list" description:"List flights only, do not download"`
ID int `short:"i" long:"id" description:"Download flight with specific ID only" default:"0"`
}
var client http.Client
var token string
func init() {
_, ok := os.LookupEnv("XC_DEBUG")
if ok {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
jar, err := cookiejar.New(nil)
if err != nil {
logrus.Fatalf("Got error while creating cookie jar %s", err.Error())
}
client = http.Client{Jar: jar}
}
func json_dumps(data interface{}) []byte {
payload, err := json.Marshal(data)
if err != nil {
logrus.Error("Cant dump json response:")
logrus.Fatal(err)
}
return payload
}
func json_load(data []byte) map[string]interface{} {
var result map[string]interface{}
err := json.Unmarshal([]byte(data), &result)
if err != nil {
logrus.Error("Cant load json response:")
logrus.Fatal(err)
}
return result
}
func json_loads(data []byte) []interface{} {
var result []interface{}
err := json.Unmarshal([]byte(data), &result)
if err != nil {
logrus.Error("Cant load json response:")
logrus.Fatal(err)
}
return result
}
func load_flights(data []byte) Flights {
var resp Flights
err := json.Unmarshal([]byte(data), &resp)
if err != nil {
logrus.Error("Cant load json response:")
logrus.Fatal(err)
}
return resp
}
func httpReq(url string, payload []byte, method string, token string) []byte {
logrus.Debug(url)
logrus.Debugf("Request: [%s]", string(payload))
var request *http.Request
if method == Method.POST {
request, _ = http.NewRequest(method, url, bytes.NewBuffer(payload))
} else {
request, _ = http.NewRequest(method, url, nil)
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
if token != "" {
logrus.Debug("setting token header")
request.Header.Set("X-CSRF-Token", token)
}
response, error := client.Do(request)
if error != nil {
logrus.Fatal(error)
}
body, _ := ioutil.ReadAll(response.Body)
logrus.Debugf("Response: [%s]", string(body))
cookie := fmt.Sprintf("%s", response.Cookies())
logrus.Debugf("Got cookie: [%s]", cookie)
return body
}
func success(resp map[string]interface{}) bool {
if _, ok := resp["success"]; ok {
return true
}
return false
}
func getToken(data jsonLogin) string {
body := httpReq(Api.url+Api.token, json_dumps(data), Method.GET, "")
resp := json_load(body)
if !success(resp) {
logrus.Fatalf("Unable to get token: [%s]", resp["message"])
}
logrus.Debug(resp)
meta := resp["meta"].(map[string]interface{})
logrus.Debug(meta["token"])
return fmt.Sprintf("%v", meta["token"])
}
func saveIgc(id string, targetdir string, date string) int {
makedir(fmt.Sprintf("%s/%s", targetdir, date))
igcurl := fmt.Sprintf("https://en.dhv-xc.de/flight/%s/igc", id)
igcdata := httpReq(igcurl, json_dumps(""), Method.GET, token)
f, _ := os.Create(fmt.Sprintf("%s/%s/%s.igc", targetdir, date, id))
logrus.Infof("Saving flight: [%s] to: [%s/%s/%s.igc]", id, targetdir, date, id)
f.Write(igcdata)
f.Close()
return 1
}
func makedir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, 0755); os.IsNotExist(err) {
logrus.Fatalf("Unable to create target dir: [%s]", err)
}
}
}
func main() {
var options Options
var parser = flags.NewParser(&options, flags.Default)
if _, err := parser.Parse(); err != nil {
switch flagsErr := err.(type) {
case flags.ErrorType:
if flagsErr == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
default:
os.Exit(1)
}
}
makedir(options.Dir)
data := jsonLogin{
User: options.User,
Pass: options.Pass,
}
token = getToken(data)
logrus.Infof("Got token: [%s]", token)
body := httpReq(Api.url+Api.login, json_dumps(data), Method.POST, token)
resp := json_load(body)
if !success(resp) {
logrus.Fatalf("Authentication failed: [%s]", resp["message"])
}
logrus.Info("Logged in")
bodyp := httpReq(Api.url+Api.flights, json_dumps(data), Method.GET, token)
flights := load_flights(bodyp)
var wg sync.WaitGroup
var saved int = 0
for i := 0; i < len(flights.Data); i++ {
if options.List {
logrus.Infof("Flight ID: [%s] Takeoff: [%s] Date: [%s]",
flights.Data[i].ID,
flights.Data[i].TakeOff,
flights.Data[i].DATE,
)
continue
}
has_id, _ := strconv.Atoi(flights.Data[i].ID)
if options.ID != 0 && has_id != options.ID {
saved += saveIgc(
fmt.Sprintf("%d", options.ID), options.Dir, "today",
)
break
}
wg.Add(1)
go func(id string, date string) {
defer wg.Done()
saved += saveIgc(id, options.Dir, date)
}(flights.Data[i].ID, flights.Data[i].DATE)
}
wg.Wait()
logrus.Infof("Saved [%d] flights", saved)
}