Skip to content

Commit

Permalink
Group api arguments into structs
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox committed Jul 29, 2023
1 parent 8e7c426 commit 4c48272
Show file tree
Hide file tree
Showing 13 changed files with 691 additions and 504 deletions.
131 changes: 80 additions & 51 deletions examples/example1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,98 @@ import (
func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities []string, lang language.Tag) {
session := flights.New()

for _, s := range srcCities {
for _, d := range dstCities {
offers, err := session.GetPriceGraph(
rangeStartDate, rangeEndDate,
[]string{s}, []string{}, []string{d}, []string{},
1, currency.PLN, flights.AnyStops, flights.Economy, flights.RoundTrip,
lang, tripLength)
if err != nil {
log.Fatal(err)
args := flights.Args{
Adults: 1,
Curr: currency.PLN,
Stops: flights.AnyStops,
Class: flights.Economy,
TripType: flights.RoundTrip,
Lang: lang,
}

priceGraphOffers, err := session.GetPriceGraph(
flights.PriceGraphArgs{
RangeStartDate: rangeStartDate,
RangeEndDate: rangeEndDate,
TripLength: tripLength,
SrcCities: srcCities,
DstCities: dstCities,
Args: args,
},
)
if err != nil {
log.Fatal(err)
}

for _, priceGraphOffer := range priceGraphOffers {
offers, _, err := session.GetOffers(
flights.OffersArgs{
Date: priceGraphOffer.StartDate,
ReturnDate: priceGraphOffer.ReturnDate,
SrcCities: srcCities,
DstCities: dstCities,
Args: args,
},
)
if err != nil {
log.Fatal(err)
}

var bestOffer flights.FullOffer

for _, o := range offers {
if bestOffer.Price == 0 || o.Price < bestOffer.Price {
bestOffer = o
}
}

for _, o := range offers {
_, priceRange, err := session.GetOffers(
o.StartDate,
o.ReturnDate,
[]string{s},
[]string{},
[]string{d},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
}
_, priceRange, err := session.GetOffers(
flights.OffersArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcAirports: []string{bestOffer.SrcAirportCode},
DstAirports: []string{bestOffer.DstAirportCode},
Args: args,
},
)
if err != nil {
log.Fatal(err)
}
if priceRange == nil {
log.Fatal("missing priceRange")
}

if o.Price < priceRange.Low {
fmt.Printf("%s %s\n", o.StartDate, o.ReturnDate)
fmt.Printf("price %d\n", int(o.Price))
url, err := session.SerializeUrl(
o.StartDate,
o.ReturnDate,
[]string{s},
[]string{},
[]string{d},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}
if bestOffer.Price < priceRange.Low {
url, err := session.SerializeUrl(
flights.UrlArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcAirports: []string{bestOffer.SrcAirportCode},
DstAirports: []string{bestOffer.DstAirportCode},
Args: args,
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
fmt.Println(url)
}
}
}

func main() {
t := time.Now()

getCheapOffers(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
2,
[]string{"Berlin", "Prague"},
[]string{"Athens"},
language.English)
[]string{"Athens", "Rome"},
language.English,
)

fmt.Println(time.Since(t))
}
44 changes: 27 additions & 17 deletions examples/example2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
session := flights.New()
var bestOffer flights.Offer

args := flights.Args{
Adults: 1,
Curr: currency.PLN,
Stops: flights.AnyStops,
Class: flights.Economy,
TripType: flights.RoundTrip,
Lang: lang,
}

offers, err := session.GetPriceGraph(
rangeStartDate, rangeEndDate,
[]string{srcCity}, []string{}, []string{dstCity}, []string{},
1, currency.PLN, flights.AnyStops, flights.Economy, flights.RoundTrip,
lang, tripLength)
flights.PriceGraphArgs{
RangeStartDate: rangeStartDate,
RangeEndDate: rangeEndDate,
TripLength: tripLength,
SrcCities: []string{srcCity},
DstCities: []string{dstCity},
Args: args,
},
)
if err != nil {
log.Fatal(err)
}
Expand All @@ -32,18 +46,13 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
url, err := session.SerializeUrl(
bestOffer.StartDate,
bestOffer.ReturnDate,
[]string{srcCity},
[]string{},
[]string{dstCity},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
flights.UrlArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcCities: []string{srcCity},
DstCities: []string{dstCity},
Args: args,
},
)
if err != nil {
log.Fatal(err)
Expand All @@ -58,5 +67,6 @@ func main() {
2,
"Warsaw",
"Athens",
language.English)
language.English,
)
}
12 changes: 6 additions & 6 deletions flights/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func getCityReqData(city string) string {
return url.QueryEscape(fmt.Sprintf(`[[["H028ib","[\"%s\",[1,2,3,5,4],null,[1,1,1],1]",null,"generic"]]]`, city))
}

func (s *Session) doRequestCity(city string, lang language.Tag) (*http.Response, error) {
func (s *Session) doRequestCity(city string, Lang language.Tag) (*http.Response, error) {
requestURL := "https://www.google.com/_/TravelFrontendUi/data/batchexecute?rpcids=H028ib&source-path=%2Ftravel%2Fflights%2Fsearch&f.sid=-8421128425468344897&bl=boq_travel-frontend-ui_20230613.06_p0" +
"&hl=" + lang.String() +
"&hl=" + Lang.String() +
"&soc-app=162&soc-platform=1&soc-device=1&_reqid=444052&rt=c"

jsonBody := []byte(
Expand All @@ -44,12 +44,12 @@ func abbrCitySchema(city, abbrCity *string) *[][][][]interface{} {
return &[][][][]interface{}{{{{nil, nil, city, nil, abbrCity}}}}
}

func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
func (s *Session) AbbrCity(city string, Lang language.Tag) (string, error) {
if abbrCity, ok := s.Cities.Load(city); ok {
return abbrCity, nil
}

resp, err := s.doRequestCity(city, lang)
resp, err := s.doRequestCity(city, Lang)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -81,10 +81,10 @@ func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
return abbrCity, nil
}

func (s *Session) AbbrCities(cities []string, lang language.Tag) ([]string, error) {
func (s *Session) AbbrCities(cities []string, Lang language.Tag) ([]string, error) {
abbrCities := []string{}
for _, c := range cities {
sc, err := s.AbbrCity(c, lang)
sc, err := s.AbbrCity(c, Lang)
if err != nil {
return nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions flights/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package flights

// func (s *Session) getRawData(args OffersArgs) (string, error) {
// serSrcs, err := s.serializeFlightLocations(args.SrcCities, args.SrcAirports, args.Lang)
// if err != nil {
// return "", err
// }
// serDsts, err := s.serializeFlightLocations(args.DstCities, args.DstAirports, args.Lang)
// if err != nil {
// return "", err
// }

// serDate := args.Date.Format("2006-01-02")
// serReturnDate := args.ReturnDate.Format("2006-01-02")

// serAdults := serializeFlightAdults(args.Adults)
// serStops := serializeFlightStop(args.Stops)

// serClass := serializeFlightClass(args.Class)
// serTripType := serializeTripType(args.TripType)

// rawData := ""

// rawData += fmt.Sprintf(`[null,null,%d,null,[],%s,%s,null,null,null,null,null,null,[`,
// serTripType, serClass, serAdults)

// rawData += fmt.Sprintf(`[[[%s]],[[%s]],null,%s,[],[],\"%s\",null,[],[],[],null,null,[],3]`,
// serSrcs, serDsts, serStops, serDate)

// if args.TripType == RoundTrip {
// rawData += fmt.Sprintf(`,[[[%s]],[[%s]],null,%s,[],[],\"%s\",null,[],[],[],null,null,[],3]`,
// serDsts, serSrcs, serStops, serReturnDate)
// }

// return rawData, nil
// }
Loading

0 comments on commit 4c48272

Please sign in to comment.