Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created October 19, 2022 14:07
Show Gist options
  • Save ma2shita/83c3df9af825be2b86b4fa5c7e147578 to your computer and use it in GitHub Desktop.
Save ma2shita/83c3df9af825be2b86b4fa5c7e147578 to your computer and use it in GitHub Desktop.
# Using:
# bash export_images.bash 7CxxxxxxxxCF "2022/10/18 7:00:00" [LOOP_COUNT:19]
# #=> 001.jpg(+2:00) ~ 019.jpg(+38:00)
# Copyright (c) 2022 Kohei "Max" MATSUSHITA ([email protected])
# Released under the MIT license
# https://opensource.org/licenses/mit-license.php
DEVICE_ID=$1
START_DATETIME=$2
LOOP_COUNT=${3:-19}
date -d 0hour > /dev/null 2>&1 # inspectbash
readonly _IS_DATECMD=$([[ "$?" -eq 0 ]] && echo "GNU" || echo "BSD") # judge (likes ternary operator)
echo "$DEVICE_ID" >&2
for N in $(seq 1 $LOOP_COUNT)
do
INC_HOUR=$(($N * 2))
if [ "$_IS_DATECMD" == "BSD" ]; then
TS=$(date -v+${INC_HOUR}H -j -f "%Y/%m/%d %H:%M:%S" "${START_DATETIME}" +%s000) # for macOS
else
TS=$(date --date "${START_DATETIME} ${INC_HOUR} hours" +%s000)
fi
echo "$TS" >&2
EXPORT_exportId=$(soracom sora-cam devices images export --device-id $DEVICE_ID --time $TS | jq -r .exportId)
GET_EXPORTED_status="" # do{}untile() の代替 / 他の実装と合わせるための記述
while [ "$GET_EXPORTED_status" != "completed" ]
do
sleep $(($RANDOM % 4 + 3)) # waiting 3 ~ 6 seconds
GET_EXPORTED=$(soracom sora-cam devices images get-exported --device-id $DEVICE_ID --export-id $EXPORT_exportId)
GET_EXPORTED_status=$(echo $GET_EXPORTED | jq -r .status)
GET_EXPORTED_url=$(echo $GET_EXPORTED | jq -r .url)
done
curl -s --output $(printf "%03d.jpg" $N) "$GET_EXPORTED_url"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment