-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreinvestCash.js
167 lines (133 loc) · 4.58 KB
/
reinvestCash.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
159
160
161
162
163
164
165
166
167
var casper = require('casper').create();
var url = 'https://www.lendingclub.com/account/gotoLogin.action';
var email = casper.cli.get('email');
var password = casper.cli.get('password');
var minAvailableCash = casper.cli.get('minCash');
var minExpectedReturn = casper.cli.get('minReturn');
var globalTimeout = casper.cli.get('timeout');
var filterName = casper.cli.get('filterName');
if (!email || !password || !minAvailableCash || !minExpectedReturn || !filterName) {
casper.echo('ERROR missing parameter(s)');
casper.exit();
}
if (casper.cli.args.length !== 5) {
casper.echo('ERROR too many parameters...');
casper.exit();
}
casper.myBeginStep = function(description) {
this.echo(description);
this.capture(description + '.png');
}
casper.myWaitUntilVisible = function(selector, then) {
this.waitUntilVisible(selector, then, function timeout() {
this.echo('ERROR timeout waitUntilVisible: ' + selector);
this.capture(selector + '.png');
this.exit();
}, globalTimeout);
}
casper.myWaitFor = function(predicate, then) {
this.waitFor(predicate, then, function timeout() {
this.echo('ERROR waitFor');
this.capture('waitFor.png');
this.exit();
}, globalTimeout);
}
//BEGIN login
casper.start(url, function() {
this.myBeginStep('login');
this.myWaitUntilVisible('#email', function() {
this.evaluate(function(email, password) {
document.querySelector('#email').setAttribute('value', email);
document.querySelector('#password').setAttribute('value', password);
document.querySelector('form[action="/account/login.action"]').submit();
}, email, password);
this.myWaitUntilVisible('#narL');
});
});
//END login
//BEGIN make sure we're starting from scratch
casper.thenOpen('https://www.lendingclub.com/portfolio/confirmStartNewPortfolio.action', function() {
this.myBeginStep("make sure we're starting from scratch");
this.myWaitFor(function() {
return this.evaluate(function() {
var readyToInvest = document.querySelector('#risk-strategy-buttons').style.opacity === "1";
var askingForFunds = document.querySelector('a[href="/account/addFunds.action"]');
return readyToInvest || askingForFunds;
});
});;
});
//END make sure we're starting from scratch
//BEGIN check available cash
casper.then(function() {
this.myBeginStep('check available cash');
var availableCash = this.evaluate(function() {
var formattedCashString = document.querySelector('#availableCash').innerHTML;
var cashString = formattedCashString.replace('$', '').replace(',', '');
return parseFloat(cashString);
});
this.echo('Available cash: ' + availableCash);
if (availableCash < minAvailableCash) {
this.echo('Available cash too low, exiting...');
this.exit();
}
});
//END check available cash
//BEGIN open saved filter
casper.then(function() {
this.myBeginStep('open saved filter');
this.capture('before savedFiltersButton.png');
this.click('#savedFiltersButton');
this.myWaitFor(function() {
return this.evaluate(function() {
return document.querySelector('.savedCriteriaLoad').innerHTML === filterName;
});
}, function() {
this.click('.savedCriteriaLoad');
this.myWaitUntilVisible('.mask');
this.waitWhileVisible('.mask');
this.myWaitFor(function() {
return this.evaluate(function() {
return document.querySelector('#risk-strategy-buttons').style.opacity === "1";
});
});
});
});
//END open saved filter
//BEGIN initialize order
casper.then(function() {
this.myBeginStep('initialize order');
this.click('#more-aggressive');
this.myWaitUntilVisible('#view-selected-portfolio-notes', function() {
this.click('#view-selected-portfolio-notes');
});
});
//END initialize order
//BEGIN if projected return high enough, continue order
casper.then(function() {
this.myBeginStep('if projected return high enough, continue order');
this.myWaitUntilVisible('#finish2', function() {
var expectedReturn = this.evaluate(function() {
var formattedExpectedReturn = document.querySelector('#projected-returns').innerHTML;
var expectedReturn = formattedExpectedReturn.replace('%', '');
return parseFloat(expectedReturn);
});
this.echo('Expected return: ' + expectedReturn);
if (expectedReturn < minExpectedReturn) {
this.echo('Cancelling due to low expected return');
this.exit();
}
this.click('#finish2');
});
});
//END if projected return high enough, continue order
//BEGIN finalize order
casper.then(function() {
this.myBeginStep('finalize order');
this.myWaitUntilVisible('#place-order-link2', function() {
this.click('#place-order-link2');
this.wait(globalTimeout);
this.echo('SUCCESS order finalized');
});
});
//END finalize order
casper.run();