-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathindex.js
More file actions
302 lines (251 loc) · 9.88 KB
/
index.js
File metadata and controls
302 lines (251 loc) · 9.88 KB
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
var cheerio = require('cheerio');
var request = require('request');
var request = require('request').defaults({
//timeout: 30000
});
var api = {};
api.url = 'http://www.supremenewyork.com';
String.prototype.capitalizeEachWord = function() {
return this.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
// getItem('all')
// other options: new, jackets, shirts, tops_sweaters, sweatshirts,
// pants, hats, bags, accessories, shoes, skate
/**
* Checks for items under desired category
*
* @param {String} category
* @return {Array}
*/
api.getItems = function(category, callback) {
var getURL = api.url + '/shop/all/' + category;
if (category == 'all') {
getURL = api.url + '/shop/all';
} else if (category == 'new') {
getURL = api.url + '/shop/new';
}
request(getURL, function(err, resp, html, rrr, body) {
if (!err) {
if (err) {
console.log('err')
return callback('No response from website', null);
} else {
var $ = cheerio.load(html);
}
var count = $('img').length;
if ($('.shop-closed').length > 0) {
return callback('Store Closed', null);
} else if (count === 0) {
return callback('Store Closed', null);
}
var parsedResults = [];
// console.log(len);
$('img').each(function(i, element) {
var nextElement = $(this).next();
var prevElement = $(this).prev();
var image = "https://" + $(this).attr('src').substring(2);
var title = $(this).attr('alt');
var availability = nextElement.text().capitalizeEachWord();
var link = api.url + this.parent.attribs.href;
var sizesAvailable;
if (availability == "") availability = "Available";
request(link, function(err, resp, html, rrr, body) {
if (err) {
return callback('No response from website', null);
} else {
var $ = cheerio.load(html);
}
var addCartURL = api.url + $('form[id="cart-addf"]').attr('action');
if (availability == "Sold Out") {
addCartURL = null;
}
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 metadata = {
title: $('h1').attr('itemprop', 'name').eq(1).html(),
style: $('.style').attr('itemprop', 'model').text(),
link: link,
description: $('.description').text(),
addCartURL: addCartURL,
price: parseInt(($('.price')[0].children[0].children[0].data).replace('$', '').replace(',', '')),
image: image,
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) {
// Because fuck skateboards
metadata.images.push('https:' + $('#img-main').attr('src'))
}
parsedResults.push(metadata);
if (!--count) {
callback(parsedResults, null);
}
})
});
} else {
return callback('No response from website', null);
}
});
};
api.getItem = function(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);
});
};
api.watchOnAllItems = [];
api.watchAllItems = function(interval, category, callback) {
api.log('Now watching for all items');
api.watchOnAllItems = setInterval(function() {
api.getItems(category, function(items) {
callback(items, null);
});
}, 1000 * interval); // Every xx sec
}
api.stopWatchingAllItems = function(callback) {
clearInterval(api.watchOnAllItems);
if (api.watchOnAllItems == "") {
callback(null, 'No watching processes found.');
} else {
callback('Watching has stopped.', null);
}
}
// searches for new item drop TODO
api.onNewItem = function(callback) {
api.watchAllItems(function(item) {
// TODO: If new items is find return callback(value);
});
}
/**
* Seeks for items on desired category page with specific keywords/styles.
* @param {Number} interval
* @param {String} category
* @param {String} style
* @param {String} category
* @return {Object}
*/
api.seek = function(category, keywords, styleSelection, callback) {
var productLink = [];
api.getItems(category, (product, err) => {
if (err) {
return callback(null, 'Error occured while trying to seek for items.');
}
for (i = 0; i < product.length; i++) {
var title = product[i].title;
var style = product[i].style;
if (style === null) {
// type - style not defined without a match
if (title.indexOf(keywords) > -1) { // check if the keywords match with the title
// found item
productLink.push(product[i].link);
return callback(product[i], null);
break;
} else {
continue;
}
} else if (style.indexOf(styleSelection) > -1) {
// type - style defined with match
if (title.indexOf(keywords) > -1) { // check if the keywords match with the title
// found item
productLink.push(product[i].link);
return callback(product[i], null);
break;
} else {
continue;
}
}
}
if (productLink[0] === undefined) {
return callback(null, "Could not find any results matching your keywords.");
}
});
}
api.log = function(message) {
console.log('[supreme api] ' + message);
}
module.exports = api;