-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
338 lines (281 loc) · 9.33 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"smiles/model"
"sort"
"strconv"
"sync"
"time"
"github.com/schollz/progressbar/v3"
)
// input parameters
var (
departureDateStr = "2022-09-10" // primer día para la ida
returnDateStr = "2022-09-20" // primer día para la vuelta
originAirportCode = "EZE" // aeropuerto de origen
destinationAirportCode = "PUJ" // aeropuerto de destino
daysToQuery = 1 // días corridos para buscar ida y vuelta
)
const (
// only used for dev
readFromFile = false
useCommandLineArguments = true
mockResponseFilePath = "data/response.json"
dateLayout = "2006-01-02"
bigMaxMilesNumber = 9_999_999
)
func main() {
if useCommandLineArguments {
if len(os.Args) != 6 {
fmt.Println("Forma de Uso: Origen Destino Fecha Ida Fecha Vuelta Cantidad de días a consultar")
fmt.Println("Ejemplo: smiles BUE PUJ 2023-01-10 2023-01-20 5")
os.Exit(1)
}
validateParameters()
}
c := http.Client{}
startingDepartureDate, err := time.Parse(dateLayout, departureDateStr)
startingReturningDate, err := time.Parse(dateLayout, returnDateStr)
if err != nil {
log.Fatal("Error parsing starting date")
}
fmt.Printf("Primer día de búsqueda para la ida: %s\n", departureDateStr)
fmt.Printf("Primer día de búsqueda para la vuelta: %s\n", returnDateStr)
fmt.Printf("Desde: %s\n", originAirportCode)
fmt.Printf("Hasta: %s\n", destinationAirportCode)
departuresCh := make(chan model.Result, daysToQuery)
returnsCh := make(chan model.Result, daysToQuery)
bar := progressbar.NewOptions(daysToQuery*2,
progressbar.OptionSetDescription("Consultando vuelos en las fechas y tramos seleccionados.."),
progressbar.OptionSetWidth(40),
progressbar.OptionSetRenderBlankState(true),
)
start := time.Now()
var wg sync.WaitGroup
for i := 0; i < daysToQuery; i++ {
departureDate := startingDepartureDate.AddDate(0, 0, i)
returnDate := startingReturningDate.AddDate(0, 0, i)
wg.Add(2)
go makeRequest(&wg, departuresCh, &c, departureDate, originAirportCode, destinationAirportCode, bar)
// inverting airports and changing date to query returns
go makeRequest(&wg, returnsCh, &c, returnDate, destinationAirportCode, originAirportCode, bar)
}
wg.Wait()
close(departuresCh)
close(returnsCh)
elapsed := time.Since(start).Round(time.Second).String()
fmt.Printf("\nLas consultas tomaron %s\n", elapsed)
var departureResults []model.Result
var returnResults []model.Result
for elem := range departuresCh {
departureResults = append(departureResults, elem)
}
for elem := range returnsCh {
returnResults = append(returnResults, elem)
}
sortResults(departureResults)
sortResults(returnResults)
fmt.Println("VUELOS DE IDA")
processResults(&c, departureResults)
fmt.Println("VUELOS DE VUELTA")
processResults(&c, returnResults)
}
func sortResults(r []model.Result) {
sort.Slice(r, func(i, j int) bool {
return r[i].QueryDate.Before(r[j].QueryDate)
})
}
func makeRequest(wg *sync.WaitGroup, ch chan<- model.Result, c *http.Client, startingDate time.Time, originAirport string, destinationAirport string, bar *progressbar.ProgressBar) {
defer wg.Done()
defer bar.Add(1)
var body []byte
var err error
data := model.Data{}
u := createURL(startingDate.Format(dateLayout), originAirport, destinationAirport) // Encode and assign back to the original query.
req := createRequest(u, "api-air-flightsearch-prd.smiles.com.br")
//fmt.Println("Making request with URL: ", req.URL.String())
//fmt.Printf("Consultando %s - %s para el día %s \n", originAirport, destinationAirport, startingDate.Format(dateLayout))
// only for dev purposes
if readFromFile {
fmt.Println("Reading from file ", mockResponseFilePath)
body, err = os.ReadFile(mockResponseFilePath)
if err != nil {
log.Fatal("error reading file")
}
} else {
res, err := c.Do(req)
if err != nil {
log.Fatal("Error making request ", err)
}
body, err = ioutil.ReadAll(res.Body)
if body == nil {
log.Fatal("Empty result")
}
}
if err := json.Unmarshal(body, &data); err != nil {
log.Fatal("Error unmarshalling data ", err)
}
ch <- model.Result{Data: data, QueryDate: startingDate}
}
func createRequest(u url.URL, authority string) *http.Request {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
log.Fatal("Error creating request ", err)
}
// headers
req.Header.Add("x-api-key", "aJqPU7xNHl9qN3NVZnPaJ208aPo2Bh2p2ZV844tw")
req.Header.Add("region", "ARGENTINA")
req.Header.Add("origin", "https://www.smiles.com.ar")
req.Header.Add("referer", "https://www.smiles.com.ar")
req.Header.Add("channel", "web")
req.Header.Add("authority", authority)
return req
}
func createURL(departureDate string, originAirport string, destinationAirport string) url.URL {
u := url.URL{
Scheme: "https",
Host: "api-air-flightsearch-prd.smiles.com.br",
RawQuery: "adults=1&cabinType=all&children=0¤cyCode=ARS&infants=0&isFlexibleDateChecked=false&tripType=2&forceCongener=true&r=ar",
Path: "/v1/airlines/search",
}
q := u.Query()
q.Add("departureDate", departureDate)
q.Add("originAirportCode", originAirport)
q.Add("destinationAirportCode", destinationAirport)
u.RawQuery = q.Encode()
return u
}
func createTaxURL(departureFlight *model.Flight, departureFare *model.Fare) url.URL {
u := url.URL{
Scheme: "https",
Host: "api-airlines-boarding-tax-prd.smiles.com.br",
RawQuery: "adults=1&children=0&infants=0&highlightText=SMILES_CLUB",
Path: "/v1/airlines/flight/boardingtax",
}
q := u.Query()
q.Add("type", "SEGMENT_1")
q.Add("uid", departureFlight.UId)
q.Add("fareuid", departureFare.UId)
u.RawQuery = q.Encode()
return u
}
func getSmilesClubFare(f *model.Flight) *model.Fare {
for i, v := range f.FareList {
if v.FType == "SMILES_CLUB" {
return &f.FareList[i]
}
}
fmt.Println("WARN: SMILES_CLUB fare not fund")
// for the sake of simplicity returning ridiculous default big number when fare not found
return &model.Fare{Miles: bigMaxMilesNumber}
}
func validateParameters() {
originAirportCode = os.Args[1]
if len(originAirportCode) != 3 {
fmt.Fprintf(os.Stderr, "Error: El aeropuerto de origen %s no es válido\n", originAirportCode)
os.Exit(1)
}
destinationAirportCode = os.Args[2]
if len(destinationAirportCode) != 3 {
fmt.Fprintf(os.Stderr, "Error: El aeropuerto de destino %s no es válido\n", destinationAirportCode)
os.Exit(1)
}
departureDateStr = os.Args[3]
_, err := time.Parse(dateLayout, departureDateStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: La fecha de salida %s no es válida. %v \n", departureDateStr, err)
os.Exit(1)
}
returnDateStr = os.Args[4]
_, err = time.Parse(dateLayout, returnDateStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: La fecha de regreso %s no es válida. %v \n", returnDateStr, err)
os.Exit(1)
}
v, err := strconv.ParseInt(os.Args[5], 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: La cantidad de días %d no es válida. %v \n", v, err)
os.Exit(1)
}
if v > 10 {
fmt.Fprintf(os.Stderr, "Error: La cantidad de días no puede ser mayor a 10 \n")
os.Exit(1)
}
daysToQuery = int(v)
}
func processResults(c *http.Client, r []model.Result) {
// using the first flight as cheapest default
var cheapestFlight *model.Flight
cheapestFare := &model.Fare{
Miles: bigMaxMilesNumber,
}
// loop through all results
for _, v := range r {
var cheapestFlightDay *model.Flight
cheapestFareDay := &model.Fare{
Miles: bigMaxMilesNumber,
}
// loop through all flights by day
for _, f := range v.Data.RequestedFlightSegmentList[0].FlightList {
smilesClubFare := getSmilesClubFare(&f)
if cheapestFareDay.Miles > smilesClubFare.Miles {
cheapestFlightDay = &f
cheapestFareDay = smilesClubFare
}
}
if cheapestFare.Miles > cheapestFareDay.Miles {
cheapestFlight = cheapestFlightDay
cheapestFare = cheapestFareDay
}
if cheapestFareDay.Miles != bigMaxMilesNumber {
fmt.Printf("Vuelo más barato del día %s: %s - %s, %s, %s, %d escalas, %d millas\n",
cheapestFlightDay.Departure.Date.Format(dateLayout),
cheapestFlightDay.Departure.Airport.Code,
cheapestFlightDay.Arrival.Airport.Code,
cheapestFlightDay.Cabin,
cheapestFlightDay.Airline.Name,
cheapestFlightDay.Stops,
cheapestFareDay.Miles,
)
}
}
fmt.Println()
if cheapestFare.Miles != bigMaxMilesNumber {
boardingTax := getTaxForFlight(c, cheapestFlight, cheapestFare)
fmt.Printf("Vuelo más barato en estas fechas: %s, %s - %s, %s, %s, %d escalas, %d millas, %f de Tasas e impuestos\n",
cheapestFlight.Departure.Date.Format(dateLayout),
cheapestFlight.Departure.Airport.Code,
cheapestFlight.Arrival.Airport.Code,
cheapestFlight.Cabin,
cheapestFlight.Airline.Name,
cheapestFlight.Stops,
cheapestFare.Miles,
boardingTax.Totals.Total.Money,
)
}
fmt.Println()
}
func getTaxForFlight(c *http.Client, flight *model.Flight, fare *model.Fare) *model.BoardingTax {
u := createTaxURL(flight, fare)
r := createRequest(u, "api-airlines-boarding-tax-prd.smiles.com.br")
var body []byte
var data model.BoardingTax
res, err := c.Do(r)
if err != nil {
log.Fatal("Error making request ", err)
}
body, err = ioutil.ReadAll(res.Body)
if body == nil {
log.Fatal("Empty result")
}
if err := json.Unmarshal(body, &data); err != nil {
log.Fatal("Error unmarshalling data ", err)
}
return &data
}