-
Notifications
You must be signed in to change notification settings - Fork 4
/
code.go
260 lines (213 loc) · 5.55 KB
/
code.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package burgoking
import (
"errors"
"fmt"
"github.com/PuerkitoBio/goquery"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"crypto/tls"
)
const (
baseURL = "https://www.mybkexperience.com"
userAgentHeader, userAgent = "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
contentTypeHeader, contentType = "Content-Type", "application/x-www-form-urlencoded"
storeIdBodyClass = "CouponEntry_StorePage"
visitAreaDatetimeBodyClass = "CouponEntry_SimpleCode"
finishBodyClass = "Finish"
surveyEntryId = "surveyEntryForm"
surveyId = "surveyForm"
indexId = "IoNF"
codeClass = "ValCode"
formAttr = "action"
indexAttr = "value"
pKey, pValue = "P", "2"
fipKey, fipValue = "FIP", "True"
jsKey, jsValue = "JavaScriptEnabled", "1"
cookiesKey, cookiesValue = "AcceptCookies", "Y"
storeIdKey = "Initial_StoreID"
indexKey = "IoNF"
dayKey = "InputDay"
monthKey = "InputMonth"
yearKey = "InputYear"
hourKey = "InputHour"
minuteKey = "InputMinute"
meridianKey = "InputMeridian"
locationKey = "BKLocation"
requiredRequests = 19
)
var (
ErrInvalidAPIResponse = errors.New("invalid response from the website")
ErrFormNotFound = errors.New("cannot find form from the response")
ErrInvalidCode = errors.New("cannot parse code from the page")
ErrTooManyRequest = errors.New("too many requests")
ErrInvalidIndex = errors.New("cannot find index from value")
)
func GenerateCode(meal *Meal) (code string, err error) {
jar, err := cookiejar.New(nil)
if err != nil {
return
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Jar: jar,
}
if meal == nil {
meal = RandomMeal()
}
req, err := buildFirstRequest()
if err != nil {
return
}
var (
resp *http.Response
doc *goquery.Document
nextAction, index string
)
requestCount := 0
for {
resp, err = client.Do(req)
if err != nil {
break
}
if resp.StatusCode != http.StatusOK {
err = ErrInvalidAPIResponse
break
}
doc, err = buildDocument(resp)
if err != nil {
break
}
requestCount++
body := doc.Find("body")
if body.HasClass(finishBodyClass) {
code, err = parseCode(doc)
break
}
nextAction, err = parseAction(doc)
if err != nil {
break
}
if body.HasClass(storeIdBodyClass) {
req, err = buildStartRequest(nextAction, meal.Restaurant)
if err != nil {
break
}
} else if body.HasClass(visitAreaDatetimeBodyClass) {
req, err = buildEntryRequest(nextAction, meal)
if err != nil {
break
}
} else {
index, err = parseIndex(doc)
if err != nil {
break
}
req, err = buildSurveyRequest(nextAction, index)
if err != nil {
break
}
}
if requestCount >= requiredRequests {
err = ErrTooManyRequest
break
}
}
return
}
func addUserAgent(req *http.Request) {
req.Header.Set(userAgentHeader, userAgent)
}
func buildDocument(resp *http.Response) (doc *goquery.Document, err error) {
doc, err = goquery.NewDocumentFromReader(resp.Body)
err = resp.Body.Close()
if err != nil {
return
}
return
}
func parseAction(doc *goquery.Document) (nextAction string, err error) {
nextAction, exists := doc.Find("#" + surveyId).Attr(formAttr)
if exists {
return
}
nextAction, exists = doc.Find("#" + surveyEntryId).Attr(formAttr)
if exists {
return
}
err = ErrFormNotFound
return
}
func parseIndex(doc *goquery.Document) (index string, err error) {
index, exists := doc.Find("#" + indexId).Attr(indexAttr)
if !exists {
err = ErrInvalidIndex
}
return
}
func parseCode(doc *goquery.Document) (code string, err error) {
parts := strings.Split(doc.Find("."+codeClass).Text(), ": ")
if len(parts) != 2 {
err = ErrInvalidCode
return
}
code = parts[1]
return
}
func buildUrl(action string) string {
return fmt.Sprintf("%s/%s", baseURL, action)
}
func padTo2(value int) string {
return fmt.Sprintf("%02d", value)
}
func commonParams() *url.Values {
data := url.Values{}
data.Set(jsKey, jsValue)
data.Set(cookiesKey, cookiesValue)
return &data
}
func buildFirstRequest() (req *http.Request, err error) {
req, err = http.NewRequest(http.MethodGet, baseURL, nil)
if err != nil {
return
}
addUserAgent(req)
return
}
func buildCommonRequest(action string, data *url.Values) (req *http.Request, err error) {
req, err = http.NewRequest(http.MethodPost, buildUrl(action), strings.NewReader(data.Encode()))
if err != nil {
return
}
addUserAgent(req)
req.Header.Set(contentTypeHeader, contentType)
return
}
func buildStartRequest(action string, restaurant int) (req *http.Request, err error) {
data := commonParams()
data.Set(pKey, pValue)
data.Set(storeIdKey, strconv.Itoa(restaurant))
return buildCommonRequest(action, data)
}
func buildEntryRequest(action string, meal *Meal) (req *http.Request, err error) {
data := commonParams()
data.Set(fipKey, fipValue)
data.Set(pKey, pValue)
data.Set(dayKey, padTo2(meal.Date.Day()))
data.Set(monthKey, padTo2(int(meal.Date.Month())))
data.Set(yearKey, strconv.Itoa(meal.Date.Year())[2:])
data.Set(hourKey, padTo2(meal.Date.Hour()))
data.Set(minuteKey, padTo2(meal.Date.Minute()))
data.Set(locationKey, "TX")
data.Set(meridianKey, "PM")
return buildCommonRequest(action, data)
}
func buildSurveyRequest(action string, questionIndex string) (req *http.Request, err error) {
data := commonParams()
data.Set(indexKey, questionIndex)
return buildCommonRequest(action, data)
}