Skip to content

Commit

Permalink
Move to US servey site and add static code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
scotow committed Jun 19, 2022
1 parent a7f0f0b commit e953e9b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
28 changes: 17 additions & 11 deletions code.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

const (
baseURL = "https://www.bkvousecoute.fr"
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"

startBodyClass = "CookieSplashPage"
entryBodyClass = "CouponEntryPage"
storeIdBodyClass = "CouponEntry_StorePage"
visitAreaDatetimeBodyClass = "CouponEntry_SimpleCode"
finishBodyClass = "Finish"

surveyEntryId = "surveyEntryForm"
Expand All @@ -30,17 +30,20 @@ const (
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"
surveyCodeKey = "SurveyCode"
dayKey = "InputDay"
monthKey = "InputMonth"
yearKey = "InputYear"
hourKey = "InputHour"
minuteKey = "InputMinute"
meridianKey = "InputMeridian"
locationKey = "BKLocation"

requiredRequests = 19
)
Expand Down Expand Up @@ -110,12 +113,12 @@ func GenerateCode(meal *Meal) (code string, err error) {
break
}

if body.HasClass(startBodyClass) {
req, err = buildStartRequest(nextAction)
if body.HasClass(storeIdBodyClass) {
req, err = buildStartRequest(nextAction, meal.Restaurant)
if err != nil {
break
}
} else if body.HasClass(entryBodyClass) {
} else if body.HasClass(visitAreaDatetimeBodyClass) {
req, err = buildEntryRequest(nextAction, meal)
if err != nil {
break
Expand Down Expand Up @@ -181,7 +184,7 @@ func parseIndex(doc *goquery.Document) (index string, err error) {
}

func parseCode(doc *goquery.Document) (code string, err error) {
parts := strings.Split(doc.Find("."+codeClass).Text(), " : ")
parts := strings.Split(doc.Find("."+codeClass).Text(), ": ")
if len(parts) != 2 {
err = ErrInvalidCode
return
Expand Down Expand Up @@ -226,22 +229,25 @@ func buildCommonRequest(action string, data *url.Values) (req *http.Request, err
return
}

func buildStartRequest(action string) (req *http.Request, err error) {
func buildStartRequest(action string, restaurant int) (req *http.Request, err error) {
data := commonParams()
data.Set(fipKey, fipValue)
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(surveyCodeKey, strconv.Itoa(meal.Restaurant))
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)
}
Expand Down
39 changes: 39 additions & 0 deletions code_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package burgoking

import (
"fmt"
"time"
"math/rand"
)

func GenerateCodeStatic(meal *Meal) (code string, err error) {
var prefix string
switch time.Now().Month() {
case time.January:
prefix = "BB"
case time.February:
prefix = "LS"
case time.March:
prefix = "JH"
case time.April:
prefix = "PL"
case time.May:
prefix = "BK"
case time.June:
prefix = "WH"
case time.July:
prefix = "FF"
case time.August:
prefix = "BF"
case time.September:
prefix = "CF"
case time.October:
prefix = "CK"
case time.November:
prefix = "CB"
case time.December:
prefix = "VM"
}
source = rand.New(rand.NewSource(time.Now().UnixNano()))
return fmt.Sprintf("%s%d", prefix, 10_000 + source.Intn(89_999)), nil
}
16 changes: 9 additions & 7 deletions meal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (

var (
RestaurantCodes = []int{
22365, // Auchan Leers - Leers
23911, // Auchan v2 - Villeneuve d'Ascq
21109, // Euralille - Lille
19974, // Gare Saint-Lazare - Paris
24191, // Zone Commerciale Grand Tour 2 - Sainte-Eulalie
22118, // Zone Commerciale de l'Épinette - Seclin
// 22365, // Auchan Leers - Leers
// 23911, // Auchan v2 - Villeneuve d'Ascq
// 21109, // Euralille - Lille
// 19974, // Gare Saint-Lazare - Paris
// 24191, // Zone Commerciale Grand Tour 2 - Sainte-Eulalie
// 22118, // Zone Commerciale de l'Épinette - Seclin

1677, // Tyler - Texas
}

source = rand.New(rand.NewSource(time.Now().UnixNano()))
Expand All @@ -26,6 +28,6 @@ type Meal struct {
func RandomMeal() *Meal {
return &Meal{
RestaurantCodes[rand.Intn(len(RestaurantCodes))],
time.Now().UTC().Add(-24 * time.Hour).Truncate(24 * time.Hour).Add(11*time.Hour + time.Duration(source.Int63n(int64(3*time.Hour)))),
time.Now().UTC().Add(-24 * time.Hour).Truncate(24 * time.Hour).Add(6*time.Hour + time.Duration(source.Int63n(int64(3*time.Hour)))),
}
}

0 comments on commit e953e9b

Please sign in to comment.