-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (65 loc) · 2.57 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
const axios = require('axios').default;
const WAE = require('@rane/web-auto-extractor').default;
/*
* Stockx is very inconsistent with sizings...
* Looking at their pricing API for Jordan's, sometimes they append a character to the property 'shoeSize'
* AJ1 Retro Satin Snake (Womens): They append 'W' to the sizes ie. "10W", "7W" | 'gender': 'women'
* AJ1 Retro Satin Snake PS (Womens): They don't 'W' to the sizes or anything ie. "10.5", "4" | 'gender': 'preschool'
* AJ1 Retro Satin Snake TD (Womens): They don't 'W' to the sizes or anything ie. "2", "4" | 'gender': 'toddler'
* AJ1 Retro High Royal Toe: They don't append anything to their sizes, not even for gs sizes under mens product ie. "5", "10" | 'gender': 'men'
* AJ12 Retro Uni Gold GS: They append "Y" to the sizes ie. "5.5Y", "7Y" | 'gender": 'child'
*/
const reg = /\- ([\d.YCW]+)$/;
async function getUnparsedProductData(sku) {
try {
// stadium goods automatically redirects to a specific product if theres a matching sku
const resp = await axios.get(`https://www.stadiumgoods.com/catalogsearch/result/?q=${sku.replace('-', ' ')}`);
const parsedSchema = WAE().parse(resp.data);
if (!!parsedSchema.microdata['Product']) {
const productData = parsedSchema.microdata['Product'][0];
return productData;
}
else {
console.log("Invalid sku");
return undefined;
}
}
catch (err) {
console.log(err);
return err;
}
}
async function getProductData(sku) {
try {
const product = await getUnparsedProductData(sku);
// Get rid of Schema properties
delete product["@context"];
delete product["@type"];
delete product.offers["@context"];
delete product.offers["@type"];
// Clean up name and add the dash to sku that Stadiumgoods omits
product.name = product.name.replace(/“|”/g, '"'); // Stadiumgoods (HTML) uses both these instead '"'
product.sku = product.sku.replace(/\s/g, '-');
for (const offer of product.offers.offers) {
// Get rid of Schema properties
delete offer["@context"];
delete offer["@type"];
// availability property returns a string to a Schema URL ending with "InStock" or "OutOfStock"
offer.inStock = offer.availability.endsWith("InStock") ? true : false;
delete offer.availability;
// Lets also add a size property while we're here and get rid of name since we don't need it
offer.size = reg.exec(offer.name)[1];
delete offer.name;
}
// Lets make things less confusing too
product["marketData"] = product.offers;
delete product.offers;
return product;
} catch (e) {
console.log(e);
return e;
}
}
module.exports = {
getProductData: getProductData
};