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..ec800ee 100644 --- a/common/amazon.ts +++ b/common/amazon.ts @@ -61,8 +61,9 @@ export async function category(url: string) { const tld = url.split('amazon.')[1].split('/')[0] const path = url.split(tld + '/')[1].split('?')[0] + // 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,35 +72,32 @@ 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 { + 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 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 - } - }) - - // Node set for validation + }); + }); + categoryObj.node = node return categoryObj 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 {