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

Travel Destination Cards #100

Merged
merged 8 commits into from
Oct 15, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Here is a list of cards that can currently be used:
| `got-quotes-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/got-quotes-card?theme=dark)` |
| `security-tips-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/security-tips-card?theme=dark)` |
| `french-word-of-the-day-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/french-word-of-the-day-card)` |
| `travel-destinations-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/travel-destinations-card?theme=dark)` |

## Themes

Expand Down
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ const available_cards = {
"/motivational-quotes-card": require("./src/cards/motivational-quote"),
"/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"),
"/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"),
"/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"),
"/top-tweets-card": require("./src/cards/top-tweets"),
"/github-facts-card": require("./src/cards/github-facts"),
"/security-tips-card": require("./src/cards/security-tips")
"/security-tips-card": require("./src/cards/security-tips"),
"/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"),
"/harry-potter-spell-card": require("./src/cards/harry-potter-spells"),
"/travel-destinations-card": require("./src/cards/travel_destinations"),
"/french-word-of-the-day-card": require("./src/cards/french_word_of_the_day"),
};

Expand All @@ -28,16 +29,16 @@ app.use(express.urlencoded({ extended: true }));

app.use("/", require("./src/help"));

for(const key in available_cards) {
if(available_cards.hasOwnProperty(key)) {
for (const key in available_cards) {
if (available_cards.hasOwnProperty(key)) {
app.use(key, available_cards[key]);
}
}

app.get("/random-card", (req,res) => {
app.get("/random-card", (req, res) => {
let urls = Object.keys(available_cards);
let query = "";
if(req.originalUrl.indexOf("?") > -1) {
if (req.originalUrl.indexOf("?") > -1) {
const queryParameters = req.originalUrl.split("?")[1];
query = `?${queryParameters}`;
}
Expand Down
48 changes: 48 additions & 0 deletions src/cards/travel_destinations.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/travel_destinations.json";
const DEFAULT_THEME = "dark";

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 destinationData = await fs.readFile(DATA_FILE_PATH, "utf8");
const destinationArray = JSON.parse(destinationData);
const randomDestination =
destinationArray[Math.floor(Math.random() * destinationArray.length)];
const destinationContent = `Destination of the Day:\n${randomDestination.destination}\nFact: ${randomDestination.fact}`;

const destinationCard = await generateCard(
destinationContent,
req.theme,
req.options,
Languages.ENGLISH
);

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

module.exports = router;
Loading
Loading