-
Notifications
You must be signed in to change notification settings - Fork 25
/
deliveroo.js
103 lines (87 loc) · 3.94 KB
/
deliveroo.js
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
/* SCRIPT FOR RETRIEVING DELIVEROO FOOD OPTIONS ~ TEBEL.SG */
var casper = require('casper').create({
verbose: false,
logLevel: 'debug',
waitTimeout: 30000,
viewportSize: {
width: 1366,
height: 768
},
pageSettings: {
"loadImages": true,
"loadPlugins": true,
"localToRemoteUrlAccessEnabled": false,
"webSecurityEnabled": true,
"ignoreSslErrors": false
// "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10'
}
});
var x = require('casper').selectXPath;
casper.start('https://deliveroo.com.sg/', function() {
// this.echo(this.getHTML());
// this.echo('LOGGING IN: ' + this.getTitle());
});
casper.then(function() {
// this.echo(this.getHTML());
this.click(x('//*[@class="page-header--nav-link page-header--button"]'));
this.sendKeys(x('//*[@id="email"]'), '[email protected]');
this.sendKeys(x('//*[@id="password"]'), '12345678');
this.click(x('//*[@class="page-header-login--login"]'));
});
casper.waitForSelector(x('//*[@class="page-header--nav-link icon-user"]'), function() {
// this.echo(this.getHTML());
if (casper.cli.has(0))
{this.sendKeys(x('//*[@name="postcode"]'), casper.cli.raw.get(0).toString());}
else
{this.sendKeys(x('//*[@name="postcode"]'), '608838');}
this.click(x('//*[@id="find_food"]'));
casper.wait(10000, function() {
this.capture('deliveroo.png');
var fs = require('fs'); fs.write('deliveroo.txt', '', 'w');
// if block to handle 2 different Deliveroo GUIs showing up randomly
if (this.exists(x('//*[@id="restaurants-deliverycount"]/h1')))
{
var sLocation = this.fetchText(x('//*[@id="restaurants-deliverycount"]/h1'));
var sTime = this.fetchText(x('//*[@class="restaurants--controls"]//*[@id="delivery_time"]//*[@selected]'));
if ((sLocation.length > 0) && (sTime.length > 0))
{
this.echo('Earliest delivery for ' + sLocation + ' is ' + sTime + '. Available:');
this.echo('');
fs.write('deliveroo.txt', 'Earliest delivery for ' + sLocation + ' is ' + sTime + '. Available:\n\n', 'a');
}
for (i = 1; i <=15; i++) {
if (!this.exists(x('(//*[@class="restaurant-name"])[' + i + ']'))) {break;}
var sName = this.fetchText(x('(//*[@class="restaurant-name"])[' + i + ']')).trim();
var sType = this.fetchText(x('(//*[@class="detail-cat"])[' + i + ']')).trim();
sType = sType.toUpperCase().replace(" • NEW","");
this.echo(sName + ' (' + sType + ')');
fs.write('deliveroo.txt', sName + ' (' + sType + ')\n', 'a');
}
this.click(x('//*[@class="header-icon icon-account"]'));
this.click(x('//*[@class="li-last"]'));
// this.echo('LOGGED OUT.');
}
else
{
var sLocation = this.fetchText(x('//*[@class="restaurant-index-page--title"]'));
var sTime = this.fetchText(x('//*[@name="day"]//*[@selected]'));
if ((sLocation.length > 0) && (sTime.length > 0))
{
this.echo('Earliest delivery for ' + sLocation + ' is ' + sTime + '. Available:');
this.echo('');
fs.write('deliveroo.txt', 'Earliest delivery for ' + sLocation + ' is ' + sTime + '. Available:\n\n', 'a');
for (i = 1; i <=15; i++) {
if (!this.exists(x('(//*[@class="restaurant-index-page-tile--name"])[' + i + ']'))) {break;}
var sName = this.fetchText(x('(//*[@class="restaurant-index-page-tile--name"])[' + i + ']')).trim();
var sType = this.fetchText(x('(//*[@class="restaurant-index-page-tile--tag"])[' + i + ']')).trim();
sType = sType.toUpperCase().replace(" • NEW","");
this.echo(sName + ' (' + sType + ')');
fs.write('deliveroo.txt', sName + ' (' + sType + ')\n', 'a');
}
this.click(x('//*[@class="page-header--nav-link icon-user"]'));
this.click(x('//*[@class="page-header-dropdown--text link-button"]'));
// this.echo('LOGGED OUT.');
}
});
});
casper.run();