-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-places.go
84 lines (69 loc) · 1.97 KB
/
google-places.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"golang.org/x/net/context"
"googlemaps.github.io/maps"
)
type GooglePlaceReview struct {
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
Rating int `json:"rating"`
Text string `json:"text"`
Time int `json:"time"`
}
type GooglePlaceReviews struct {
Reviews []*GooglePlaceReview `json:"reviews"`
}
func SearchGoogleReviews(args *SummaryForm, conf *ApiKeys) (string, *GooglePlaceReviews, error) { // get 5 reviews for a business
c, err := maps.NewClient(maps.WithAPIKey(conf.GoogleMaps.Key))
if err != nil {
fmt.Println(err)
}
fmt.Println(args)
r := &maps.NearbySearchRequest{
Location: &maps.LatLng{
Lat: args.Latitude,
Lng: args.Longitude,
},
Keyword: args.Name,
RankBy: "distance",
}
resp, err := c.NearbySearch(context.Background(), r)
if err != nil {
fmt.Println(err)
}
if len(resp.Results) > 0 {
matchedPlace := resp.Results[0]
fmt.Println("looking for: " + args.Name + " found: " + matchedPlace.Name)
r2 := &maps.PlaceDetailsRequest{
PlaceID: matchedPlace.PlaceID,
}
details, err := c.PlaceDetails(context.Background(), r2)
if err != nil {
fmt.Println(err)
}
gReviews := new(GooglePlaceReviews)
for x := range details.Reviews {
gReview := new(GooglePlaceReview)
gReview.AuthorName = details.Reviews[x].AuthorName
gReview.AuthorURL = details.Reviews[x].AuthorURL
gReview.Rating = details.Reviews[x].Rating
gReview.Text = details.Reviews[x].Text
gReview.Time = details.Reviews[x].Time
gReviews.Reviews = append(gReviews.Reviews, gReview)
}
reviews := ""
for x := range details.Reviews {
reviews += details.Reviews[x].Text + " . "
}
return reviews, gReviews, nil
}
return "", nil, err
}
func GetReviews(c *maps.Client, b *maps.PlaceDetailsRequest) ([]maps.PlaceReview, error) { // get reviews for a business
resp, err := c.PlaceDetails(context.Background(), b)
if err != nil {
fmt.Println(err)
}
return resp.Reviews, err
}