Avatar

@marksteve

4 public vals
Joined August 11, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { checkEpicTvDiscounts } from "https://esm.town/v/marksteve/checkEpicTvDiscounts";
import { epicTvWatchList } from "https://esm.town/v/marksteve/epicTvWatchList";
export const watchEpicTvDiscounts = async (interval: Interval) => {
const discounts = [];
for (
let [user, { secret, watchList }] of Object.entries(
epicTvWatchList,
)
) {
discounts.push(await checkEpicTvDiscounts(user, secret, watchList));
}
return discounts;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
import { fetch } from "https://esm.town/v/std/fetch";
export let getEpicTvProduct = async (url) => {
const cheerio = await import("npm:cheerio@1.0.0-rc.12");
const html = await (await fetch(url)).text();
const $ = cheerio.load(html);
return {
name: $(".page-title").text().trim(),
discount: $(".sale-value").text().trim(),
price: $('[data-price-type="finalPrice"]').text().trim(),
oldPrice: $('[data-price-type="oldPrice"]:first').text().trim(),
};
};
1
2
3
4
5
6
7
8
9
import process from "node:process";
import { telegram as telegram2 } from "https://esm.town/v/stevekrouse/telegram?v=4";
export let telegram = (message, options = undefined) =>
telegram2(
process.env.telegram_me,
message,
options,
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { epicTvDiscounts } from "https://esm.town/v/marksteve/epicTvDiscounts";
import { getEpicTvProduct } from "https://esm.town/v/marksteve/getEpicTvProduct";
import { set } from "https://esm.town/v/std/set?v=11";
import { telegram } from "https://esm.town/v/stevekrouse/telegram?v=4";
export const checkEpicTvDiscounts = async (user: string, secret: string, watchList: {
url: string;
targetDiscount: number;
}[]) => {
for (let { url, targetDiscount } of watchList) {
const product = await getEpicTvProduct(url);
console.log({ product });
const discount = parseFloat(product.discount);
if (isNaN(discount)) {
console.log(`No discount for ${product.name}`);
continue;
}
if (epicTvDiscounts[url] === discount) {
console.log(`No discount change for ${product.name}`);
continue;
}
else {
epicTvDiscounts[url] = discount;
}
if (parseFloat(product.discount) < targetDiscount) {
console.log(`Target discount not met for ${product.name}`);
continue;
}
telegram(
secret,
`${product.name} is ${product.discount} off!\n${product.oldPrice} ➡️ ${product.price}`,
);
}
await set("epicTvDiscounts", epicTvDiscounts);
return epicTvDiscounts;
};
Next