Skip to content

Commit 2351621

Browse files
authored
A BASH Wrapper for the quizapi.io API
Version: 1.0.0
1 parent db6db61 commit 2351621

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed

Diff for: quiz.sh

+272
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#!/bin/bash
2+
3+
##
4+
# A BASH Wrapper for the quizapi.io API
5+
# Version: 1.0.0
6+
##
7+
8+
##
9+
# Colors
10+
##
11+
green='\e[32m'
12+
blue='\e[34m'
13+
clear='\e[0m'
14+
orange='\e[33m'
15+
red='\e[31m'
16+
17+
##
18+
# Check if jq is installed
19+
##
20+
21+
if ! [ -x "$(command -v jq)" ] ; then
22+
echo "The jq command is required! Please install it and then try again"
23+
exit 1
24+
fi
25+
26+
##
27+
# API URL
28+
##
29+
url='quizapi.io'
30+
quiz_endpoint='api/v1/questions'
31+
32+
##
33+
# Quiz session file
34+
##
35+
temp_quiz=$(mktemp /tmp/temp-quiz.XXXXXX)
36+
37+
##
38+
# Color Functions
39+
##
40+
41+
ColorGreen(){
42+
echo -ne $green$1$clear
43+
}
44+
ColorBlue(){
45+
echo -ne $blue$1$clear
46+
}
47+
ColorRed(){
48+
echo -ne $red$1$clear
49+
}
50+
ColorOrange(){
51+
echo -ne $orange$1$clear
52+
}
53+
54+
##
55+
# Help function
56+
##
57+
function usage() {
58+
echo "Usage: $0 -a API_KEY [-c Category] [-d Difficulty] [-t Tags]" 1>&2; exit 1;
59+
}
60+
61+
##
62+
# Read the arguments with the getopts command
63+
##
64+
while getopts "a:c:d:t:" o; do
65+
case "${o}" in
66+
a)
67+
API_KEY=${OPTARG}
68+
;;
69+
c)
70+
category=${OPTARG}
71+
;;
72+
d)
73+
difficulty=${OPTARG}
74+
;;
75+
t)
76+
tags=${OPTARG}
77+
;;
78+
*)
79+
usage
80+
;;
81+
esac
82+
done
83+
84+
shift $((OPTIND-1))
85+
86+
##
87+
# Check if an API key was specified
88+
##
89+
if [ -z "${API_KEY}" ]; then
90+
usage
91+
fi
92+
93+
##
94+
# Question formatting
95+
##
96+
function get_questions(){
97+
content=$( curl -s ${url}/${quiz_endpoint} -G -d limit=1 -d category=${category} -d difficulty=${difficulty} -d tags=${tags} -H "X-Api-Key: ${API_KEY}" -o ${temp_quiz} )
98+
}
99+
100+
##
101+
# Check if the API request was successful
102+
##
103+
function check_error(){
104+
check_auth=$(cat ${temp_quiz} | jq .error?)
105+
106+
if [ "$check_auth" == '' ] ; then
107+
content_check="1"
108+
else
109+
content_check="0"
110+
fi
111+
}
112+
113+
##
114+
# Check if there are any questions found
115+
##
116+
function check_if_null(){
117+
question_count=$(cat ${temp_quiz})
118+
if [ "$question_count" == "No questions found.." ] ; then
119+
echo "No questions found.."
120+
echo "Try with different category or tag."
121+
rm ${temp_quiz}
122+
exit 1
123+
fi
124+
}
125+
126+
##
127+
# Parse the Json output
128+
##
129+
function format_question(){
130+
#jq .[0].question
131+
132+
question=$( cat ${temp_quiz} | jq .[0].question )
133+
answer_a=$( cat ${temp_quiz} | jq .[0].answers.answer_a )
134+
answer_b=$( cat ${temp_quiz} | jq .[0].answers.answer_b )
135+
answer_c=$( cat ${temp_quiz} | jq .[0].answers.answer_c )
136+
answer_d=$( cat ${temp_quiz} | jq .[0].answers.answer_d )
137+
answer_e=$( cat ${temp_quiz} | jq .[0].answers.answer_e )
138+
answer_f=$( cat ${temp_quiz} | jq .[0].answers.answer_f )
139+
answer_a_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_a_correct )
140+
answer_b_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_b_correct )
141+
answer_c_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_c_correct )
142+
answer_d_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_d_correct )
143+
answer_e_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_e_correct )
144+
answer_f_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_f_correct )
145+
correct=$( cat ${temp_quiz} | jq -r .[0].correct_answer )
146+
multiple_correct_answers=$( cat ${temp_quiz} | jq -r .[0].multiple_correct_answers )
147+
#correct_check=$( echo ${correct} | awk -F'Correct: ' '{ print $2 }' )
148+
correct_check=$( echo ${correct} )
149+
150+
}
151+
152+
##
153+
# Check if multiple possible
154+
##
155+
function multiple_correct_answers(){
156+
if [ $multiple_correct_answers == true ] ; then
157+
echo -ne "$(ColorGreen 'There are multiple answers!')
158+
"
159+
else
160+
echo -ne "$(ColorGreen 'There is only 1 correct answer!' )
161+
"
162+
fi
163+
}
164+
165+
##
166+
# Show answers that do not have null values
167+
##
168+
function check_answers_value(){
169+
options=()
170+
correct_options=()
171+
for x in {a..f} ; do
172+
correct_answers="answer_${x}_correct"
173+
correct_answers_value=$(eval echo "\$$correct_answers")
174+
175+
answer_value="answer_${x}"
176+
answer_value_check=$(eval echo "\$$answer_value")
177+
178+
if ! [ "$answer_value_check" == null ] ; then
179+
#echo $(ColorGreen 'a:)') ${answer_a}
180+
options+=("${answer_value_check}")
181+
if [ $correct_answers_value == true ] ; then
182+
correct_options+=("${answer_value_check}")
183+
fi
184+
fi
185+
done
186+
187+
}
188+
189+
##
190+
# Call all functions
191+
##
192+
function main(){
193+
get_questions > /dev/null
194+
check_error 2>/dev/null
195+
check_if_null 2>/dev/null
196+
format_question 2>/dev/null
197+
check_answers_value 2>/dev/null
198+
}
199+
main
200+
201+
##
202+
# Check if the answer is correct
203+
##
204+
function check_answer() {
205+
answer="answer_${answer_str_lower}_correct"
206+
answer_value=$(eval echo "\$$answer")
207+
208+
if [ "$answer_value" == "true" ] ; then
209+
echo -ne "$(ColorGreen 'Correct Answer' )
210+
" ;
211+
else
212+
echo -ne "$(ColorRed 'Wrong Answer' )
213+
" ;
214+
fi
215+
}
216+
217+
menu() {
218+
echo ""
219+
multiple_correct_answers
220+
echo -ne "
221+
${question}
222+
"
223+
for i in ${!options[@]}; do
224+
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
225+
done
226+
if [[ "$msg" ]]; then echo "$msg"; fi
227+
}
228+
229+
##
230+
# Print Question if the API call was successful
231+
##
232+
if [ "$content_check" -gt "0" ] ; then
233+
prompt="Check an option (again to uncheck, ENTER when done): "
234+
while menu && read -rp "$prompt" num && [[ "$num" ]]; do
235+
[[ "$num" != *[![:digit:]]* ]] &&
236+
(( num > 0 && num <= ${#options[@]} )) ||
237+
{ msg="Invalid option: $num"; continue; }
238+
((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
239+
[[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
240+
done
241+
242+
answer_selected=()
243+
echo ""
244+
printf "Selected was: "; msg=" nothing"
245+
for i in ${!options[@]}; do
246+
[[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; answer_selected+=(${options[i]}) ; msg=""; }
247+
done
248+
echo "$msg"
249+
echo "Correct: is: " ${correct_options[@]}
250+
251+
correct_ansers_string=${correct_options[@]}
252+
answers_given_string=${answer_selected[@]}
253+
254+
if [ "${answers_given_string}" == "$correct_ansers_string" ] ; then
255+
echo -ne "$(ColorGreen 'Correct Answer' )" ;
256+
echo ""
257+
else
258+
echo -ne "$(ColorRed 'Wrong Answer' )" ;
259+
echo ""
260+
fi
261+
else
262+
echo "An error occured:"
263+
cat ${temp_quiz}
264+
echo ""
265+
rm -f ${temp_quiz}
266+
exit 1
267+
fi
268+
269+
##
270+
# Clean temp files
271+
##
272+
rm -f ${temp_quiz}

0 commit comments

Comments
 (0)