Skip to content

Commit d426b83

Browse files
committed
Fix for updated hotukdeals site + add "TAKE10XY" generated vouchers
1 parent 60d2c3a commit d426b83

File tree

2 files changed

+130
-43
lines changed

2 files changed

+130
-43
lines changed

dominos

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if [[ $# -ne 0 ]]; then
5050
fi
5151

5252
# The magic script.
53-
CMD="casperjs --ignore-ssl-errors=true --ssl-protocol=any dominos.js $POSTCODE"
53+
CMD="casperjs --verbose --ignore-ssl-errors=true --ssl-protocol=any dominos.js $POSTCODE"
5454

5555
# Execute the command, emailing output if address given.
5656
if [ "$EMAIL" != "" ]; then
@@ -62,7 +62,7 @@ if [ "$EMAIL" != "" ]; then
6262
*) subject="Script Error" ;;
6363
esac
6464

65-
echo "$output" | mail -a "Message-ID:<scripts@dave.desktop>" -s "Dominos Vouchers - $subject" $EMAIL;
65+
echo "$output" | mail -a "Message-ID:<scripts@telescuff>" -s "Dominos Vouchers - $subject" $EMAIL;
6666

6767
else
6868
$CMD

dominos.js

Lines changed: 128 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,140 @@ function capture(name) {
3636
});
3737
}
3838

39+
function reportErrors(fn) {
40+
return function () {
41+
try {
42+
return fn.apply(this, arguments);
43+
} catch (e) {
44+
console.log(e);
45+
throw e;
46+
}
47+
}
48+
}
49+
50+
51+
(reportErrors(function take10s() {
52+
var generated = [],
53+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
54+
i,j;
3955

56+
for (i = 0; i < alphabet.length; i++) {
57+
for (j = 0; j < alphabet.length; j++) {
58+
generated.push({
59+
code: "TAKE10" + alphabet[i] + alphabet[j], description: "Generated TAKE10 code"
60+
});
61+
}
62+
}
63+
64+
[].push.apply(codes, generated);
65+
66+
})());
4067

