From 38f66f5bb5238c7cbb42a7ad07b9b1a652edfd2b Mon Sep 17 00:00:00 2001 From: spinach <19keegandeppe@gmail.com> Date: Sat, 4 Mar 2023 17:03:56 -0500 Subject: [PATCH] working baseline with force, daily and random quotes --- quotes.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 quotes.sh diff --git a/quotes.sh b/quotes.sh new file mode 100755 index 0000000..7e5dfc7 --- /dev/null +++ b/quotes.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +usage() { + cat < "daily_$TODAY.json" + fi + + cat "daily_$TODAY.json" +} + +random() { + # have to be careful not to exceed 10 requests per minute + curl -fs "$API/random" | gojq '.[0]' +} + +get_quote() { + if [ "$1" == "today" ] ; then + daily + elif [ "$1" == "random" ] ; then + random + else + if [[ ! -f "quotes.json" || "$UPDATE" == true ]] ; then + # update quotes file + curl -fs "$API/quotes" > quotes.json + fi + + num=$(($RANDOM % 49)) + cat quotes.json | gojq ".[$num]" + fi +} + +# parse long form +for arg in "$@"; do + shift + case "$arg" in + '--help') set -- "$@" '-h' ;; + '--random') set -- "$@" '-r' ;; + '--daily') set -- "$@" '-d' ;; + '--force') set -- "$@" '-f' ;; + *) set -- "$@" "$arg" ;; + esac +done + +# parse args +while getopts "hrdf" opt ; do + case "$opt" in + 'h') + usage + exit 0 + ;; + 'r') + [ -n "$TYPE" ] && usage 1 || TYPE="random" + ;; + 'd') + [ -n "$TYPE" ] && usage 1 || TYPE="today" + ;; + 'f') + UPDATE=true + ;; + '?') + usage + exit 1 + ;; + esac +done + +QUOTE=$(get_quote "$TYPE") + +Q=$(echo "$QUOTE" | gojq '.q' | tr -d '"') +A=$(echo "$QUOTE" | gojq '.a' | tr -d '"') + +printf '"%s" - %s\n' "$Q" "$A"