mirror of
https://github.com/getbible/getverse.git
synced 2024-11-21 20:05:10 +00:00
Added option to get scripture by book number.
This commit is contained in:
parent
4ffde4983a
commit
a2f43aefc2
@ -2,46 +2,59 @@
|
|||||||
|
|
||||||
# Do some prep work
|
# Do some prep work
|
||||||
command -v jq >/dev/null 2>&1 || {
|
command -v jq >/dev/null 2>&1 || {
|
||||||
echo >&2 "We require jq for this script to run, but it's not installed. Aborting."
|
echo >&2 "We require jq for this script to run, but it's not installed. Aborting."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
command -v curl >/dev/null 2>&1 || {
|
command -v curl >/dev/null 2>&1 || {
|
||||||
echo >&2 "We require curl for this script to run, but it's not installed. Aborting."
|
echo >&2 "We require curl for this script to run, but it's not installed. Aborting."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
# set the query
|
# set the query (default: 1 John 3:16-18)
|
||||||
QUERY="${1:-1 John 3:16-18}"
|
QUERY="${1:-62 3:16-18}"
|
||||||
# set the translation
|
# set the translation (default: kjv)
|
||||||
VERSION="${2:-kjv}"
|
VERSION="${2:-kjv}"
|
||||||
# get the name from the query
|
# get the name from the query TODO: find better filter
|
||||||
BOOKNAME=$(echo ${QUERY%%[0-9]?:*} | xargs echo -n)
|
BOOKNAME=$(echo ${QUERY%%[0-9]?:*} | xargs echo -n)
|
||||||
BOOKNAME=$(echo ${BOOKNAME%%[0-9]:*} | xargs echo -n)
|
BOOKNAME=$(echo ${BOOKNAME%%[0-9]:*} | xargs echo -n)
|
||||||
BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json")
|
# check if the name was given by number
|
||||||
NUMBERS=$(echo "${QUERY//$BOOKNAME/}" | xargs echo -n)
|
re='^[0-9]+$'
|
||||||
|
if [[ "$BOOKNAME" =~ $re ]]; then
|
||||||
|
BOOK_NR="${BOOKNAME}"
|
||||||
|
else
|
||||||
|
# get the list of books from the API to get the book number
|
||||||
|
BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json")
|
||||||
|
BOOK_NR=$(echo "$BOOKS" | jq -r ".[] | select(.name == \"${BOOKNAME}\") | .nr")
|
||||||
|
fi
|
||||||
|
# get chapter and verses numbers
|
||||||
|
NUMBERS=$(echo "${QUERY/$BOOKNAME/}" | xargs echo -n)
|
||||||
|
# get chapter number
|
||||||
CHAPTER_NR=$(echo "${NUMBERS%:*}" | xargs echo -n)
|
CHAPTER_NR=$(echo "${NUMBERS%:*}" | xargs echo -n)
|
||||||
|
# get verses numbers
|
||||||
VERSES_NR=$(echo "${NUMBERS#*:}" | xargs echo -n)
|
VERSES_NR=$(echo "${NUMBERS#*:}" | xargs echo -n)
|
||||||
BOOK_NR=$(echo "$BOOKS" | jq -r ".[] | select(.name == \"${BOOKNAME}\") | .nr")
|
# get chapter text from getBible API
|
||||||
CHAPTER=$(curl -s "https://getbible.net/v2/${VERSION}/${BOOK_NR}/${CHAPTER_NR}.json")
|
CHAPTER=$(curl -s "https://getbible.net/v2/${VERSION}/${BOOK_NR}/${CHAPTER_NR}.json")
|
||||||
IFS=',' read -ra VERSES_NR_ARRAY <<< "${VERSES_NR}"
|
# read verses into array by comma separator
|
||||||
|
IFS=',' read -ra VERSES_NR_ARRAY <<<"${VERSES_NR}"
|
||||||
|
# start the scripture array
|
||||||
SCRIPTURE=()
|
SCRIPTURE=()
|
||||||
for VER_NR in "${VERSES_NR_ARRAY[@]}"
|
# load the verses in this loop over the verse numbers sets
|
||||||
do
|
for VER_NR in "${VERSES_NR_ARRAY[@]}"; do
|
||||||
if [[ "${VER_NR}" == *"-"* ]]; then
|
# check if this is a range of verses
|
||||||
IFS='-' read -ra VER_NR_ARRAY <<< "${VER_NR}"
|
if [[ "${VER_NR}" == *"-"* ]]; then
|
||||||
START=${VER_NR_ARRAY[0]}
|
IFS='-' read -ra VER_NR_ARRAY <<<"${VER_NR}"
|
||||||
END=${VER_NR_ARRAY[-1]}
|
START=${VER_NR_ARRAY[0]}
|
||||||
while [ "$START" -le "$END" ]; do
|
END=${VER_NR_ARRAY[-1]}
|
||||||
|
while [ "$START" -le "$END" ]; do
|
||||||
VERSE=$(echo "$CHAPTER" | jq -r ".verses[] | select(.verse == ${START})")
|
VERSE=$(echo "$CHAPTER" | jq -r ".verses[] | select(.verse == ${START})")
|
||||||
TEXT=$(echo "$VERSE" | jq -r '.text' )
|
TEXT=$(echo "$VERSE" | jq -r '.text')
|
||||||
SCRIPTURE+=("${START} ${TEXT}")
|
SCRIPTURE+=("${START} ${TEXT}")
|
||||||
START=$(($START + 1))
|
START=$(($START + 1))
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
VERSE=$(echo "$CHAPTER" | jq ".verses[] | select(.verse == ${VER_NR})")
|
VERSE=$(echo "$CHAPTER" | jq ".verses[] | select(.verse == ${VER_NR})")
|
||||||
TEXT=$(echo "$VERSE" | jq -r '.text' )
|
TEXT=$(echo "$VERSE" | jq -r '.text')
|
||||||
SCRIPTURE+=("${VER_NR} ${TEXT}")
|
SCRIPTURE+=("${VER_NR} ${TEXT}")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# we return the scripture one verse per/line
|
# we return the scripture one verse per/line
|
||||||
IFS=$'\n'; echo "${SCRIPTURE[*]}"
|
IFS=$'\n'; echo "${SCRIPTURE[*]}"
|
||||||
|
Loading…
Reference in New Issue
Block a user