-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
303 lines (254 loc) · 7.99 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require('dotenv').config();
const { Builder, By, Key, until } = require('selenium-webdriver');
run();
async function run() {
const browser = process.env.BROWSER;
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
if (!browser || !username || !password) {
console.error(
'Error reading environment variables. Ensure you have created a .env file with the variables: BROWSER, USERNAME, and PASSWORD.'
);
return;
}
const driver = await new Builder().forBrowser(browser).build();
try {
await driver
.manage()
.window()
.maximize();
await driver.get('http://www.neopets.com');
await login(driver, username, password);
await collectInterest(driver);
await buyStocks(driver, 1000, 17);
await sellStocks(driver, 30);
await shopOfOffers(driver);
await anchorManagement(driver);
await tdmbgpop(driver);
await fruitMachine(driver);
await coltzansShrine(driver);
} catch (error) {
console.error('Error while running.\n', error);
} finally {
console.log('Bot completed.');
await driver.sleep(3000);
await driver.quit();
}
}
async function login(driver, username, password) {
const xpath = {
loginLink: '//*[@id="header"]/table/tbody/tr[1]/td[3]/a[1]',
usernameField: '//*[@id="templateLoginPopupUsername"]',
passwordField:
'//*[@id="templateLoginPopup"]/div/form/table/tbody/tr[2]/td[2]/input',
logoutLink: '//*[@id="logout_link"]',
};
await driver.navigate().to('http://www.neopets.com');
await driver.findElement(By.xpath(xpath.loginLink)).click();
await driver.findElement(By.xpath(xpath.usernameField)).sendKeys(username);
await driver
.findElement(By.xpath(xpath.passwordField))
.sendKeys(password, Key.RETURN);
await driver.wait(until.elementLocated(By.xpath(xpath.logoutLink)));
console.log('Login completed.');
}
async function collectInterest(driver) {
const xpath = {
interestBox:
'//*[@id="content"]/table/tbody/tr/td[2]/table[2]/tbody/tr/td/div/table/tbody/tr[2]/td',
button:
'//*[@id="content"]/table/tbody/tr/td[2]/table[2]/tbody/tr/td/div/table/tbody/tr[2]/td/div/form/input[2]',
};
await driver.navigate().to('http://www.neopets.com/bank.phtml');
await driver.wait(until.elementLocated(By.xpath(xpath.interestBox)));
try {
await driver.findElement(By.xpath(xpath.button)).click();
await driver.sleep(3000);
console.log('Interest collection completed.');
} catch (e) {
if (e.name === 'NoSuchElementError') {
console.log('Interest already collected.');
} else {
console.error('Error collecting bank interest.', e);
}
}
}
async function buyStocks(driver, numShares, buyPrice) {
const xpath = {
stockField: '//*[@id="content"]/table/tbody/tr/td[2]/div[2]/form/input[2]',
stockTable: '//*[@id="content"]/table/tbody/tr/td[2]/div[2]/table/tbody',
stockLink:
'//*[@id="content"]/table/tbody/tr/td[2]/div[2]/table/tbody/tr[1]/td[2]/a/b/font',
stockNumberField:
'//*[@id="content"]/table/tbody/tr/td[2]/div[2]/form/table/tbody/tr[2]/td[2]/input',
};
await driver
.navigate()
.to('http://www.neopets.com/stockmarket.phtml?type=list');
let stockField = await driver.wait(
until.elementLocated(By.xpath(xpath.stockField))
);
await stockField.sendKeys(Key.SPACE, Key.RETURN);
let stockTable = await driver.wait(
until.elementLocated(By.xpath(xpath.stockTable))
);
let stocks = await stockTable.findElements(By.css('tr:not(:first-child)'));
let stockInfo = [];
for (stock of stocks) {
try {
let stockLink = await stock.findElement(By.css('td:nth-child(2) a b'));
let stockPrice = await stock
.findElement(By.css('td:nth-child(6)'))
.getText();
stockInfo.push({
link: stockLink,
price: stockPrice,
});
} catch (e) {
if (e.name !== 'NoSuchElementError') {
console.error('Error buying stocks.', e);
}
continue;
}
}
stockInfo = stockInfo.filter(
stock => stock.price >= 15 && stock.price <= buyPrice
);
stockInfo = stockInfo.sort((a, b) => a.price - b.price);
if (stockInfo.length > 0) {
await stockInfo[0].link.click();
let stockLink = await driver.wait(
until.elementLocated(By.xpath(xpath.stockLink))
);
await stockLink.click();
let stockNumberField = await driver.wait(
until.elementLocated(By.xpath(xpath.stockNumberField))
);
await stockNumberField.sendKeys(numShares.toString(), Key.RETURN);
}
await driver.sleep(3000);
console.log('Buying stocks completed.');
}
async function sellStocks(driver, sellPrice) {
const xpath = {
stockTable: '//*[@id="postForm"]/table[1]/tbody',
sellButton: '//*[@id="show_sell"]/center/input',
};
await driver
.navigate()
.to('http://www.neopets.com/stockmarket.phtml?type=portfolio');
let stockTable = await driver.wait(
until.elementLocated(By.xpath(xpath.stockTable))
);
let stocks = await stockTable.findElements(
By.css('tr:not(:nth-child(-n+2)):not(:last-child):nth-child(odd)')
);
let stockInfo = [];
for (stock of stocks) {
try {
let stockArrow = await stock.findElement(
By.css('td:first-child img:first-child')
);
let stockShareTable = await stock.findElement(
By.xpath('following-sibling::*[1]')
);
let stockPrice = await stock
.findElement(By.css('td:nth-child(4)'))
.getText();
stockInfo.push({
arrow: stockArrow,
shareTable: stockShareTable,
price: stockPrice,
});
} catch (e) {
if (e.name !== 'NoSuchElementError') {
console.error('Error selling stocks.', e);
}
continue;
}
}
stockInfo = stockInfo.filter(stock => stock.price >= sellPrice);
stockInfo = stockInfo.sort((a, b) => a.price - b.price);
if (stockInfo.length > 0) {
for (stock of stockInfo) {
await stock.arrow.click();
let shares = await stock.shareTable.findElements(
By.css('tbody tr:not(:first-child)')
);
for (share of shares) {
let numShares = await share
.findElement(By.css('td:first-child'))
.getText();
let shareField = await share.findElement(By.css('input'));
await shareField.sendKeys(numShares);
}
}
await driver.findElement(By.xpath(xpath.sellButton)).click();
}
await driver.sleep(3000);
console.log('Selling stocks completed.');
}
async function shopOfOffers(driver) {
const xpath = {
event: '//*[@id="content"]/table/tbody/tr/td[2]/table',
};
await driver
.navigate()
.to('http://www.neopets.com/shop_of_offers.phtml?slorg_payout=yes');
await driver.wait(until.elementLocated(By.xpath(xpath.event)));
console.log('Shop of offers completed.');
}
async function anchorManagement(driver) {
const xpath = {
button: '//*[@id="btn-fire"]/a/div',
prizeImage: '//*[@id="krawk-island"]/div[3]/div/img',
};
await driver
.navigate()
.to('http://www.neopets.com/pirates/anchormanagement.phtml');
const button = await driver.wait(
until.elementLocated(By.xpath(xpath.button))
);
await button.click();
await driver.wait(until.elementLocated(By.xpath(xpath.prizeImage)));
console.log('Anchor management completed.');
}
async function tdmbgpop(driver) {
const xpath = {
button: '//*[@id="content"]/table/tbody/tr/td[2]/div[3]/form/input[2]',
};
await driver
.navigate()
.to('http://www.neopets.com/faerieland/tdmbgpop.phtml');
const button = await driver.wait(
until.elementLocated(By.xpath(xpath.button))
);
await button.click();
await driver.sleep(3000);
console.log('TDMGBPOP completed.');
}
async function fruitMachine(driver) {
const xpath = {
button: '//*[@id="content"]/table/tbody/tr/td[2]/div[2]/form/input[3]',
};
await driver.navigate().to('http://www.neopets.com/desert/fruit/index.phtml');
const button = await driver.wait(
until.elementLocated(By.xpath(xpath.button))
);
await button.click();
await driver.sleep(10000);
console.log('Fruit machine completed.');
}
async function coltzansShrine(driver) {
const xpath = {
button:
'//*[@id="content"]/table/tbody/tr/td[2]/div[2]/div/form[1]/input[2]',
};
await driver.navigate().to('http://www.neopets.com/desert/shrine.phtml');
const button = await driver.wait(
until.elementLocated(By.xpath(xpath.button))
);
await button.click();
await driver.sleep(3000);
console.log("Coltzan's shrine completed.");
}