|
Cool project! Seems a bit complex though, compared to doing a shell script showing a notification or sending yourself an email each morning when you open it? Or just doing a light script on val.town? For instance this could be an example val.town script that does something similar (just need to bind to a data source for the dictionary) import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
import { OpenAI } from "https://esm.town/v/std/openai";
import { email } from "https://esm.town/v/std/email";
// Dutch words database
const dutchWords = [
{ word: "boek", translation: "book" },
{ word: "huis", translation: "house" },
{ word: "boom", translation: "tree" },
{ word: "water", translation: "water" },
{ word: "kat", translation: "cat" },
{ word: "hond", translation: "dog" },
{ word: "appel", translation: "apple" },
{ word: "tafel", translation: "table" },
{ word: "school", translation: "school" },
{ word: "fiets", translation: "bicycle" }
];
export default async function generateDutchWordLearning() {
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
const openai = new OpenAI();
// Ensure SQLite table exists
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_dutch_words (
word TEXT PRIMARY KEY,
translation TEXT,
example TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// Fetch words not previously used
const usedWords = await sqlite.execute(`
SELECT word FROM ${KEY}_dutch_words
`);
const availableWords = dutchWords.filter(
w => !usedWords.rows.some(row => row.word === w.word)
);
if (availableWords.length < 3) {
// Reset if we've used all words
await sqlite.execute(`DELETE FROM ${KEY}_dutch_words`);
availableWords = dutchWords;
}
// Randomly select 3 unique words
const selectedWords = [];
for (let i = 0; i < 3; i++) {
const randomIndex = Math.floor(Math.random() * availableWords.length);
selectedWords.push(availableWords.splice(randomIndex, 1)[0]);
}
// Generate example sentences with ChatGPT
const wordDetails = await Promise.all(selectedWords.map(async (wordObj) => {
const exampleResponse = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{
role: "user",
content: `Geef een voorbeeld zin met het woord "${wordObj.word}" in het Nederlands.`
}]
});
const example = exampleResponse.choices[0].message.content || "Geen voorbeeld gevonden";
// Store in database
await sqlite.execute(`
INSERT INTO ${KEY}_dutch_words (word, translation, example)
VALUES (?, ?, ?)
`, [wordObj.word, wordObj.translation, example]);
return { ...wordObj, example };
}));
// Prepare HTML email
const htmlContent = `
<html>
<body>
<h2>Dutch Word Learning </h2>
${wordDetails.map(w => `
<div>
<h3>${w.word} (${w.translation})</h3>
<p><em>Example:</em> ${w.example}</p>
</div>
`).join('')}
</body>
</html>
`;
// Send email
await email({
subject: "Your Daily Dutch Words ",
html: htmlContent,
text: wordDetails.map(w =>
`${w.word} (${w.translation}): ${w.example}`
).join('\n')
});
return wordDetails;
}
|