Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
test.js
.DS_Store
.env
.env
.idea
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const stockXAPI = require('./src/classes/Stockx');
const stockXAPI = require('./src/classes');

module.exports = stockXAPI;
24 changes: 15 additions & 9 deletions src/api/scrapers/fetchapikey.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ module.exports = async (options = {}) => {

checkRes(res);

let globalConstantString, constantsObject;

const parseError = new Error("Failed to parse API key for search");
let searchOnlyApiKeyString;
try {
globalConstantString = res.body.split('window.globalConstants = ')[1].split('</script>')[0].trim();
constantsObject = globalConstantString.endsWith(';') ? parseJSON(globalConstantString.slice(0, globalConstantString.length - 1)) : JSON.parse(globalConstantString);
let splittedBodyContent = res.body.split('window.searchOnlyApiKey = ');

if (splittedBodyContent.length < 2) {
throw parseError
}

splittedBodyContent = splittedBodyContent[1].split("';")
searchOnlyApiKeyString = splittedBodyContent[0].replace("'", "").trim();
} catch (err) {
console.error(err)
throw parseError;
}
catch(err){
throw new Error("Failed to parse API key for search");
};

return constantsObject.search.SEARCH_ONLY_API_KEY;
};
return searchOnlyApiKeyString;
};
21 changes: 15 additions & 6 deletions src/api/scrapers/searchproducts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ module.exports = async (query, options = {}) => {
const { limit, proxy, userAgent, cookieJar } = options;

const res = await request({
url: `https://www.stockx.com/api/browse?_search=${query}`,
url: `https://www.stockx.com/api/browse?_search=${query}`,
headers: {
'user-agent': userAgent,
'sec-fetch-dest': 'none',
'accept': '*/*',
'sec-fetch-site': 'cross-site',
'sec-fetch-dest': 'empty',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'accept': 'application/json',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'accept-encoding': '*',
'authority': 'stockx.com',
'x-requested-with': 'XMLHttpRequest',
// 'referer': 'https://stockx.com/de-de/new-balance-test-run-30-salehe-bembury-finders-keepers',
'app-platform': 'Iron',
'sec-ch-ua-platform': '"macOS"',
'app-version': '2022.02.13.03',
'accept-language': 'en-US'
}.
},
proxy,
//jar: cookieJar
});
Expand All @@ -41,4 +50,4 @@ module.exports = async (query, options = {}) => {

if (productArray == "") throw new Error("No products found!");
else return productArray;
};
};
3 changes: 3 additions & 0 deletions src/classes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const stockx = require('./stockx');

module.exports = stockx
1 change: 1 addition & 0 deletions src/classes/stockx.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = class StockX {
* @param {string} query - The query string to search for
* @param {Object=} options
* @param {Number=} options.limit - The limit on how many products to return at max
* @deprecated Seems like the api blocks this endpoint now. Use newSearchProducts()
*/
async searchProducts(query, options = {}){
//Search products and return them
Expand Down