can search

This commit is contained in:
Bel LaPointe
2026-03-08 20:45:47 -06:00
parent cddb6f4280
commit e8783c982d
2 changed files with 43 additions and 18 deletions

View File

@@ -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()
}

View File

@@ -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)
}
}