Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use other languages #7

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions examples/example1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ import (

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

func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities []string) {
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, tripLength, s, d, currency.PLN)
offers, err := session.GetPriceGraph(rangeStartDate, rangeEndDate, tripLength, s, d, currency.PLN, lang)
if err != nil {
log.Fatal(err)
}

for _, o := range offers {
_, priceRange, err := session.GetOffers(o.StartDate, o.ReturnDate, s, d, currency.PLN)
_, priceRange, err := session.GetOffers(o.StartDate, o.ReturnDate, s, d, currency.PLN, lang)
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(
url, err := session.SerializeUrl(
o.StartDate,
o.ReturnDate,
[]string{s},
Expand All @@ -40,6 +41,7 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
Expand All @@ -52,8 +54,11 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
}

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"})
getCheapOffers(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
2,
[]string{"Berlin", "Prague"},
[]string{"Athens"},
language.English)
}
19 changes: 12 additions & 7 deletions examples/example2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

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

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

offers, err := session.GetPriceGraph(rangeStartDate, rangeEndDate, tripLength, srcCities, dstCities, currency.PLN)
offers, err := session.GetPriceGraph(rangeStartDate, rangeEndDate, tripLength, srcCities, dstCities, currency.PLN, lang)
if err != nil {
log.Fatal(err)
}
Expand All @@ -26,7 +27,7 @@ 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 := flights.SerializeUrl(
url, err := session.SerializeUrl(
bestOffer.StartDate,
bestOffer.ReturnDate,
[]string{srcCities},
Expand All @@ -38,6 +39,7 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
Expand All @@ -46,8 +48,11 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
}

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")
getBestOffer(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
2,
"Warsaw",
"Athens",
language.English)
}
61 changes: 26 additions & 35 deletions flights/serialize_city.go → flights/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"golang.org/x/text/language"
)

