-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp.go
162 lines (131 loc) · 3.71 KB
/
yelp.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
const ( // yelp parameters
Token_Url = "https://api.yelp.com/oauth2/token"
Post_Format = "application/x-www-form-urlencoded"
)
type ApiToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
}
type Business struct {
Rating float32 `json:"rating"`
Price string `json:"price"`
Id string `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Phone string `json:"phone"`
RatingCount int32 `json:"review_count"`
ImgUrl string `json:"image_url"`
Hours []struct {
HoursType string `json:"hours_type"`
Open []struct {
Day int32 `json:"day"`
Start string `json:"start"`
End string `json:"end"`
Overnight bool `json:"is_overnight"`
} `json:"open"`
} `json:"hours"`
IsClosed bool `json:"is_closed"`
Categories []struct {
Alias string `json:"alias"`
Title string `json:"title"`
} `json:"categories"`
Coordinates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
} `json:"coordinates"`
Analysis struct {
Emotion string `json:"emotion"`
} `json:"analysis"`
}
type YelpSearchResp struct {
Total int64 `json:"total"`
Businesses []Business `json:"businesses"`
}
type YelpReviewSearchResp struct {
Total int32 `json:"total"`
Reviews []struct {
Text string `json:"text"`
Url string `json:"url"`
Rating int32 `json:"rating"`
TimeCreated string `json:"time_created"`
User struct {
Name string `json:"name"`
ImageUrl string `json:"image_url"`
} `json:"user"`
} `json:"reviews"`
}
func GetApiToken(conf *ApiKeys) (*ApiToken, error) { // get credentials
form := url.Values{}
form.Add("grant_type", "client_credentials")
form.Add("client_id", conf.Yelp.Id)
form.Add("client_secret", conf.Yelp.Secret)
resp, err := PostId(Token_Url, Post_Format, form.Encode())
if err != nil {
panic(err)
}
cred, err := ParseApiToken(resp)
return cred, err
}
func PostId(url string, format string, args string) ([]byte, error) { // send api secret
argsBytes := bytes.NewBuffer([]byte(args))
resp, err := http.Post(url, format, argsBytes)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
return []byte(bodyBytes), err
}
func ParseApiToken(body []byte) (*ApiToken, error) { // retreive token
var c = new(ApiToken)
err := json.Unmarshal(body, &c)
return c, err
}
func MakeGet(url string, cred *ApiToken) (*http.Response, error) { // http get request
auth := cred.TokenType + " " + cred.AccessToken
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", auth)
res, err := client.Do(req)
return res, err
}
func YelpSearch(args string, cred *ApiToken) (*YelpSearchResp, error) { // yelp business search
addr := "https://api.yelp.com/v3/businesses/search" + "?" + args
resp, err := MakeGet(addr, cred)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var r = new(YelpSearchResp)
err_ := json.Unmarshal([]byte(bodyBytes), &r)
return r, err_
}
func YelpReviewSearch(id string, cred *ApiToken) (*YelpReviewSearchResp, error) { // yelp review search
addr := "https://api.yelp.com/v3/businesses/" + id + "/reviews"
resp, err := MakeGet(addr, cred)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var r = new(YelpReviewSearchResp)
err_ := json.Unmarshal([]byte(bodyBytes), &r)
return r, err_
}