Skip to content

Commit

Permalink
Add examples (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox authored Jul 16, 2023
1 parent 2d56a18 commit a3916ed
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 61 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ jobs:
- name: Run unit tests
run: |
go test ./... -v
build:
name: Build examples
runs-on: ubuntu-22.04
steps:
- name: Set up toolchain
uses: actions/setup-go@v3
with:
go-version: 1.20.1
id: go

- name: Check out repository code
uses: actions/checkout@v3

- name: Build examples
run: |
go build ./examples/example1
go build ./examples/example2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/example1
/example2
59 changes: 59 additions & 0 deletions examples/example1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"log"
"time"

"github.com/krisukox/google-flights-api/flights"
"golang.org/x/text/currency"
)

func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities []string) {
session := flights.New()

for _, s := range srcCities {
for _, d := range dstCities {
offers, err := session.GetPriceGraph(rangeStartDate, rangeEndDate, tripLength, s, d, currency.PLN)
if err != nil {
log.Fatal(err)
}

for _, o := range offers {
_, priceRange, err := session.GetOffers(o.StartDate, o.ReturnDate, s, d, currency.PLN)
if err != nil {
log.Fatal(err)
}

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

func main() {
rangeStartDate, _ := time.Parse("2006-01-02", "2023-10-01")
rangeEndDate, _ := time.Parse("2006-01-02", "2023-10-30")

getCheapOffers(rangeStartDate, rangeEndDate, 2, []string{"Wrocław", "Katowice"}, []string{"Ateny"})
}
53 changes: 53 additions & 0 deletions examples/example2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"log"
"time"

"github.com/krisukox/google-flights-api/flights"
"golang.org/x/text/currency"
)

func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities string) {
session := flights.New()
var bestOffer flights.Offer

offers, err := session.GetPriceGraph(rangeStartDate, rangeEndDate, tripLength, srcCities, dstCities, currency.PLN)
if err != nil {
log.Fatal(err)
}

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

fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
url, err := flights.SerializeUrl(
bestOffer.StartDate,
bestOffer.ReturnDate,
[]string{srcCities},
[]string{},
[]string{dstCities},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}

func main() {
rangeStartDate, _ := time.Parse("2006-01-02", "2023-10-01")
rangeEndDate, _ := time.Parse("2006-01-02", "2023-10-30")

getBestOffer(rangeStartDate, rangeEndDate, 2, "Wrocław", "Ateny")
}
File renamed without changes.
File renamed without changes.
61 changes: 0 additions & 61 deletions main.go

This file was deleted.

0 comments on commit a3916ed

Please sign in to comment.