can search
This commit is contained in:
59
cmd/main.go
59
cmd/main.go
@@ -2,14 +2,21 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/time/rate"
|
||||||
"googlemaps.github.io/maps"
|
"googlemaps.github.io/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main(ctx context.Context) error {
|
func Run(ctx context.Context) error {
|
||||||
|
rps := 2
|
||||||
|
limit := 100
|
||||||
|
|
||||||
|
limiter := rate.NewLimiter(rate.Limit(rps), 1)
|
||||||
|
|
||||||
c, err := maps.NewClient(maps.WithAPIKey(os.Getenv("GOOGLE_PLACES_API_KEY")))
|
c, err := maps.NewClient(maps.WithAPIKey(os.Getenv("GOOGLE_PLACES_API_KEY")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -22,23 +29,41 @@ func Main(ctx context.Context) error {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
} else if len(resp.Results) < 1 {
|
||||||
|
return fmt.Errorf("no results for %q", os.Args[1])
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%+v", resp)
|
origin := resp.Results[0].Geometry.Location
|
||||||
|
|
||||||
/*
|
type Result struct {
|
||||||
resp, err := c.NearbySearch(ctx, &maps.NearbySearchRequest{
|
Name string
|
||||||
Location: &maps.LatLng{},
|
Lat float64
|
||||||
Radius: uint(0),
|
Lng float64
|
||||||
Keyword: os.Args[1],
|
}
|
||||||
Name: "",
|
results := []Result{}
|
||||||
OpenNow: false,
|
var nextToken string
|
||||||
|
for len(results) < limit {
|
||||||
|
limiter.Wait(ctx)
|
||||||
|
resp, err := c.TextSearch(ctx, &maps.TextSearchRequest{
|
||||||
|
Query: os.Args[2],
|
||||||
|
Location: &origin,
|
||||||
|
Radius: uint(50),
|
||||||
|
PageToken: nextToken,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nextToken = resp.NextPageToken
|
||||||
|
for _, result := range resp.Results {
|
||||||
|
results = append(results, Result{
|
||||||
|
Name: result.Name,
|
||||||
|
Lat: result.Geometry.Location.Lat,
|
||||||
|
Lng: result.Geometry.Location.Lng,
|
||||||
})
|
})
|
||||||
if err != nil {
|
}
|
||||||
return err
|
log.Printf("%d...", len(resp.Results))
|
||||||
}
|
}
|
||||||
|
b, _ := json.Marshal(results)
|
||||||
|
fmt.Printf("%s\n", b)
|
||||||
|
|
||||||
return fmt.Errorf("%+v", resp)
|
return ctx.Err()
|
||||||
*/
|
|
||||||
|
|
||||||
return io.EOF
|
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -10,7 +10,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||||
defer can()
|
defer can()
|
||||||
if err := cmd.Main(ctx); err != nil {
|
if err := cmd.Run(ctx); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user