Last active
February 17, 2022 13:19
-
-
Save neroist/4c94795b88c4dce32cf5407b909de389 to your computer and use it in GitHub Desktop.
BreakFast API Console Application - Written in Nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## A console application for finding breakfast recipes using the BreakFast API. | |
## If you're building this, please compile with -d:ssl | |
from httpclient import newHttpClient, getContent | |
from unicode import title | |
import strformat | |
import strutils | |
import json | |
let | |
client = newHttpClient() | |
recipe = parseJson(client.getContent("https://breakfastapi.fun/"))["recipe"] | |
duration = recipe["total_duration"].getInt() # get recipe duration | |
var | |
dirs = recipe["directions"].getStr().split('.') # may not include proper punctuation or grammar sometimes | |
dirs.delete dirs.find("") # For some reason, there's an empty string in `dirs`. This removes it | |
dirs.add "And enjoy!" # Add message at the end of the directions :) | |
echo "Breakfast name: ", recipe["name"].getStr(), '\n' | |
try: | |
echo &"Cook Time: {duration} minutes ({duration / 60} hours)\n" # Cook Time: x minutes (y hours) | |
except RangeDefect: # Sometimes duration/60 raises a RangeDefect exception due to its quotient | |
echo &"Cook Time: {duration} minutes\n" | |
# ! Be careful of repeating decimals | |
## --- Print ingredients --- | |
echo "Ingriedients:" | |
for i in recipe["ingredients"]: | |
echo " - ", i.getStr().title() # Capitalize each of the words in the ingredient | |
## --- Print directions --- | |
echo "\nDirections: " | |
for num, i in dirs: | |
echo num + 1, ". ", i.strip() | |
echo "" # print newline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated due to update in the BreakFast API.