Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Category functionality fix #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions commands/unwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 2 additions & 1 deletion commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 18 additions & 20 deletions common/amazon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion common/watchlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ interface CategoryData {
lastPrice: number;
symbol: string;
image: string;
node: null;
node: string;
}

interface PartialProductInfo {
Expand Down