Soshot/bin/thumb_server.sh

123 lines
4.3 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-04-02 15:42:06 +02:00
hashUrl=$2 # hash(url) is the name of final image
2013-03-19 17:07:13 +01:00
thumbSize=$3 # size of thumb widthxheight ex : 190x90
sizeNameDir=$4 # name of size dir
onlyThumb=$5 # make only thumbshot no full size image
waitForResult=$6 # if true we try to make soon as possible or add to queue
##########################################
##### DON'T EDIT THIS PARAM USE #####
##### cache/config/serverOptions.php #####
##########################################
2013-04-09 16:47:04 +02:00
maxThread=1 # 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=false # 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)
2013-04-02 15:42:06 +02:00
firstLevel=${hashUrl:0:2}
secondLevel=${hashUrl:2:2}
startPath=$sizeNameDir/$firstLevel/$secondLevel/
if [ $site == "manual" ]
then
site=$(cat "cache/tmp/manual.txt")
echo '' > "cache/tmp/manual.txt"
fi
2013-04-02 14:35:14 +02:00
mkdir -p "cache/img/$startPath"
2013-04-09 16:59:11 +02:00
if [ -f "cache/config/serverOptions.php" ]
then
2013-04-09 16:59:11 +02:00
source "cache/config/serverOptions.php"
fi
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`
2013-04-02 15:42:06 +02:00
timeout $timeOut ./bin/phantomjs --disk-cache=false --local-storage-path=/bin/ --ignore-ssl-errors=true /bin/rasterize.js "$site" "cache/tmp/$hashUrl.png"
2013-04-02 15:42:06 +02:00
if [ ! -f "cache/tmp/$hashUrl.png" ]
2013-03-19 17:07:13 +01:00
then
echo 0
errorCode=`echo $(curl -k --write-out %{http_code} --silent -S --connect-timeout $timeOut \--no-keepalive --output /dev/null $site)`
if [ $site == 'manual' ]
then
errorCode='manual'
fi
2013-03-19 17:07:13 +01:00
case $errorCode in
2013-04-02 15:42:06 +02:00
000) cp "bin/000.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/000.png" "cache/img/$startPath""$hashUrl.png" ;;
200) cp "bin/error.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/error.png" "cache/img/$startPath""$hashUrl.png" ;;
2013-04-02 15:42:06 +02:00
404) cp "bin/404.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/404.png" "cache/img/$startPath""$hashUrl.png" ;;
*) cp "bin/error.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/error.png" "cache/img/$startPath""$hashUrl.png" ;;
2013-03-19 17:07:13 +01:00
esac
if $log
then
end_time=`date +%s`
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
if [ $errorCode == 200 ]
then
echo "none --- "$site" --- "$hashUrl" --- "$thumbSize" --- "$sizeNameDir" --- "$onlyThumb" --- true --- "$errorCode > "cache/logs/retry/"$hashUrl".log"
else
echo "none --- "$site" --- "$hashUrl" --- "$thumbSize" --- "$sizeNameDir" --- "$onlyThumb" --- true --- "$errorCode > "cache/logs/other/"$hashUrl".log"
fi
2013-03-19 17:07:13 +01:00
fi
exit
fi
if [ $onlyThumb == 1 ]
then
2013-04-02 15:42:06 +02:00
convert "cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
2013-04-02 15:42:06 +02:00
eval $optimizeTool "cache/img/$startPath""$hashUrl""_thumb.png"
fi
else
2013-04-02 15:42:06 +02:00
convert "cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "cache/img/$startPath""$hashUrl.png"
convert "cache/img/$startPath""$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
2013-04-02 15:42:06 +02:00
eval $optimizeTool "cache/img/$startPath""$hashUrl""_thumb.png" "cache/img/$startPath""$hashUrl.png"
fi
fi
2013-04-02 15:42:06 +02:00
rm "cache/tmp/$hashUrl.png"
2013-03-14 11:56:21 +01:00
if [ $onlyThumb ]
2013-03-14 11:56:21 +01:00
then
2013-04-02 15:42:06 +02:00
if [[ -f "cache/img/$startPath""$hashUrl""_thumb.png" ]]
then
echo 1
else
echo 0
fi
2013-03-14 11:56:21 +01:00
else
2013-04-02 15:42:06 +02:00
if [[ -f "cache/img/$startPath""$hashUrl""_thumb.png" && -f "cache/img/$startPath""$hashUrl.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 >> 'cache/logs/success.txt'
2013-03-19 17:07:13 +01:00
fi