Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Runs every 1 days
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* This val creates a cron job that sends an email with recent Stack Overflow posts
* mentioning various file upload-related topics.
* It uses the Stack Exchange API to fetch recent questions and the Val Town email API to send the digest.
*/
import { StackExchange } from "https://esm.sh/@userscripters/stackexchange-api-types";
// Array of topics related to file uploads
const TOPICS = [
"file-upload",
"multipart-form-data",
"image-upload",
"s3",
"uploadcare",
];
// Function to fetch recent questions from Stack Overflow for multiple topics
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);
}
// Remove duplicates (in case a question has multiple tags from our topics)
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 to format questions into HTML
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("");
}
// Main cron job function
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();
}
August 23, 2024