Skip to content

Commit

Permalink
Merge pull request #77 from HritvikBhatia/GOT-quotes
Browse files Browse the repository at this point in the history
game of thrones quotes
  • Loading branch information
akanshSirohi authored Oct 12, 2024
2 parents ebe4576 + 7d8fdb3 commit bfdd417
Show file tree
Hide file tree
Showing 5 changed files with 564 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Here is a list of cards that can currently be used:
| `fun-fact-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/fun-fact-card?theme=dark)` |
| `github-facts-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/github-facts-card?theme=dark)` |
| `random-facts-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/random-facts-card?theme=dark)` |
| `breaking-bad-quote-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/breaking-bad-quote-card?theme=dark)` |
| `got-quotes-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/got-quotes-card?theme=dark)` |


## Themes
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const available_cards = {
"/github-facts-card": require("./src/cards/github-facts"),
"/random-facts-card": require("./src/cards/random-facts"),
"/fun-fact-card": require("./src/cards/fun-fact-card"),
"/got-quotes-card": require("./src/cards/got-quotes"),
};

app.use(express.json());
Expand Down
47 changes: 47 additions & 0 deletions src/cards/got-quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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/got-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 gotquotes = JSON.parse(await fs.readFile(DATA_FILE_PATH, "utf8"));
const random_quotes = gotquotes[Math.floor(Math.random() * gotquotes.length)];

const quotes_content = `${random_quotes.sentence}\n\n- ${random_quotes.character}`;
const got_card = await generateCard(
quotes_content,
req.theme,
req.options,
Languages.ENGLISH
);

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

module.exports = router;
Loading

0 comments on commit bfdd417

Please sign in to comment.