From 0f342365e5753a2059fcbfe567f569e5c8e68bf5 Mon Sep 17 00:00:00 2001 From: Muhammad Zain Ul Abiddin Date: Thu, 14 Mar 2024 00:22:33 +0500 Subject: [PATCH 1/2] Category functionality fix --- commands/unwatch.ts | 10 +++++++++ commands/watch.ts | 3 ++- common/amazon.ts | 53 +++++++++++++++++++++++++++++++-------------- common/watcher.ts | 2 +- common/watchlist.ts | 5 ++++- global.d.ts | 2 +- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/commands/unwatch.ts b/commands/unwatch.ts index a1ff1a1..0f1f7b3 100644 --- a/commands/unwatch.ts +++ b/commands/unwatch.ts @@ -36,4 +36,14 @@ async function run(bot: Client, message: Message, args: string[]) { message.channel.send('Successfully removed item: ' + item.query) return } + + if (item.type === 'category') { + // @ts-ignore + const catName = item.category || item.link + // @ts-ignore + await removeWatchlistItem(item.category) + // @ts-ignore + message.channel.send('Successfully removed item: ' + catName) + return + } } \ No newline at end of file diff --git a/commands/watch.ts b/commands/watch.ts index 788da23..1ea7d2a 100644 --- a/commands/watch.ts +++ b/commands/watch.ts @@ -145,7 +145,8 @@ async function run(bot: Client, message: Message, args: string[]) { cache: results.list.splice(0, cache_limit) }) - response = `Successfully added category: ${processed.category}` + // Cannot figure out processed.category fix + response = `Successfully added category: ${results.name}` break } diff --git a/common/amazon.ts b/common/amazon.ts index 9a8c097..28ac9f3 100644 --- a/common/amazon.ts +++ b/common/amazon.ts @@ -61,8 +61,10 @@ export async function category(url: string) { const tld = url.split('amazon.')[1].split('/')[0] const path = url.split(tld + '/')[1].split('?')[0] + console.log(`https://www.amazon.${tld}/${path}/?ie=${ie}&node=${node}`); + // Get parsed page with puppeteer/cheerio - const $ = await getPage(`https://www.amazon.${tld}/${path}/?ie=${ie}&node=${node}`).catch(e => { + const $ = await getPage(url).catch(e => { debug.log(e, 'error') }) @@ -71,33 +73,52 @@ export async function category(url: string) { debug.log('Detected category', 'debug') const categoryObj: Category = { - name: $('.bxw-pageheader__title h1').text().trim(), + name: $('#nav-search-label-id').text().trim(), link: url, list: [], node - } + } const topRated = $('.octopus-best-seller-card .octopus-pc-card-content li.octopus-pc-item').toArray() - // @ts-expect-error - categoryObj.list = topRated.map(() => { - const item = $(this).find('.octopus-pc-item-link') - const asin = item.attr('href').split('/dp/')[1].split('?')[0].replace(/\//g, '') - const name = item.attr('title') - const priceFull = $(this).find('.octopus-pc-asin-price').text().trim() - const price = priceFormat(priceFull.replace(/[a-zA-Z]/g, '')) - - return { + for(let i = 0; i < topRated.length; i++) { + const item = $(topRated[i]); + const name = item.find('.octopus-pc-item-link').attr('title'); + const asin = item.find('.octopus-pc-item-link').attr('href').split('/dp/')[1].split('?')[0].replace(/\//g, ''); + const priceFull = item.find('.octopus-pc-asin-price .a-offscreen').text().trim() + const price = priceFormat(priceFull.replace(/[a-zA-Z]/g, '')); + categoryObj.list.push({ fullTitle: name, fullLink: `https://amazon.${tld}/dp/${asin}/`, - asin: asin, + asin, price: price.includes('NaN') ? '' : price, lastPrice: parseFloat(price) || 0, symbol: priceFull.replace(/[,.]+/g, '').replace(/[\d a-zA-Z]/g, ''), - image: $(this).find('.octopus-pc-item-image').attr('src'), + image: item.find('.octopus-pc-item-image').attr('src'), node - } - }) + }) + } + + + // // @ts-expect-error + // categoryObj.list = topRated.map(() => { + // const item = $('.octopus-pc-item-link') + // const asin = item.attr('href').split('/dp/')[1].split('?')[0].replace(/\//g, '') + // const name = item.attr('title') + // const priceFull = $(this).find('.octopus-pc-asin-price').text().trim() + // const price = priceFormat(priceFull.replace(/[a-zA-Z]/g, '')) + + // return { + // fullTitle: name, + // fullLink: `https://amazon.${tld}/dp/${asin}/`, + // asin: asin, + // price: price.includes('NaN') ? '' : price, + // lastPrice: parseFloat(price) || 0, + // symbol: priceFull.replace(/[,.]+/g, '').replace(/[\d a-zA-Z]/g, ''), + // image: $(this).find('.octopus-pc-item-image').attr('src'), + // node + // } + // }) // Node set for validation categoryObj.node = node diff --git a/common/watcher.ts b/common/watcher.ts index 090ae95..3115cfd 100644 --- a/common/watcher.ts +++ b/common/watcher.ts @@ -105,7 +105,7 @@ async function itemCheck(product: LinkItem) { async function categoryCheck(cat: CategoryItem) { let total = 0 - + // First, get current items in category for comparison const newItems = await category(cat.link) diff --git a/common/watchlist.ts b/common/watchlist.ts index c64f3a9..a2e835b 100644 --- a/common/watchlist.ts +++ b/common/watchlist.ts @@ -36,8 +36,11 @@ export const removeWatchlistItem = async (url: string) => { if (x.type === 'link') return x.link === url // @ts-ignore if (x.type === 'query') return x.query === url + // @ts-ignore + if (x.type === 'category' && x.category === url) return x.category === url + // @ts-ignore + if (x.type === 'category' && x.link === url) return x.link === url }) - if (item) watchlist.splice(watchlist.indexOf(item), 1) fs.writeFileSync(watchFile, JSON.stringify(watchlist), 'utf-8') diff --git a/global.d.ts b/global.d.ts index 89bc46f..bb1259f 100644 --- a/global.d.ts +++ b/global.d.ts @@ -101,7 +101,7 @@ interface CategoryData { lastPrice: number; symbol: string; image: string; - node: null; + node: string; } interface PartialProductInfo { From ce0fe7d278ce1e22c91d45902f18edf45ad87cf8 Mon Sep 17 00:00:00 2001 From: Muhammad Zain Ul Abiddin Date: Sat, 16 Mar 2024 23:09:07 +0500 Subject: [PATCH 2/2] Fix issues --- common/amazon.ts | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/common/amazon.ts b/common/amazon.ts index 28ac9f3..ec800ee 100644 --- a/common/amazon.ts +++ b/common/amazon.ts @@ -61,7 +61,6 @@ export async function category(url: string) { const tld = url.split('amazon.')[1].split('/')[0] const path = url.split(tld + '/')[1].split('?')[0] - console.log(`https://www.amazon.${tld}/${path}/?ie=${ie}&node=${node}`); // Get parsed page with puppeteer/cheerio const $ = await getPage(url).catch(e => { @@ -79,13 +78,13 @@ export async function category(url: string) { node } - const topRated = $('.octopus-best-seller-card .octopus-pc-card-content li.octopus-pc-item').toArray() - - for(let i = 0; i < topRated.length; i++) { - const item = $(topRated[i]); + const topRated = $('.octopus-best-seller-card .octopus-pc-card-content li.octopus-pc-item').toArray(); + + topRated.forEach((element) => { + const item = $(element); const name = item.find('.octopus-pc-item-link').attr('title'); const asin = item.find('.octopus-pc-item-link').attr('href').split('/dp/')[1].split('?')[0].replace(/\//g, ''); - const priceFull = item.find('.octopus-pc-asin-price .a-offscreen').text().trim() + const priceFull = item.find('.octopus-pc-asin-price .a-offscreen').text().trim(); const price = priceFormat(priceFull.replace(/[a-zA-Z]/g, '')); categoryObj.list.push({ fullTitle: name, @@ -96,31 +95,9 @@ export async function category(url: string) { symbol: priceFull.replace(/[,.]+/g, '').replace(/[\d a-zA-Z]/g, ''), image: item.find('.octopus-pc-item-image').attr('src'), node - }) - } - - - // // @ts-expect-error - // categoryObj.list = topRated.map(() => { - // const item = $('.octopus-pc-item-link') - // const asin = item.attr('href').split('/dp/')[1].split('?')[0].replace(/\//g, '') - // const name = item.attr('title') - // const priceFull = $(this).find('.octopus-pc-asin-price').text().trim() - // const price = priceFormat(priceFull.replace(/[a-zA-Z]/g, '')) - - // return { - // fullTitle: name, - // fullLink: `https://amazon.${tld}/dp/${asin}/`, - // asin: asin, - // price: price.includes('NaN') ? '' : price, - // lastPrice: parseFloat(price) || 0, - // symbol: priceFull.replace(/[,.]+/g, '').replace(/[\d a-zA-Z]/g, ''), - // image: $(this).find('.octopus-pc-item-image').attr('src'), - // node - // } - // }) - - // Node set for validation + }); + }); + categoryObj.node = node return categoryObj