This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
128 lines (109 loc) · 3.48 KB
/
app.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
const rp = require('request-promise');
const cheerio = require('cheerio');
const axios = require('axios')
const url = 'http://www.supremenewyork.com';
const options = {
uri: url,
transform: function(body) {
return cheerio.load(body);
}
}
var api = {};
api.getAll = (category, callback) => {
if(category === 'all' || category === 'new'){
options.uri += `/shop/${category}/`;
rp(options).then(($)=>{
callback($('img').length, category)
return $;
}).catch((err=>{
if(err.statusCode === 404){
console.log('error: 404 supreme webshop is closed. check back later')
callback(null, null, err.statusCode)
}
return err;
}))
}else{
options.uri += '/mobile_stock.json';
async function getProducts() {
try {
const response = await axios.get(options.uri, {
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
});
let categoryData = response.data.products_and_categories[`${category}`];
callback(categoryData, category)
} catch (error) {
console.error(error)
callback(null, null, error)
}
}
getProducts()
}
//Resets uri for next query
options.uri = url;
}
api.getItem = (itemId, callback) => {
options.uri += `/shop/${itemId}.json`;
axios.get(options.uri).then((res)=>{
checkStock(res.data)
return res.data;
}).catch(err=>{
callback(null, null, err)
return err;
})
checkStock = (product) => {
if(product.styles.length > 1 ) {
let stockCount = 0;
product.styles.map((style, i)=>{
stockCount = stockBySize(style, product);
})
callback(product, stockCount)
}else{
let stockCount = product.styles[0].sizes[0].stock_level;
callback(product, stockCount);
}
}
stockBySize = (style, product) => {
let stockCount = 0;
style.sizes.map((size, x)=>{
!size.stock_level ? stockCount++ : null;
})
if(stockCount === style.sizes.length){
return 0;
}else{
return (style.sizes.length - stockCount);
}
}
options.uri = url;
}
api.getRandom = (callback) => {
options.uri += '/random/';
rp(options).then(($)=>{
console.log(`Found ${$('div.random-item').length} in Random`)
grabRandomItemInfo($);
return $;
}).catch((err=>{
if(err.statusCode === 404){
console.log('error: page not found')
callback(null, err.statusCode)
}
return err;
}))
grabRandomItemInfo = ($) => {
let itemObject = [];
$('div.random-item').each((i, element)=>{
let name = $(element).find('span.caption').text();
itemObject.push(
name = {
name: $(element).find('span.caption').text(),
href: $(element).find('a').attr('href'),
img: `http:${$(element).find('img').attr('src')}`
})
})
callback(itemObject)
}
options.uri = url;
}
module.exports = api;