From e953e9b5450cfc503a3413d895bbde54ff7298e3 Mon Sep 17 00:00:00 2001 From: scotow Date: Sun, 19 Jun 2022 14:05:14 +0200 Subject: [PATCH] Move to US servey site and add static code generator --- code.go | 28 +++++++++++++++++----------- code_static.go | 39 +++++++++++++++++++++++++++++++++++++++ meal.go | 16 +++++++++------- 3 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 code_static.go diff --git a/code.go b/code.go index c348b17..93f62b5 100644 --- a/code.go +++ b/code.go @@ -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" @@ -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 ) @@ -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 @@ -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 @@ -226,9 +229,10 @@ 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) } @@ -236,12 +240,14 @@ func buildStartRequest(action string) (req *http.Request, err error) { 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) } diff --git a/code_static.go b/code_static.go new file mode 100644 index 0000000..fa472f8 --- /dev/null +++ b/code_static.go @@ -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 +} \ No newline at end of file diff --git a/meal.go b/meal.go index 805e66d..f4f11fd 100644 --- a/meal.go +++ b/meal.go @@ -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())) @@ -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)))), } }