Skip to content

Commit

Permalink
feat: apply coupons to price detection
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Nov 23, 2023
1 parent 6e8894d commit 3555e16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions common/amazon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function search(query: string, suffix: string) {
const sanq = query.replace(/ /g, '+')
const url = `https://www.amazon.${suffix}/s?k=${sanq}`
const results: SearchData[] = []
const foundAsins: string[] = []

const $ = await getPage(url)
const limit = $('.s-result-list').find('.s-result-item').length
Expand All @@ -28,6 +29,10 @@ export async function search(query: string, suffix: string) {
const price = priceFormat($(this).find('.a-price').find('.a-offscreen').first().text().trim().replace(/[a-zA-Z]/g, ''))
const maybeCoupon = priceFormat($(this).find('.s-coupon-unclipped span').first().text().trim().replace(/[a-zA-Z]/g, ''))

// prevent duplicates
if (foundAsins.includes(asin)) return
foundAsins.push(asin)

results.push({
fullTitle: $(this).find('span.a-text-normal').text().trim(),
ratings: $(this).find('.a-icon-alt').text().trim(),
Expand Down
4 changes: 3 additions & 1 deletion common/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export async function sendPriceChange(bot: Client, notification: NotificationDat
name: 'AmazonMonitor'
})
.setThumbnail(notification.image)
.setDescription(`Old Price: ${notification.symbol} ${priceFormat(notification.oldPrice)}\nNew Price: ${notification.symbol} ${notification.newPrice}\n\n${notification.link}`)
.setDescription(`Old Price: ${notification.symbol}${priceFormat(notification.oldPrice)}\nNew Price: ${notification.symbol}${notification.newPrice.toFixed(2) + (
notification.coupon > 0 ? ` (with ${notification.symbol}${notification.coupon.toFixed(2)} coupon)` : ''
)}\n\n${notification.link}`)
.setColor('Green')

const channel = await bot.channels.fetch(notification.channelId)
Expand Down
14 changes: 12 additions & 2 deletions common/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function itemCheck(product: LinkItem) {
difference: product.difference || null,
symbol: newData?.symbol,
image: newData?.image,
coupon: 0
}
] as NotificationData[]
}
Expand Down Expand Up @@ -135,6 +136,7 @@ async function categoryCheck(cat: CategoryItem) {
difference: cat.difference || null,
symbol: item.symbol,
image: item?.image,
coupon: 0,
})
}
})
Expand Down Expand Up @@ -169,11 +171,18 @@ async function queryCheck(query: QueryItem) {

if (matchingObj.lastPrice === item.lastPrice) return

if (item.lastPrice > matchingObj.lastPrice) {
// if the obj has a coupon, modify the lastprice to reflect that
if (matchingObj.coupon > 0) {
matchingObj.lastPrice -= matchingObj.coupon
}

const newPriceWithCoupon = item.coupon > 0 ? item.lastPrice - item.coupon : item.lastPrice

if (newPriceWithCoupon < matchingObj.lastPrice) {
notifications.push({
itemName: item.fullTitle,
oldPrice: matchingObj.lastPrice,
newPrice: item.lastPrice,
newPrice: newPriceWithCoupon,
link: item.fullLink,
guildId: query.guildId,
channelId: query.channelId,
Expand All @@ -182,6 +191,7 @@ async function queryCheck(query: QueryItem) {
difference: query.difference || null,
symbol: item.symbol,
image: item?.image,
coupon: matchingObj.coupon,
})
}
})
Expand Down
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface Command {

interface NotificationData {
itemName: string
coupon: number
oldPrice: number
newPrice: number
symbol: string
Expand Down

0 comments on commit 3555e16

Please sign in to comment.