Skip to content

Commit

Permalink
Added quote of the moment page
Browse files Browse the repository at this point in the history
  • Loading branch information
jollytoad committed Mar 13, 2024
1 parent f1de03f commit 7ac622e
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 9 deletions.
7 changes: 7 additions & 0 deletions cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as quote_cron from "./routes/quote/_cron/generate_quote.ts";

// TODO: discover all _cron modules and generate this function

export default function initCron() {
Deno.cron(quote_cron.name, quote_cron.schedule, quote_cron.default);
}
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"unstable": [
"kv"
"kv",
"cron"
],
"tasks": {
"build": "deno run --allow-all scripts/build.ts",
Expand Down
176 changes: 171 additions & 5 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import handler from "./handler.ts";
import init from "@http/fns/hosting/init_deploy";
import initCron from "./cron.ts";

await initCron();

await Deno.serve(await init(handler)).finished;
1 change: 1 addition & 0 deletions routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default cascade(
byPattern("/sse/feed", lazy(() => import("./routes/sse/feed.tsx"))),
byPattern("/sse", lazy(() => import("./routes/sse/index.tsx"))),
byPattern("/sleep", lazy(() => import("./routes/sleep.ts"))),
byPattern("/quote", lazy(() => import("./routes/quote/index.tsx"))),
byPattern("/quiz/answer/:id/:answer", lazy(() => import("./routes/quiz/answer/:id/:answer.tsx"))),
byPattern("/quiz", lazy(() => import("./routes/quiz/index.tsx"))),
byPattern("/ex/:from/:to", lazy(() => import("./routes/ex/:from/:to.tsx"))),
Expand Down
9 changes: 6 additions & 3 deletions routes/blog/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
This site is powered by...

- [Deno](https://deno.land) - the JS/TS runtime that isn't Node
- [JSR](https://jsr.io/) - the new registry that supplies many of the dependencies
- [JSR](https://jsr.io/) - the new registry that supplies many of the
dependencies
- [Deno Deploy](https://deno.com/deploy) - the Deno based serverless hosting
service
- [htmx](https://htmx.org) - used for many of the interactive demos
- [missing.css](https://missing.style) - css library
- [@http/fns](https://jsr.io/@http/fns) - my HTTP server functions library (for routing etc)
- [@http/jsx-stream](https://jsr.io/@http/jsx-stream) - my JSX streaming serializer
- [@http/fns](https://jsr.io/@http/fns) - my HTTP server functions library (for
routing etc)
- [@http/jsx-stream](https://jsr.io/@http/jsx-stream) - my JSX streaming
serializer
- [remark](https://remark.js.org) - the markdown processor (although I use
[mdast](https://github.com/syntax-tree/mdast) and it's utilities directly)
- [esbuild](https://esbuild.github.io/) - to build the service worker, as not
Expand Down
12 changes: 12 additions & 0 deletions routes/quote/_components/Quote.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getQuote } from "../_lib/quote_store.ts";

export async function Quote() {
const quote = await getQuote();

return (
<blockquote class="quote-of-the-moment">
<p>{quote}</p>
<footer>- a generative AI</footer>
</blockquote>
);
}
31 changes: 31 additions & 0 deletions routes/quote/_cron/generate_quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { setQuote } from "../_lib/quote_store.ts";
import OpenAI from "npm:openai";

export const name = "Generate a new quote of the moment";

export const schedule = Deno.env.get("QUOTE_SCHEDULE") ?? "*/30 * * * *";

export default async function generateQuote() {
console.log("Generating a new quote...");

const openai = new OpenAI();

const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [
{
role: "user",
content:
"Generate a new unique inspirational quote of the day. Do not attribute it to anyone",
},
],
});

const content = completion?.choices[0]?.message.content;

if (content) {
await setQuote(content);
}
}

export const init = generateQuote;
9 changes: 9 additions & 0 deletions routes/quote/_lib/quote_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as store from "$store";

export async function getQuote() {
return await store.getItem<string>(["quote"]) ?? "Nothing to see here";
}

export async function setQuote(quote: string) {
await store.setItem(["quote"], quote);
}
Loading

0 comments on commit 7ac622e

Please sign in to comment.