Fully automated fill_exercice.sh
This commit is contained in:
parent
f8641366dc
commit
499346e611
1 changed files with 38 additions and 71 deletions
|
@ -36,69 +36,43 @@ new_key() {
|
|||
THEME="$1"
|
||||
EXERCICE="$2"
|
||||
NAME="$3"
|
||||
KEY=`echo $4 | sed 's/"/\\\\"/g'`
|
||||
KEY=`echo $4 | sed 's/"/\\\\"/g' | sed 's#\\\\#\\\\\\\\#g'`
|
||||
|
||||
curl -f -s -d "{\"name\": \"$NAME\", \"key\": \"$KEY\"}" "${BASEURL}/api/themes/$THEME/$EXERCICE/keys" |
|
||||
grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
|
||||
}
|
||||
|
||||
# Theme
|
||||
while read -p "Theme name: " THEME_NAME
|
||||
curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | while read f; do basename "$f"; done | while read THEME_URI
|
||||
do
|
||||
if [ -z "$THEME_NAME" ]; then
|
||||
break
|
||||
fi
|
||||
|
||||
THEME_NAME=$(echo "${THEME_URI}" | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g')
|
||||
THEME_ID=`new_theme "$THEME_NAME"`
|
||||
if [ -z "$THEME_ID" ]; then
|
||||
echo -e "\e[31;01m!!! An error occured during theme add\e[00m"
|
||||
continue
|
||||
else
|
||||
echo -e "\e[32m>>> New theme created:\e[00m $THEME_ID - $THEME_NAME"
|
||||
echo -e "\e[33m>>> New theme created:\e[00m $THEME_ID - $THEME_NAME"
|
||||
fi
|
||||
|
||||
LAST=null
|
||||
EXO_NUM=1
|
||||
THM_BASEURI=
|
||||
echo
|
||||
echo -e "\e[36m--- Filling exercice ${EXO_NUM} in theme ${THEME_NAME}\e[00m"
|
||||
while read -p "Exercice title (^D to pass to the next theme) [Exercice ${EXO_NUM}]: " EXO_NAME
|
||||
EXO_NUM=0
|
||||
THM_BASEURI="/${THEME_URI}/"
|
||||
curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g' | sed 1d | while read f; do basename "$f"; done | while read EXO_NAME
|
||||
do
|
||||
if [ -z "${EXO_NAME}" ]; then
|
||||
EXO_NAME="Exercice ${EXO_NUM}"
|
||||
fi
|
||||
|
||||
|
||||
if [ -z "${THM_BASEURI}" ]
|
||||
if ! echo $EXO_NAME | grep Exercice > /dev/null
|
||||
then
|
||||
read -p "Base URI for the theme [/${THEME_NAME}/]: ${BASEURI}" THM_BASEURI
|
||||
if [ -z "${THM_BASEURI}" ]; then
|
||||
THM_BASEURI="/${THEME_NAME}/"
|
||||
fi
|
||||
fi
|
||||
read -p "Base URI for the exercice [${EXO_NAME}/]: ${BASEURI}${THM_BASEURI}" EXO_BASEURI
|
||||
if [ -z "${EXO_BASEURI}" ]; then
|
||||
EXO_BASEURI="${EXO_NAME}/"
|
||||
continue
|
||||
fi
|
||||
|
||||
EXO_NUM=$((EXO_NUM + 1))
|
||||
echo
|
||||
echo -e "\e[36m--- Filling exercice ${EXO_NUM} in theme ${THEME_NAME}\e[00m"
|
||||
|
||||
EXO_BASEURI="${EXO_NAME}/"
|
||||
|
||||
FILES=$(curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | while read f; do basename $f; done)
|
||||
|
||||
DEF_VIDEO=$(echo "$FILES" | grep -E "\.(mov|mkv|mp4|avi)$" | head -1)
|
||||
|
||||
read -p "Video URI [${DEF_VIDEO}]: ${BASEURI}${THM_BASEURI}${EXO_BASEURI}" EXO_VIDEO
|
||||
if [ -z "${EXO_VIDEO}" ]; then
|
||||
EXO_VIDEO="$DEF_VIDEO"
|
||||
fi
|
||||
|
||||
README=$(echo "$FILES" | grep -iE "readme" | head -1)
|
||||
if ! [ -z "$README" ]; then
|
||||
echo
|
||||
echo "################################# $README #################################"
|
||||
curl -f -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/${README}"
|
||||
echo
|
||||
echo "###########################################################################"
|
||||
echo
|
||||
fi
|
||||
EXO_VIDEO=$(echo "$FILES" | grep -E "\.(mov|mkv|mp4|avi|flv|ogv|webm)$" | tail -1)
|
||||
|
||||
if [ "${LAST}" = "null" ]; then
|
||||
echo ">>> Assuming this exercice has no dependency"
|
||||
|
@ -106,62 +80,55 @@ do
|
|||
echo ">>> Assuming this exercice depends on the last entry (id=${LAST})"
|
||||
fi
|
||||
|
||||
EXO_GAIN=$((5 * (2 ** $EXO_NUM)))
|
||||
EXO_GAIN=$((3 * (2 ** $EXO_NUM) - 1))
|
||||
echo ">>> Using default gain: ${EXO_GAIN} points"
|
||||
|
||||
echo "--- Enter the description for ${EXO_NUM} in theme ${THEME_NAME}"
|
||||
EXO_DESC=`cat`
|
||||
|
||||
echo "--- Enter the optional hint for ${EXO_NUM} in theme ${THEME_NAME}"
|
||||
EXO_HINT=`cat`
|
||||
EXO_DESC=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/description.txt")
|
||||
EXO_HINT=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/hint.txt")
|
||||
|
||||
EXO_ID=`new_exercice "${THEME_ID}" "${EXO_NAME}" "${EXO_DESC}" "${EXO_HINT}" "${LAST}" "${EXO_GAIN}" "${THM_BASEURI}${EXO_BASEURI}${EXO_VIDEO}"`
|
||||
if [ -z "$EXO_ID" ]; then
|
||||
echo -e "\e[31;01m!!! An error occured during exercice add.\e[00m"
|
||||
continue
|
||||
else
|
||||
echo -e "\e[32m>>> New exercice created: $EXO_ID - $EXO_NAME\e[00m"
|
||||
echo -e "\e[32m>>> New exercice created:\e[00m $EXO_ID - $EXO_NAME"
|
||||
fi
|
||||
|
||||
|
||||
# Keys
|
||||
while read -p "New key: " KEY_RAW
|
||||
curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/keys.txt" | while read KEYLINE
|
||||
do
|
||||
if [ -z "${KEY_RAW}" ]
|
||||
then
|
||||
break
|
||||
KEY_NAME=$(echo "$KEYLINE" | cut -d : -f 1)
|
||||
KEY_RAW=$(echo "$KEYLINE" | cut -d : -f 2-)
|
||||
|
||||
if [ -z "${KEY_NAME}" ]; then
|
||||
KEY_NAME="Flag"
|
||||
fi
|
||||
|
||||
read -p "Key type/kind/hint: " KEY_TYPE
|
||||
|
||||
KEY_ID=`new_key "${THEME_ID}" "${EXO_ID}" "${KEY_TYPE}" "${KEY_RAW}"`
|
||||
KEY_ID=`new_key "${THEME_ID}" "${EXO_ID}" "${KEY_NAME}" "${KEY_RAW}"`
|
||||
if [ -z "$KEY_ID" ]; then
|
||||
echo -e "\e[31;01m!!! An error occured during key add.\e[00m"
|
||||
echo -e "\e[31;01m!!! An error occured during key import!\e[00m (name=${KEYNAME};raw=${KEY_RAW})"
|
||||
else
|
||||
echo -e "\e[32m>>> New key created: $KEY_ID - $KEY_TYPE\e[00m"
|
||||
fi
|
||||
echo -e "\e[32m>>> New key added:\e[00m $KEY_ID - $KEY_NAME"
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# Files
|
||||
for FBASE in $(for f in $FILES; do echo $f; done | grep -vEi "(readme|${EXO_VIDEO})")
|
||||
for f in $FILES; do echo $f; done | grep -vEi "(ressources|readme|description.txt|hint.txt|keys.txt|${EXO_VIDEO})" |
|
||||
while read FBASE
|
||||
do
|
||||
if read -p "Import file ${BASEURI}${THM_BASEURI}${EXO_BASEURI}${FBASE}? (Enter: yes, ^D: no)"
|
||||
then
|
||||
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}${FBASE}"`
|
||||
if [ -z "$FILE_ID" ]; then
|
||||
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
||||
else
|
||||
echo -e "\e[32m>>> New file added: $FILE_ID - $FBASE\e[00m"
|
||||
fi
|
||||
echo "Import file ${BASEURI}${THM_BASEURI}${EXO_BASEURI}${FBASE}"
|
||||
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}${FBASE}"`
|
||||
if [ -z "$FILE_ID" ]; then
|
||||
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
||||
else
|
||||
echo -e "\e[32m>>> New file added:\e[00m $FILE_ID - $FBASE"
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
LAST=$EXO_ID
|
||||
EXO_NUM=$((EXO_NUM + 1))
|
||||
echo
|
||||
echo -e "\e[36m--- Filling exercice ${EXO_NUM} in theme ${THEME_NAME}\e[00m"
|
||||
done
|
||||
echo
|
||||
done
|
||||
|
|
Reference in a new issue