-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-bot.js
258 lines (214 loc) · 7.81 KB
/
request-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
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
const cheerio = require('cheerio');
var request = require('request-promise');
const querystring = require('querystring');
const events = require('events');
const notifier = require('./notifier.js')
const Generator = require('./pookygen.js');
var tough = require('tough-cookie');
var cookieJar = request.jar();
var request = request.defaults({ jar: true })
var url;
var hasBeenNotified = false;
var item;
module.exports = class Bot {
constructor(url) {
this.url = url;
}
async start() {
// Load cookies
// await Generator.getCookies(function (sessionCookies) {
// console.log(sessionCookies)
// for (var i = 0; i < sessionCookies.length; i++) {
// console.log(sessionCookies[i].name)
// // Easy creation of the cookie - see tough-cookie docs for details
// // let cookie = new tough.Cookie({
// // key: sessionCookies[i].name,
// // value: sessionCookies[i].value,
// // domain: sessionCookies[i].domain,
// // maxAge: -1
// // });
// // cookieJar.setCookie(cookie, sessionCookies[i].domain);
// }
// });
// Wait for drop
notifier.on('new-items', function (item) {
if (!hasBeenNotified)
cartItem((response) => {
// console.log(response)
});
hasBeenNotified = true;
});
};
}
function getItem(itemURL, callback) {
request(itemURL, function (err, resp, html, rrr, body) {
if (err) {
return callback('No response from website', null);
} else {
var $ = cheerio.load(html);
}
var sizeOptionsAvailable = [];
if ($('option')) {
$('option').each(function (i, elem) {
var size = {
id: parseInt($(this).attr('value')),
size: $(this).text(),
}
sizeOptionsAvailable.push(size);
});
if (sizeOptionsAvailable.length > 0) {
sizesAvailable = sizeOptionsAvailable
} else {
sizesAvailable = null
}
} else {
sizesAvailable = null;
}
var availability;
var addCartURL = api.url + $('form[id="cart-addf"]').attr('action');
var addCartButton = $('input[value="add to cart"]')
if (addCartButton.attr('type') == '') {
availability = 'Available'
} else {
availability = 'Sold Out'
}
if (availability == 'Sold Out') {
addCartURL = null
}
var metadata = {
title: $('h1').attr('itemprop', 'name').eq(1).html(),
style: $('.style').attr('itemprop', 'model').text(),
link: itemURL,
description: $('.description').text(),
addCartURL: addCartURL,
price: parseInt(($('.price')[0].children[0].children[0].data).replace('$', '').replace(',', '')),
image: 'http:' + $('#img-main').attr('src'),
sizesAvailable: sizesAvailable,
images: [],
availability: availability
};
// Some items don't have extra images (like some of the skateboards)
if ($('.styles').length > 0) {
var styles = $('.styles')[0].children;
for (li in styles) {
for (a in styles[li].children) {
if (styles[li].children[a].attribs['data-style-name'] == metadata.style) {
metadata.images.push('https:' + JSON.parse(styles[li].children[a].attribs['data-images']).zoomed_url)
}
}
}
} else if (title.indexOf('Skateboard') != -1) {
metadata.images.push('https:' + $('#img-main').attr('src'))
}
callback(null, metadata);
});
};
function cartItem(callback) {
var body = {
"s": "63029",
"st": "22422",
"qty": "1"
};
var formData = querystring.stringify(body);
var contentLength = formData.length;
// Set the headers for the request
var headers = {
"Accept": "application/json",
"Origin": "https://www.supremenewyork.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
'Content-Length': contentLength,
"Referer": "https://www.supremenewyork.com/mobile/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9,fr;q=0.8",
"Cookie": "",
};
// Configure the request
var options = {
url: "https://www.supremenewyork.com/shop/171995/add.json",
method: 'POST',
headers: headers,
body: formData,
jar: cookieJar
};
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// Print out the response body
console.log(response.statusCode);
console.log(response.headers['set-cookie']);
const cookies = response.headers['set-cookie'] || [];
// cookies.forEach(c => {
// cookieJar.setCookie(c, url);
// });
// console.log(cookieJar.getCookies(url));
callback(response);
} else {
console.log(error);
}
});
};
function loadItem(callback) {
request({ url: cartURL, jar: cookieJar }, function (err, resp, html, rrr, body) {
callback();
});
};
function checkoutItem(callback) {
var body = {
"credit_card[cnb]": "4532 9072 8270 0827",
"credit_card[month]": "01",
"credit_card[rsusr]": "333",
"credit_card[year]": "2025",
"from_mobile": "1",
"g-recaptcha-response": "",
"order[billing_address]": "3820 Cedar Run Ct.",
"order[billing_address_2]": "",
"order[billing_city]": "Efland",
"order[billing_country]": "USA",
"order[billing_name]": "Joe Scott",
"order[billing_state]": "NC",
"order[billing_zip]": "27243",
"order[email]": "jox.rox.js@gmail.com",
"order[tel]": "919-928-1202",
"order[terms]": [
"0",
"1"
],
"same_as_billing_address": "1",
"store_credit_id": ""
};
var formData = querystring.stringify(body);
var contentLength = formData.length;
// Set the headers for the request
var headers = {
"Accept": "application/json",
"Origin": "https://www.supremenewyork.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
'Content-Length': contentLength,
"Referer": "https://www.supremenewyork.com/mobile/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9,fr;q=0.8",
"Cookie": "",
};
// Configure the request
var options = {
url: "https://www.supremenewyork.com/checkout.json",
method: 'POST',
headers: headers,
body: formData
};
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// Print out the response body
// console.log(response.statusCode);
// console.log(response.headers['set-cookie']);
callback(response);
} else {
console.log(error);
}
});
};