import { email } from "https://esm.town/v/std/email?v=9";
import { fetch } from "https://esm.town/v/std/fetch?v=4";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { hnJobToText } from "https://esm.town/v/yeskunall/hnJobToText?v=2";
const hnEmail = async ({
posts,
footer,
}: {
posts: any[];
footer?: boolean;
}) => {
const footer_ = [
"Unsubscribe by clearing your interval here: https://val.town/@me.intervals",
];
const postsAsText = await Promise.all(
posts.map(hnJobToText),
);
const subject = `👨🏽💻 (${posts.length}) jobs found`;
const text = [
postsAsText.join("\n\n"),
...(footer === false ? [] : footer_),
].join("\n\n");
return { subject, text };
};
export async function hnLatestJobs() {
const jobStories: Number[] = await fetch(
"https://hacker-news.firebaseio.com/v0/jobstories.json",
).then((res) => res.json());
const posts = await Promise.all(
jobStories.map(async (id) => {
return await fetchJSON(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`,
);
}),
);
const { subject, text } = await hnEmail({
posts,
});
return email({ subject, text });
}