import { diffHtml } from "https://esm.town/v/chet/diffHtml";
import { blob } from "https://esm.town/v/std/blob?v=11";
import { email } from "https://esm.town/v/std/email?v=11";
import { fetch } from "https://esm.town/v/std/fetch";
export async function watchWebsite(url: string) {
const newHtml = await fetch(url).then(r => r.text());
const key = "watch:" + url;
let oldHtml = "";
try {
oldHtml = await blob.get(key).then(r => r.text());
} catch (error) {}
await blob.set(key, newHtml);
if (!oldHtml) return console.log("NO OLD", { oldHtml, newHtml });
const diff = diffHtml(oldHtml, newHtml);
if (!diff) return console.log("NO DIFF", { oldHtml, newHtml });
console.log("DIFF", diff);
email({ subject: `Diff for ${url}`, text: diff });
}