Skip to content

Instantly share code, notes, and snippets.

@nna774
Last active May 11, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nna774/4e604e8ae658245738b1cdce41c80a80 to your computer and use it in GitHub Desktop.
Save nna774/4e604e8ae658245738b1cdce41c80a80 to your computer and use it in GitHub Desktop.
#! /bin/bash -xeu
BUCKET='nana-camera'
: ${SLACK_WH:?'define SLACK_WH'}
report() {
local MSG=${1:-'コケた'}
curl -X POST --data-urlencode "payload={\"text\": \"${MSG}\"}" ${SLACK_WH}
}
take() {
local outdir=$1
local to=`date +"${outdir}/%H-%M-%S.jpg"`
ffmpeg -s 1920x1080 -i /dev/video0 -ss 0 -vframes 1 -f image2 -y ${to}
}
sync () {
local dir=$1
local date=$2
local keyprefix="images/${date}"
aws s3 sync ${dir} "s3://${BUCKET}/${keyprefix}"
}
main() {
local date=`date +'%Y/%m/%d'`
local preminute=''
local prehour=''
local outdir=`mktemp -d`
local takefail=0
local syncfail=0
local lasttake=0
while true; do
local minute=`date +'%M'`
local hour=`date +'%H'`
local now=`date +'%s'`
local diff=$((now-lasttake))
if [ ${diff} -lt 10 ]; then
sleep $((9-diff))
fi
if ! take ${outdir}; then
report "take fail ${takefail} times"
$((++takefail))
sleep 10
fi
sleep 1
lasttake=`date +'%s'`
if [ "${minute}" != "${preminute}" ]; then
if ! sync ${outdir} ${date}; then
report "sync fail ${syncfail} times"
$((++syncfail))
sleep 120
fi
rm -rf ${outdir}
outdir=`mktemp -d`
preminute=${minute}
date=`date +'%Y/%m/%d'`
fi
if [ ${takefail} -gt 10 ]; then
echo "take fail"
echo "exit"
report "take fail; exit"
exit
fi
if [ ${syncfail} -gt 5 ]; then
echo "sync fail"
echo "exit"
report "sync fail; exit"
exit
fi
if [ "${hour}" != "${prehour}" ]; then
echo "reset takefail and syncfail"
takefail=0
syncfail=0
prehour=${hour}
fi
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment