Skip to content

Instantly share code, notes, and snippets.

@neroist
Last active February 17, 2022 13:19
Show Gist options
  • Save neroist/4c94795b88c4dce32cf5407b909de389 to your computer and use it in GitHub Desktop.
Save neroist/4c94795b88c4dce32cf5407b909de389 to your computer and use it in GitHub Desktop.
BreakFast API Console Application - Written in Nim
## 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
@neroist
Copy link
Author

neroist commented Feb 16, 2022

Updated due to update in the BreakFast API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment