Skip to content

Commit

Permalink
feat: show coupon in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Nov 23, 2023
1 parent 929e0dd commit 6e8894d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
16 changes: 11 additions & 5 deletions commands/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ async function run(bot: Client, message: Message, args: string[]) {

if (parsedArgs.priceLimit && parseFloat(result.price) > (parsedArgs.priceLimit as number)) continue

embed.addFields([{
name: trim(result.fullTitle, 50),
value: `${parseFloat(result.price) ? result.symbol + result.price : 'Not in stock'} - ${result.fullLink}`,
inline: false
}])
const priceWithCoupon = result.coupon > 0 ? parseFloat(result.price) - result.coupon : result.price

embed.addFields([
{
name: trim(result.fullTitle, 50),
value: `${parseFloat(result.price) ? `${result.symbol + (
result.coupon > 0 ? `${priceWithCoupon} (${result.symbol + result.coupon.toFixed(2)} coupon)` : result.price
)}` : 'Not in stock'} - ${result.fullLink}`,
inline: false
},
])
}

message.channel.send({
Expand Down
6 changes: 4 additions & 2 deletions common/amazon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ export async function search(query: string, suffix: string) {
$('.s-result-list').find('.s-result-item').each(function () {
if (results.length >= limit) return

const link = $(this).find('.a-link-normal[href*="/dp/"]').attr('href')
const link = '/dp/' + $(this).find('.a-link-normal[href*="/dp/"]').first().attr('href')?.split('/dp/')[1].split('?')[0]

if (!link) return
if (!link || link.includes('undefined')) return

const asin = linkToAsin(link)
const priceString = $(this).find('.a-price').find('.a-offscreen').first().text().trim()
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, ''))

results.push({
fullTitle: $(this).find('span.a-text-normal').text().trim(),
ratings: $(this).find('.a-icon-alt').text().trim(),
coupon: maybeCoupon.includes('NaN') ? -1 : parseFloat(maybeCoupon),
price: price.includes('NaN') ? '' : price,
lastPrice: parseFloat(price) || 0,
symbol: priceString.replace(/[,.]+/g, '').replace(/[\d a-zA-Z]/g, ''),
Expand Down
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Watchlist = Array<LinkItem | CategoryItem | QueryItem>
interface SearchData {
fullTitle: string;
ratings: string;
coupon: number;
price: string;
lastPrice: number;
symbol: string;
Expand Down

0 comments on commit 6e8894d

Please sign in to comment.