-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-bot.js
134 lines (95 loc) · 4.26 KB
/
browser-bot.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
const pluginStealth = require("puppeteer-extra-plugin-stealth");
var cheerio = require('cheerio');
var UserAgent = require('user-agents');
var notifier = require('./notifier.js')
const puppeteer = require('puppeteer-extra')
puppeteer.use(pluginStealth());
var addressCookie = [
{
name: "js-address",
value: 'Joe%20Scott|jox.rox.js%40gmail.com|919-928-1202|3820%20Cedar%20Run%20Ct.||Efland|NC|27243|USA',
domain: 'www.supremenewyork.com'
}
];
class Bot {
constructor(options) {
this.page = null;
this.browser = null;
this.keyword = options.keyword.toLowerCase();
}
async prepare() {
// Create mobile user agent
const userAgent = new UserAgent({ deviceCategory: 'mobile' })
// Start browser if not already running
if (this.browser == null)
this.browser = await puppeteer.launch({ headless: false });
// Create main page
this.page = (await this.browser.pages())[0];
// Set viewport size (may not be needed)
// await this.page.setViewport({ width: userAgent.data.viewportWid th, height: userAgent.data.viewportHeight })
// Set user agent
await this.page.setUserAgent(userAgent.toString());
await this.page.setViewport({ width: 0, height: 0 });
await this.page.setCookie(...addressCookie);
notifier.once('new-items', (items) => {
var bogos = items.filter(d => d.name.includes(this.keyword))
console.log(bogos)
this.checkout(`https://www.supremenewyork.com/mobile/#products/${bogos[0].id}`)
return;
});
// Navigate to the page
await this.page.goto("https://www.supremenewyork.com/shop/new", { waitUntil: 'domcontentloaded' });
}
async checkout(url) {
var startTime = Date.now()
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
const dropdown = await this.page.waitForSelector('#size-options', { visible: true })
var $ = cheerio.load(await this.page.evaluate(() => document.body.innerHTML));
var sizeOptionsAvailable = [];
if ($('#size-options')) {
$('option').each(function (i, elem) {
var size = {
id: parseInt($(this).attr('value')),
size: $(this).text(),
}
sizeOptionsAvailable.push(size);
});
} else {
sizeOptionsAvailable = null;
}
if (sizeOptionsAvailable.length != 0) {
const sizeToBuy = sizeOptionsAvailable[sizeOptionsAvailable.length - 1]
const selectElem = await this.page.$('#size-options');
await selectElem.type(sizeToBuy.size);
} else {
console.log(`No sizes found!`)
return;
}
await this.page.waitForSelector('#cart-update', { visible: true })
await this.page.focus('span[class="cart-button"]');
await this.page.click('span[class="cart-button"]');
// Click the submit button
const checkout = await this.page.waitForSelector('#checkout-now', { visible: true })
await checkout.focus();
// Click checkout button
await Promise.all([
this.page.click("#checkout-now"),
this.page.waitForNavigation({ waitUntil: 'domcontentloaded' })
]);
const input = await this.page.waitForSelector('#credit_card_n', { visible: true })
await input.click({ clickCount: 1 })
await input.type('4616081118283165');
await this.page.select('#credit_card_month', '01')
await this.page.select('#credit_card_year', '2022')
const input2 = await this.page.waitForSelector('#cav', { visible: true })
await input2.click({ clickCount: 1 })
await input2.type('333');
const input3 = await this.page.waitForSelector('#order_terms', { visible: true })
await input3.click({ clickCount: 1 })
const input4 = await this.page.waitForSelector('#submit_button', { visible: true })
await input4.click({ clickCount: 1 })
await this.page.evaluate(async () => { recaptchaCallback() })
console.log(`Checkout time: ${ Date.now() - startTime }ms`)
}
}
module.exports = Bot;