Soshot/bin/thumb_server.sh

91 lines
3.1 KiB
Bash
Raw Normal View History

2013-03-14 11:56:21 +01:00
#!/bin/bash
# inspirated source http://www.cambus.net/creating-thumbnails-using-phantomjs-and-imagemagick/ for global idea
2013-03-19 17:07:13 +01:00
# https://gist.github.com/rsvp/1171304 for httpstatus code
# All info at http://forge.leslibres.org/projects/soshot
2013-03-14 11:56:21 +01:00
site=$1 # url for thumbshot
2013-03-19 17:07:13 +01:00
md5Site=$2 # md5(url) is the name of final image
thumbSize=$3 # size of thumb widthxheight ex : 190x90
onlyThumb=$4 # make only thumbshot no full size image
waitForResult=$5 # if true we try to make soon as possible or add to queue
maxThread=3 # max parralle work for me the best is nb core - 1
timeOut=60 # default time out after this time the site are declared in error
log=true # log all generation success and error
2013-03-19 17:07:13 +01:00
randomSleep=`echo $((RANDOM%20))`
optimizeTool='' # external tool form optimize png
maxQueue=50 # max process in queue
currentProcess=$(ps -e | grep -v grep | grep thumb_ | wc -l)
if [ $currentProcess -gt $maxQueue ]
then
exit
fi
2013-03-14 11:56:21 +01:00
2013-03-19 17:07:13 +01:00
if [ ! $waitForResult ]
then
sleep `echo $((RANDOM%20))`
while [[ `ps -e | grep -v grep | grep phantomjs | wc -l` -ge $maxThread ]]
do
sleep `echo $((RANDOM%20))`
done
fi
2013-03-19 17:07:13 +01:00
start_time=`date +%s`
timeout $timeOut ./bin/phantomjs --disk-cache=false --local-storage-path=/bin/ --ignore-ssl-errors=true /bin/rasterize.js "$site" "cache/tmp/$md5Site.png"
if [ ! -f "cache/tmp/$md5Site.png" ]
then
echo 0
errorCode=`echo $(curl -k --write-out %{http_code} --silent -S --connect-timeout $timeOut \--no-keepalive --output /dev/null $site)`
case $errorCode in
000) cp "bin/000.png" "cache/img/$md5Site""_thumb.png" && cp "bin/000.png" "cache/img/$md5Site.png" ;;
404) cp "bin/404.png" "cache/img/$md5Site""_thumb.png" && cp "bin/404.png" "cache/img/$md5Site.png" ;;
*) cp "bin/error.png" "cache/img/$md5Site""_thumb.png" && cp "bin/error.png" "cache/img/$md5Site.png" ;;
esac
if $log
then
end_time=`date +%s`
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
echo $logDate `expr $end_time - $start_time`s --- ERROR --- httpstatus $errorCode --- $site >> 'cache/logs/logs.txt'
fi
exit
fi
if [ $onlyThumb ]
then
2013-03-19 17:07:13 +01:00
convert "cache/tmp/$md5Site.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "cache/img/$md5Site""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "cache/img/$md5Site""_thumb.png"
fi
else
convert "cache/tmp/$md5Site.png" -crop 1280x1024+0+0 "cache/img/$md5Site.png" &&
2013-03-19 17:07:13 +01:00
convert "cache/img/$md5Site.png" -filter Lanczos -thumbnail "$thumbSize" "cache/img/$md5Site""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "cache/img/$md5Site""_thumb.png" "cache/img/$md5Site.png"
fi
fi
2013-03-14 11:56:21 +01:00
rm "cache/tmp/$md5Site.png"
if [ $onlyThumb ]
2013-03-14 11:56:21 +01:00
then
if [[ -f "cache/img/$md5Site""_thumb.png" ]]
then
echo 1
else
echo 0
fi
2013-03-14 11:56:21 +01:00
else
if [[ -f "cache/img/$md5Site""_thumb.png" && -f "cache/img/$md5Site.png" ]]
then
echo 1
else
echo 0
fi
2013-03-14 11:56:21 +01:00
fi
2013-03-19 17:07:13 +01:00
if $log
then
end_time=`date +%s`
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
echo $logDate `expr $end_time - $start_time`s --- $site >> 'cache/logs/logs.txt'
fi