import { StackExchange } from "https://esm.sh/@userscripters/stackexchange-api-types";
const TOPICS = [
"file-upload",
"multipart-form-data",
"image-upload",
"s3",
"uploadcare",
];
async function fetchRecentQuestions(): Promise<StackExchange.Question[]> {
const allQuestions: StackExchange.Question[] = [];
for (const topic of TOPICS) {
const response = await fetch(
`https://api.stackexchange.com/2.3/questions?order=desc&sort=creation&site=stackoverflow&filter=withbody&tagged=${topic}&pagesize=5`,
);
const data = await response.json();
allQuestions.push(...data.items);
}
return Array.from(new Set(allQuestions.map(q => q.question_id)))
.map(id => allQuestions.find(q => q.question_id === id)!)
.sort((a, b) => b.creation_date - a.creation_date);
}
function formatQuestionsHtml(questions: StackExchange.Question[]): string {
return questions.map(q => `
<h2><a href="${q.link}">${q.title}</a></h2>
<p>${q.body.substring(0, 200)}...</p>
<p>Tags: ${q.tags.join(", ")}</p>
<hr>
`).join("");
}
async function cronJob() {
const { email } = await import("https://esm.town/v/std/email");
const questions = await fetchRecentQuestions();
const htmlContent = formatQuestionsHtml(questions);
await email({
to: "team@pinata.cloud",
subject: "Stack Overflow File Upload Topics Digest",
html: `
<h1>Recent Stack Overflow Posts on File Upload Topics</h1>
<p>Covering topics: ${TOPICS.join(", ")}</p>
${htmlContent}
<p>
<small>
View source: <a href="${import.meta.url.replace("esm.town", "val.town")}">
${import.meta.url.replace("esm.town", "val.town")}
</a>
</small>
</p>
`,
});
}
export default async function(interval: Interval) {
await cronJob();
}