-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuel.js
executable file
·158 lines (125 loc) · 4.71 KB
/
fuel.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
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
#! /usr/bin/env casperjs
var casper = require('casper').create({
logLevel: 'debug',
verbose: false,
viewportSize: {
width: 1280,
height: 800,
},
pageSettings: {
loadImages: false,
},
waitTimeout: 10000,
});
var fs = require('fs');
var LOGIN_URL = 'http://nikeplus.nike.com/plus/';
(function() {
var currentUrl;
var email;
var frameName;
var fuel = new Object();
var password;
function waitForUrlChange() {
casper.waitFor(function() {
return casper.getCurrentUrl() !== currentUrl;
}, function() {
currentUrl = casper.getCurrentUrl();
});
}
function getData() {
casper.then(function doShitWithData() {
var date = casper.evaluate(function() {
return window.np.baked_data.activity.localStart.localTime;
});
var data = casper.evaluate(function() {
return window.np.baked_data.dataPoints;
});
data = data.map(function(x) {
return x.fuel;
});
fuel[date] = data;
var serializedFuel = JSON.stringify(fuel, null, 4);
fs.write('fuel.json', serializedFuel, 'w');
var serializedDate = new Date(date);
console.log('Wrote', serializedDate);
});
casper.then(function clickPreviousLink() {
casper.click('a.previous');
casper.then(waitForUrlChange);
});
casper.then(getData);
}
(function() {
casper.start(LOGIN_URL, function getLoginFrameName() {
frameName = casper.evaluate(function() {
var frame = document.getElementById('checkers');
return frame.name;
});
currentUrl = casper.getCurrentUrl();
});
casper.then(function submitLoginForm() {
casper.withFrame(frameName, function() {
casper.evaluate(function(email, password) {
var form = document.querySelector('form');
var emailNode = form.querySelector('input[type=email]');
emailNode.value = email;
var passwordNode = form.querySelector('input[type=password]');
passwordNode.value = password;
// Submitting like this because form.submit() doesn't work
var submit = form.querySelector('input[type=submit]');
submit.click();
}, {
email: email,
password: password,
});
});
casper.then(waitForUrlChange);
});
casper.then(function changeLocaleIfWrong() {
var localeText = casper.evaluate(function() {
var localeLink = document.querySelector('a#footer_region_link');
var localeText = localeLink.innerText;
localeText = toLowerCase();
return localeText;
});
if (localeText !== 'english') {
casper.click('a#footer_region_link');
casper.then(waitForUrlChange);
casper.then(function clickValidLocaleLink() {
// Have to hover over "English" link to see all English locales
casper.evaluate(function() {
var link = document.querySelector('a[rel=english]');
var event = document.createEvent('MouseEvents');
event.initEvent('mouseover', true, false);
link.dispatchEvent(event);
});
var validLocaleLinkSelector = 'a[rel=en_GB]';
casper.waitUntilVisible(validLocaleLinkSelector, function() {
casper.click(validLocaleLinkSelector);
});
casper.then(waitForUrlChange);
});
}
});
casper.then(function clickActivityLink() {
casper.click('a[title=Activity]');
casper.then(waitForUrlChange);
});
casper.then(function clickLastSyncBar() {
casper.evaluate(function() {
var links = document.querySelectorAll('a.bar_shim');
var lastLink = links[links.length - 1];
window.location = lastLink.href;
});
casper.then(waitForUrlChange);
});
casper.then(getData);
if (casper.cli.args.length < 2) {
console.log('Usage: ./fuel.js <email> <password>');
casper.exit();
}
email = casper.cli.args[0];
password = casper.cli.args[1];
casper.run();
})();
})();