Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Kungfu Panda Card #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const available_cards = {
"/travel-destinations-card": require("./src/cards/travel_destinations"),
"/french-word-of-the-day-card": require("./src/cards/french_word_of_the_day"),
"/health-tip-card": require("./src/cards/health-tip-card"),
"/kungfu-panda": require("./src/cards/kungfu-panda-quotes"),
};

app.use(express.json());
Expand Down
43 changes: 43 additions & 0 deletions src/cards/kungfu-panda-quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const express = require("express");
const router = express.Router();
const fs = require("fs").promises;
const { generateCard, CARD_AGE, Languages } = require("../card-generator");
const { parseOptions } = require("../options-parser");
const DATA_FILE_PATH = "./src/data/kungfu_panda_quotes.json";
const DEFAULT_THEME = "pandaDark";

const handleTheme = (req, res, next) => {
req.theme = req.query.theme || DEFAULT_THEME;
next();
};
const handleOptions = (req, res, next) => {
if (req.theme === "custom") {
req.options = parseOptions(req.query);
}
next();
};

router.get("/", handleTheme, handleOptions, async (req, res) => {
try {
const quotes = JSON.parse(await fs.readFile(DATA_FILE_PATH, "utf-8"));
const random_quotes = quotes[Math.floor(Math.random() * quotes.length)];
let quote_content = `${random_quotes.quote}\n\n- ${random_quotes.author}`;
const quote_card = await generateCard(
quote_content,
req.theme,
req.options,
Languages.ENGLISH
);

res.writeHead(200, {
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CARD_AGE}`,
});
res.end(quote_card);
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});

module.exports = router;
Loading