Add some async to the queries

main
Ibraheem Saleh 10 months ago
parent cc02ef4363
commit d9839f21ea

@ -39,7 +39,7 @@ const headers = {
'TE': 'trailers'
};
const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6616096","6614122","6616093","6616090","6616091","6615930","6615929","6615931"]
const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6614122","6616093","6616090","6616091","6615930","6615929","6615931"]
const queries = skuIds.map(skuId => ({
skuId,
@ -151,42 +151,52 @@ async function fetchGraphQL(zip, store, query) {
async function pollStore(zip, store) {
console.log(`Starting poll for zip ${zip} and store ${store}...`);
const batchSize = 5;
while (!found) {
for (const { skuId, query } of queries) {
const data = await fetchGraphQL(zip, store, query);
if (
data &&
data.data &&
data.data.productBySkuId &&
data.data.productBySkuId.fulfillmentOptions &&
data.data.productBySkuId.fulfillmentOptions.buttonStates &&
data.data.productBySkuId.fulfillmentOptions.buttonStates.length > 0
) {
const buttonState = data.data.productBySkuId.fulfillmentOptions.buttonStates[0].buttonState;
console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`);
if (buttonState !== "SOLD_OUT") {
found = true;
const productUrl = getProductUrl(skuId)
console.log(productUrl)
winningResult = { skuId, zip, store, buttonState, data };
const openPromise = open(productUrl);
const addToCartPromise = addToCart(productUrl);
await open('./alarm.wav');
await addToCartPromise
return winningResult;
const queryBatches = [];
for (let i = 0; i < queries.length; i += batchSize) {
queryBatches.push(queries.slice(i, i + batchSize));
}
for (const batch of queryBatches) {
const fetchPromises = batch.map(({ skuId, query }) => fetchGraphQL(zip, store, query).then(data => ({ skuId, data })));
const results = await Promise.all(fetchPromises);
for (const { skuId, data } of results) {
if (
data &&
data.data &&
data.data.productBySkuId &&
data.data.productBySkuId.fulfillmentOptions &&
data.data.productBySkuId.fulfillmentOptions.buttonStates &&
data.data.productBySkuId.fulfillmentOptions.buttonStates.length > 0
) {
const buttonState = data.data.productBySkuId.fulfillmentOptions.buttonStates[0].buttonState;
console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`);
if (buttonState !== "SOLD_OUT") {
found = true;
const productUrl = getProductUrl(skuId);
console.log(productUrl);
winningResult = { skuId, zip, store, buttonState, data };
const openPromise = open(productUrl);
const addToCartPromise = addToCart(productUrl);
await open('./alarm.wav');
await addToCartPromise;
return winningResult;
}
} else {
console.log(`Incomplete data for zip ${zip}, store ${store}.`);
}
lines++;
if (lines > 30) {
lines = 0;
process.stdout.write('\x1B[3J\x1B[2J\x1B[H');
}
await new Promise(resolve => setTimeout(resolve, 500));
}
} else {
console.log(`Incomplete data for zip ${zip}, store ${store}.`);
}
lines++
if(lines > 30) {
lines = 0;
process.stdout.write('\x1B[3J\x1B[2J\x1B[H');
}
await new Promise(resolve => setTimeout(resolve, 500));
}
}
return null;
}
return null;
}
async function runAllPollers() {

Loading…
Cancel
Save