From e8783c982dce722d7d26f6e1c8b6cb1e95e902f6 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 8 Mar 2026 20:45:47 -0600 Subject: [PATCH] can search --- cmd/main.go | 59 ++++++++++++++++++++++++++++++++++++++--------------- main.go | 2 +- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 761d96a..fbbe1d0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,14 +2,21 @@ package cmd import ( "context" + "encoding/json" "fmt" - "io" + "log" "os" + "golang.org/x/time/rate" "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"))) if err != nil { return err @@ -22,23 +29,41 @@ func Main(ctx context.Context) error { }) if err != nil { 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 - /* - resp, err := c.NearbySearch(ctx, &maps.NearbySearchRequest{ - Location: &maps.LatLng{}, - Radius: uint(0), - Keyword: os.Args[1], - Name: "", - OpenNow: false, + type Result struct { + Name string + Lat float64 + Lng float64 + } + results := []Result{} + 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 io.EOF + return ctx.Err() } diff --git a/main.go b/main.go index fc4d586..8566e1e 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( func main() { ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) defer can() - if err := cmd.Main(ctx); err != nil { + if err := cmd.Run(ctx); err != nil { panic(err) } }