Skip to content

Commit 33a88a7

Browse files
committed
Porcess only 10 products at a time
1 parent 56373dd commit 33a88a7

File tree

1 file changed

+59
-36
lines changed

1 file changed

+59
-36
lines changed

bot.js

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ const processUrl = async (msg, ctx) => {
5252
ctx.chat.id, sentMsg.message_id,
5353
`Sorry, I couldn't track this product. Make sure you've sent correct product link.`,
5454
{ parse_mode: "Markdown", reply_markup }
55-
);
55+
).catch(e => {});
5656
}
5757
} else {
5858
ctx.reply( "I'm sorry, but you can't add more products as you've already reached the maximum limit.\n\nPlease delete atleast one product. And try again.\n\nTo get list send /list",
59-
{ reply_to_message_id: ctx.message.message_id } );
59+
{ reply_to_message_id: ctx.message.message_id } ).catch(e => {});
6060
}
6161
} else {
62-
ctx.reply( `Sorry, I can't track this product. Cuz the link you sent is not a amazon or flipkart product link.` );
62+
ctx.reply( `Sorry, I can't track this product. Cuz the link you sent is not a amazon or flipkart product link.` ).catch(e => {});
6363
}
6464
} else {
65-
ctx.reply( `Sorry ${ctx.message.chat.first_name}, I can't track this product. Make sure you've sent correct product link.` );
65+
ctx.reply( `Sorry ${ctx.message.chat.first_name}, I can't track this product. Make sure you've sent correct product link.` ).catch(e => {});
6666
}
6767
} catch(e){
6868
console.error(e)
@@ -76,7 +76,7 @@ bot.command("start", (ctx) => {
7676
ctx.reply(
7777
`Hello ${ctx.message.chat.first_name}, I can track price for Amazon & Flipkart products (Soon more).\n\nCheck /help to get started.\n`,
7878
{ reply_to_message_id: ctx.message.message_id, reply_markup, }
79-
);
79+
).catch(() => {})
8080
manageUsers( { id: ctx.message.from.id, name: ctx.message.from.first_name }, "update" );
8181
} catch (e) {
8282
console.log("Error", e);
@@ -92,7 +92,7 @@ bot.command("help", (ctx) => {
9292
reply_to_message_id: ctx.message.message_id,
9393
reply_markup,
9494
}
95-
);
95+
).catch(() => {})
9696
} catch (e) { }
9797
});
9898

@@ -224,37 +224,60 @@ bot.callbackQuery("stopTracking", async (ctx) => {
224224
const track = async () => {
225225
try {
226226
const products = await manageProducts({}, "read");
227-
await Promise.all(
228-
products.result.map(async (product) => {
229-
const details = await getProductDetails(product.link, product.merchant);
230-
if (details.ok && !isNaN(details.price) && details.price !== product.price) {
231-
try {
232-
await manageProducts({ tracking_id: product.tracking_id, userId: product.userId, merchant: product.merchant, title: details.title, link: product.link, initPrice: product.price, price: details.price, users: product.users}, "update");
233-
await Promise.all(product.users.map(async user => {
234-
bot.api.sendMessage(
235-
user.userId,
236-
`<a href="${details.image}"> </a><b>Price has been ${details.price > product.price ? "increased" : "decreased"
237-
} by ${Math.abs(product.price - details.price)}</b>. \n\n<b>${details.title
238-
}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link
239-
}">${product.merchant}</a>\n\nTo stop tracking send /stop_${user.tracking_id
240-
}`,
241-
{
242-
parse_mode: "HTML",
243-
reply_markup: {
244-
inline_keyboard: details?.link ? [
245-
[{ text: "Buy Now", url: details.link }],
246-
[{ text: "Stop Tracking - " + user.tracking_id, callback_data: `stopTracking`, }]]
247-
: []
248-
}
249-
}).catch(e => {})
250-
}))
251-
} catch (e) { bot.start() }
252-
}
253-
})
254-
);
255-
} catch (e) { }
227+
// Process 10 products at a time
228+
for (let i = 0; i < products.result.length; i = i + 10 ) {
229+
const temp = products.result.slice(i, i + 10)
230+
231+
await Promise.all(
232+
temp.map(async (product) => {
233+
const details = await getProductDetails(product.link, product.merchant);
234+
235+
if (details.ok && !isNaN(details.price) && details.price !== product.price) {
236+
try {
237+
await manageProducts({ tracking_id: product.tracking_id, userId: product.userId, merchant: product.merchant, title: details.title, link: product.link, initPrice: product.price, price: details.price, users: product.users}, "update");
238+
await Promise.all(product.users.map(async user => {
239+
bot.api.sendMessage(
240+
user.userId,
241+
`<a href="${details.image}"> </a><b>Price has been ${details.price > product.price ? "increased" : "decreased"
242+
} by ${Math.abs(product.price - details.price)}</b>. \n\n<b>${details.title
243+
}</b>\n\nCurrent Price: <b>${details.price}</b>\nLink: <a href="${details.link
244+
}">${product.merchant}</a>\n\nTo stop tracking send /stop_${user.tracking_id
245+
}`,
246+
{
247+
parse_mode: "HTML",
248+
reply_markup: {
249+
inline_keyboard: details?.link ? [
250+
[{ text: "Buy Now", url: details.link }],
251+
[{ text: "Stop Tracking - " + user.tracking_id, callback_data: `stopTracking`, }]]
252+
: []
253+
}
254+
}).catch(e => console.log(`🚀 ~ file: bot.js:255 ~ temp.map ~ e:`, e))
255+
}))
256+
257+
// wait for 1 sec
258+
await new Promise(resolve => setTimeout(resolve, 1000))
259+
} catch (e) {
260+
console.log(`🚀 ~ file: bot.js:260 ~ temp.map ~ e:`, e)
261+
bot.start()
262+
// wait for 5 sec
263+
await new Promise(resolve => setTimeout(resolve, 5000))
264+
}
265+
}
266+
})
267+
);
268+
}
269+
} catch (e) {
270+
console.log(`🚀 ~ file: bot.js:270 ~ track ~ e:`, e)
271+
}
256272
};
257273

274+
bot.command("update", async (ctx) => {
275+
if (ADMINS.includes(ctx.from.id)) {
276+
track()
277+
ctx.reply("Updating products...")
278+
}
279+
})
280+
258281
bot.catch((err) => {
259282
console.error("err");
260283
const ctx = err.ctx;
@@ -266,4 +289,4 @@ bot.catch((err) => {
266289

267290
setInterval(track, 3600000); //Track every hr.
268291

269-
export default bot;
292+
export default bot

0 commit comments

Comments
 (0)