import { blob } from "https://esm.town/v/std/blob?v=12";
import { email } from "https://esm.town/v/std/email";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=6";
export default async function(interval: Interval) {
const lastLakeTemp = await blob.getJSON("lastLakeTemp");
const temperature = await getLakeTemp();
console.log(temperature);
if (!lastLakeTemp) {
await blob.setJSON("lastLakeTemp", temperature);
await email({
subject: `Welcome to Candlewood Lake Temperature Alerts! Current Temperature: ${temperature}°F`,
text:
`Howdy Surfer, \n\nWelcome to Candlewood Lake Temperature Alerts! \n\nThe current temperature of Candlewood Lake is ${temperature}°F. \n\nHappy Surfing!`,
});
console.log("sent email");
return;
}
const change = Math.abs(temperature - lastLakeTemp);
if (change > 1 && temperature < 75) {
await blob.setJSON("lastLakeTemp", temperature);
await email({
subject: `Candlewood Lake Temperature Alert: ${temperature}°F`,
text:
`Sup Surfer! \n\nThe temperature of Candlewood Lake has changed from ${lastLakeTemp}°F to ${temperature}°F. \n\nBrace for frigid waves! `,
});
console.log("sent email");
}
if (change > 1 && temperature >= 75) {
await blob.setJSON("lastLakeTemp", temperature);
await email({
subject: `Candlewood Lake Temperature Alert: ${temperature}°F`,
text:
`Sup Surfer! \n\nThe temperature of Candlewood Lake has changed from ${lastLakeTemp}°F to ${temperature}°F. \n\nWonderful warm waves are in your future! `,
});
console.log("sent email");
}
}
export async function getLakeTemp() {
const result = await fetchText("https://www.omniafishing.com/w/candlewood-lake-3-fishing-reports/current-conditions");
const temperature = result.match(/<strong>([0-9]+)/)[1];
return parseInt(temperature);
}