4168
function getCodes() {
42-
var pageCodes = this.getElementsAttribute("input.voucherReveal-peel-bottom-code", "value"),
43-
pageDescs = this.getElementsInfo("p.thread-title-text").map(function (info) {
44-
return info.text;
45-
}),
69+
var sections = _.compact(this.evaluate(function () {
70+
return $("article.thread--voucher").toArray().map(function (el) {
71+
return $(el).find("[data-voucher-button]").data() && $(el).attr("id") || null;
72+
});
73+
})),
74+
pageCodes = this.evaluate(function (sections) {
75+
return sections.map(function (id) {
76+
return {
77+
id: $("#"+id).find("[data-voucher-button]").data().voucherButton.id,
78+
79+
desc: $("#"+id).find("strong.thread-title").text().trim()
80+
.replace(/\s{2,}/g, " ").replace("INSTRUCTIONS: ", "")
81+
.replace(" Read more", "")
82+
};
83+
});
84+
}, sections),
4685
pageCodeData;
47-
if (pageCodes.length !== pageDescs.length) {
48-
this.die(
49-
"Error: pageCodes.length !== pageDescs.length: " +
50-
pageCodes.length + " !== " + pageDescs.length
51-
);
52-
}
5386

54-
casper.echo("Got codes: " + pageCodes);
55-
56-
_(pageCodes).zip(pageDescs).each(function (codeAndDesc) {
57-
var multiCodes = codeAndDesc[0].trim().split(" ");
58-
_(multiCodes).each(function (code) {
59-
if (code) {
60-
codes.push({
61-
code: code,
62-
description: codeAndDesc[1]
63-
});
64-
}
87+
_(pageCodes).each(function (pageCode) {
88+
89+
say("getting voucher for id " + pageCode.id);
90+
91+
casper.then(function () {
92+
this.evaluate(function (id) {
93+
$.cookie("show_voucher", id);
94+
}, pageCode.id);
95+
});
96+
97+
say("reloading to show popup");
98+
99+
casper.then(function () {
100+
this.reload();
101+
});
102+
103+
say("waiting for popup with code for id " + pageCode.id);
104+
105+
casper.waitForSelector("div.popover-content span.voucher-code");
106+
107+
say("scriaping popup with code for id " + pageCode.id);
108+
109+
casper.then(reportErrors(function () {
110+
var code = casper.getElementInfo("div.popover-content span.voucher-code").text;
111+
var multiCodes = code.trim().replace(/"/g, "").split(" ");
112+
_(multiCodes).each(function (code) {
113+
if (code) {
114+
codes.push({
115+
code: code,
116+
description: pageCode.desc
117+
});
118+
}
119+
});
120+
121+
this.echo(
122+
"popup with code for id " + pageCode.id + " is '" + code + "' with description '" +
123+
pageCode.desc + "'"
124+
);
125+
}));
126+
});
127+
128+
say("removing cookie");
129+
130+
casper.then(function () {
131+
this.evaluate(function () {
132+
$.removeCookie("show_voucher");
65133
});
66134
});
135+
136+
say("reloading to hide popup");
137+
138+
casper.then(function () {
139+
this.reload();
140+
});
67141
}
68142

69-
casper.start("http://www.hotukdeals.com/vouchers/dominos.co.uk");
143+
var pageNumSelector = "#pagination > nav > div > span.tGrid-cell.tGrid-cell--shrink" +
144+
".vAlign--all-m.space--h-4.space--v-1.text--color-brandPrimary > button > span.hide--toW2";
70145

71-
say("on voucher page");
146+
casper.start("https://www.hotukdeals.com/vouchers/dominos.co.uk");
72147

73-
for (currCodePage = 0; currCodePage < numCodePages; currCodePage++)
74-
{
75-
capture("Voucher code page " + (currCodePage + 1) + " of " + numCodePages);
148+
say("waiting for load");
76149

77-
casper.then(getCodes);
150+
casper.waitForSelector("[data-voucher-button]");
78151

79-
say("--- going to next page");
152+
casper.then(reportErrors(getCodes));
80153

81-
casper.thenClick("a.paginationButton--arrow-next");
154+
for (currCodePage = 1; currCodePage < numCodePages; currCodePage++)
155+
{
156+
capture("Voucher code page " + (currCodePage + 1) + " of " + numCodePages);
82157

83-
casper.waitForSelectorTextChange("span.paginationButton--current");
158+
casper.then(function () {
159+
say("going to next page");
160+
161+
if (casper.visible(pageNumSelector)) {
162+
casper.thenClick("a[rel='next']");
163+
casper.waitForSelectorTextChange(pageNumSelector);
164+
casper.then(reportErrors(getCodes));
165+
} else {
166+
casper.say("no more pages of codes, breaking early.");
167+
return;
168+
}
169+
});
84170
}
85171

172+
86173
casper.thenOpen("https://www.dominos.co.uk");
87174

88175
capture("Dominos homepage");
@@ -91,11 +178,11 @@ casper.waitForSelector("#store-finder-search");
91178

92179
say("searching for store");
93180

94-
casper.then(function () {
181+
casper.then(reportErrors(function () {
95182
this.fillSelectors("#store-finder-search", {
96183
"input[type='text']": POSTCODE
97184
});
98-
});
185+
}));
99186

100187
capture("find store");
101188

@@ -135,9 +222,9 @@ capture("enter vouchers");
135222

136223
say("entering codes...");
137224

138-
casper.then(function () {
225+
casper.then(reportErrors(function () {
139226

140-
codes.forEach(function (code) {
227+
codes.forEach(reportErrors(function (code) {
141228

142229
say("entering code: " + code.code);
143230

@@ -152,9 +239,9 @@ casper.then(function () {
152239
this.then(function () {
153240
code.status = this.fetchText("div.voucher-code-input > p.help-block").trim();
154241
});
155-
242+
156243
this.then(function () {
157-
// Clear the voucher, if it was successfully added, so subsequent vouchers can be
244+
// Clear the voucher, if it was successfully added, so subsequent vouchers can be
158245
// checked.
159246
if (this.exists("[data-voucher] .basket-product-actions button")) {
160247
say("voucher worked! clearing voucher");
@@ -167,20 +254,20 @@ casper.then(function () {
167254
this.waitWhileSelector("[data-voucher] .basket-product-actions button");
168255
}
169256
});
170-
}.bind(this));
171-
});
257+
}).bind(this));
258+
}));
172259

173260
capture("finish");
174261

175-
casper.then(function () {
262+
casper.then(reportErrors(function () {
176263
var working = codes.filter(function (code) {
177264
return !/invalid|expired|Voucher Used|already been used/i.test(code.status);
178265
}).map(function (code) {
179266
return code.description + " [" + code.code + "]";
180267
});
181268

182269
this.echo(codes.map(function (code) {
183-
return code.description + " [" + code.code + "] = \"" + code.status + "\"";
270+
return "[" + code.code + "] " + code.description + " = \"" + code.status + "\"";
184271
}).join("\n"));
185272

186273
this.echo("\n\n=======Working vouchers (" + working.length + "/" + codes.length + ")========");
@@ -192,6 +279,6 @@ casper.then(function () {
192279
} else {
193280
casper.exit(100);
194281
}
195-
});
282+
}));
196283

197284
casper.run();

0 commit comments

Comments
 (0)