func sendRequest(city string) (*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=pl&soc-app=162&soc-platform=1&soc-device=1&_reqid=444052&rt=c"
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() +
"&soc-app=162&soc-platform=1&soc-device=1&_reqid=444052&rt=c"

jsonBody := []byte(`f.req=%5B%5B%5B%22H028ib%22%2C%22%5B%5C%22` + city + `%5C%22%2C%5B1%2C2%2C3%2C5%2C4%5D%2Cnull%2C%5B1%2C1%2C1%5D%2C1%5D%22%2Cnull%2C%22generic%22%5D%5D%5D&at=AAuQa1qJpLKW2Hl-i40OwJyzmo22%3A1687083247610&`)
bodyReader := bytes.NewReader(jsonBody)
Expand All @@ -19,37 +23,14 @@ func sendRequest(city string) (*http.Response, error) {
if err != nil {
return nil, err
}
req.Header.Set("authority", "www.google.com")
req.Header.Set("accept", "*/*")
req.Header.Set("accept-language", "pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7,hr;q=0.6")
req.Header.Set("cache-control", "no-cache")
req.Header.Set("content-type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Set("cookie", "CONSENT=YES+srp.gws-20211208-0-RC2.pl+FX+371")
req.Header.Set("origin", "https://www.google.com")
req.Header.Set("cookie", `CONSENT=PENDING+672`)
req.Header.Set("pragma", "no-cache")
req.Header.Set("sec-ch-ua", "\"Google Chrome\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"")
req.Header.Set("sec-ch-ua-arch", "\"x86\"")
req.Header.Set("sec-ch-ua-bitness", "\"64\"")
req.Header.Set("sec-ch-ua-full-version", "\"113.0.5672.92\"")
req.Header.Set("sec-ch-ua-full-version-list", "\"Google Chrome\";v=\"113.0.5672.92\", \"Chromium\";v=\"113.0.5672.92\", \"Not-A.Brand\";v=\"24.0.0.0\"")
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-model", "")
req.Header.Set("sec-ch-ua-platform", "Linux")
req.Header.Set("sec-ch-ua-platform-version", "5.19.0")
req.Header.Set("sec-ch-ua-wow64", "?0")
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "same-origin")
req.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
req.Header.Set("x-goog-ext-190139975-jspb", "[\"PL\",\"ZZ\",\"Xkt6eg==\"]")
req.Header.Set("x-goog-ext-259736195-jspb", "[\"pl\",\"PL\",\"PLN\",1,null,[-120],null,[[48764690,48627726,47907128,48480739,48710756,48676280,48593234,48707378]],1,[]]")
req.Header.Set("x-same-domain", "1")

client := http.Client{
Timeout: 30 * time.Second,
}

return client.Do(req)
return s.client.Do(req)
}

func decodeInnerObject(body *bufio.Reader) ([][][][]interface{}, error) {
Expand All @@ -76,14 +57,16 @@ func decodeInnerObject(body *bufio.Reader) ([][][][]interface{}, error) {
return innerObject, nil
}

func AbbrCity(city string) (string, error) {
resp, err := sendRequest(city)
func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
resp, err := s.doRequestCity(city, lang)
if err != nil {
return "", err
}
defer resp.Body.Close()

body := bufio.NewReader(resp.Body)
body1, err := io.ReadAll(resp.Body)

body := bufio.NewReader(bytes.NewReader(body1))
skipPrefix(body)
readLine(body)

Expand All @@ -92,11 +75,19 @@ func AbbrCity(city string) (string, error) {
return "", fmt.Errorf("couldn't decode inner object: %w", err)
}

foundCity, ok := innerObject[0][0][0][2].(string)
if len(innerObject) <= 0 ||
len(innerObject[0]) <= 0 ||
len(innerObject[0][0]) <= 0 ||
len(innerObject[0][0][0]) <= 4 {
return "", fmt.Errorf("wrong inner object: %v", innerObject)
}
resCities := innerObject[0][0]

foundCity, ok := resCities[0][2].(string)
if !ok {
return "", fmt.Errorf("couldn't get city name")
}
serializedCity, ok := innerObject[0][0][0][4].(string)
serializedCity, ok := resCities[0][4].(string)
if !ok {
return "", fmt.Errorf("couldn't get serialized city name")
}
Expand All @@ -107,10 +98,10 @@ func AbbrCity(city string) (string, error) {
return serializedCity, nil
}

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

import (
"testing"

"golang.org/x/text/language"
)

const (
abbrA = "/m/0n2z"
abbrB = "/m/081m_"
)

func TestAbbrCityEN(t *testing.T) {
session := New()

cityA := "Athens"
cityB := "Warsaw"

a, err := session.AbbrCity(cityA, language.English)
if err != nil {
t.Fatal(err)
}
if a != abbrA {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrA, a)
}

w, err := session.AbbrCity(cityB, language.English)
if err != nil {
t.Fatal(err)
}
if w != abbrB {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrB, w)
}
}

func TestAbbrCityDE(t *testing.T) {
session := New()

cityA := "Athen"
cityB := "Warschau"

a, err := session.AbbrCity(cityA, language.German)
if err != nil {
t.Fatal(err)
}
if a != abbrA {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrA, a)
}

w, err := session.AbbrCity(cityB, language.German)
if err != nil {
t.Fatal(err)
}
if w != abbrB {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrB, w)
}
}

func TestAbbrCityPL(t *testing.T) {
session := New()

cityA := "Ateny"
cityB := "Warszawa"

a, err := session.AbbrCity(cityA, language.Polish)
if err != nil {
t.Fatal(err)
}
if a != abbrA {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrA, a)
}

b, err := session.AbbrCity(cityB, language.Polish)
if err != nil {
t.Fatal(err)
}
if b != abbrB {
t.Fatalf("wrong abbreviated city name, expected: %s received: %s", abbrB, b)
}
}
Loading