Skip to content

Commit

Permalink
Merge pull request #70 from HritvikBhatia/breaking-bad-quotes
Browse files Browse the repository at this point in the history
Breaking bad quotes
  • Loading branch information
akanshSirohi authored Oct 10, 2024
2 parents 25d22e6 + e92a449 commit 4ba4b58
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.prettierignore
.idea
.idea
package-lock.json
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const available_cards = {
"/word-of-the-day-card": require("./src/cards/word_of_the_day"),
"/challenge-of-the-week-card": require("./src/cards/challenge-of-the-week"),
"/team-work-quote-card" : require("./src/cards/team-work-quote"),
"/breaking-bad-quote-card" : require("./src/cards/breaking-bad-quotes"),
"/bhagavad-geeta-card" : require("./src/cards/bhagavad-geeta-quotes"),
"/programming-facts-card": require("./src/cards/programming-facts"),
"/spanish-quote-card": require("./src/cards/spanish-quote"),
Expand Down
48 changes: 48 additions & 0 deletions src/cards/breaking-bad-quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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/breaking-bad-quotes.json";
const DEFAULT_THEME = "dark_2";

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 breakingbadQuotesData = JSON.parse(
await fs.readFile(DATA_FILE_PATH, "utf8")
);
const randomQuote = breakingbadQuotesData[Math.floor(Math.random() * breakingbadQuotesData.length)];
const quoteContent = `"${randomQuote.quote}"\n\nAuthor- ${randomQuote.author}`;

const quoteCard = await generateCard(
quoteContent,
req.theme,
req.options,
Languages.ENGLISH
);

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

module.exports = router;
Loading

0 comments on commit 4ba4b58

Please sign in to comment.