-
Notifications
You must be signed in to change notification settings - Fork 18
Diana C. #3
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
base: main
Are you sure you want to change the base?
Diana C. #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,77 @@ | ||
| // API documentation: https://www.thecocktaildb.com/api.php | ||
|
|
||
| import path from 'path'; | ||
| import path from "path"; | ||
| import fsPromises from "node:fs/promises"; | ||
|
|
||
| const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; | ||
| const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1"; | ||
|
|
||
| // Add helper functions as needed here | ||
|
|
||
| function getAlcoString(strAlcoholic) { | ||
| if (strAlcoholic === "Alcoholic") { | ||
| return "Yes"; | ||
| } else { | ||
| return "No"; | ||
| } | ||
| } | ||
|
|
||
| function generateMarkdown(data) { | ||
| const drinks = data.drinks; | ||
| let markdown = "# Cocktail Recipes"; | ||
| for (const drink of drinks) { | ||
| markdown += generateDrinkMarkdown(drink); | ||
| } | ||
| return markdown; | ||
| } | ||
|
|
||
| function getIngredients(drink) { | ||
| let ingredients = ""; | ||
|
|
||
| for (let i = 1; i <= 15; i++) { | ||
| const ingredient = drink["strIngredient" + i]; | ||
| const measure = (drink["strMeasure" + i] ?? "").trim(); | ||
|
|
||
| if (!ingredient) break; | ||
| if (measure) { | ||
| ingredients += `- ${measure} ${ingredient}\n`; | ||
| } else { | ||
| ingredients += `- ${ingredient}\n`; | ||
| } | ||
| } | ||
|
|
||
| return ingredients; | ||
| } | ||
|
|
||
| function generateDrinkMarkdown(drink) { | ||
| return ` | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These empty lines are visible in the markdown |
||
|
|
||
| ## ${drink.strDrink} | ||
|
|
||
|  | ||
|
|
||
| **Category**: ${drink.strCategory} | ||
|
|
||
| **Alcoholic**: ${getAlcoString(drink.strAlcoholic)} | ||
|
|
||
| ### Ingredients | ||
|
|
||
| ${getIngredients(drink)} | ||
|
|
||
|
|
||
| ### Instructions | ||
|
|
||
| ${drink.strInstructions} | ||
|
|
||
| Serve in: ${drink.strGlass} | ||
|
|
||
|
|
||
| `; | ||
| } | ||
|
|
||
| export async function main() { | ||
| if (process.argv.length < 3) { | ||
| console.error('Please provide a cocktail name as a command line argument.'); | ||
| console.error("Please provide a cocktail name as a command line argument."); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -20,10 +83,21 @@ export async function main() { | |
|
|
||
| try { | ||
| // 1. Fetch data from the API at the given URL | ||
|
|
||
| const responce = await fetch(url); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!responce.ok) { | ||
| throw new Error("not ok" + responce.status); | ||
| } | ||
| const data = await responce.json(); | ||
| console.log(JSON.stringify(data.drinks[0], null, 2)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug console.logs should be deleted from the file when the development is finished. |
||
| // 2. Generate markdown content to match the examples | ||
| const str = generateMarkdown(data); | ||
|
|
||
| // 3. Write the generated content to a markdown file as given by outPath | ||
| await fsPromises.writeFile(outPath, str); | ||
| } catch (error) { | ||
| // 4. Handle errors | ||
| console.error("No cocktails found with that name."); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Cocktail Recipes | ||
|
|
||
|
|
||
| ## Margarita | ||
|
|
||
|  | ||
|
|
||
| **Category**: Ordinary Drink | ||
|
|
||
| **Alcoholic**: Yes | ||
|
|
||
| ### Ingredients | ||
|
|
||
| - 1 1/2 oz Tequila | ||
| - 1/2 oz Triple sec | ||
| - 1 oz Lime juice | ||
| - Salt | ||
|
|
||
|
|
||
|
|
||
| ### Instructions | ||
|
|
||
| Rub the rim of the glass. | ||
|
|
||
| Serve in: Cocktail glass | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
drinksreturnednull, the function will still try tofor...ofover null, which will throw a TypeError.