-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsnaper.sh
120 lines (103 loc) · 3.17 KB
/
websnaper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/sh
# check PID
PID_FILE='/tmp/websnap.pid'
# check since when the pid is created
if [ -e $PID_FILE ]
then
NOW_IN_MILLIS=`date --utc +%s`
FILE_IN_MILLIS=`date --utc --reference=$PID_FILE +%s`
ELAPSED_IN_MILLIS=$(($NOW_IN_MILLIS - $FILE_IN_MILLIS))
if [ $ELAPSED_IN_MILLIS -gt 120 ]
then
#echo 'this pid is too old, I remove it'
killall chrome
rm -rf $PID_FILE
fi
fi
if [ -e $PID_FILE ]
then
echo "Content-type: text/html"
echo ""
echo "busy..."
else
touch $PID_FILE
URL=`echo "$QUERY_STRING" | grep -oE "(^|[?&])url=[^&]+" | cut -f 2 -d "="`
SIZE=`echo "$QUERY_STRING" | grep -oE "(^|[?&])size=[^&]+" | cut -f 2 -d "="`
if [ $SIZE != 'XXL' -a $SIZE != 'XL' -a $SIZE != 'L' -a $SIZE != 'M' -a $SIZE != 'S' ]
then
echo "Status: 400 Bad Request"
echo "Content-type: text/html"
echo ""
echo "Missing or wrong parameter 'size'. Values are : (L)arge,(M)edium,(S)mall"
else
# check HTTP HEADER response
HTTP_CODE=`curl --user-agent "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.0 Safari/532.0" --connect-timeout 10 -s -w "%{http_code}\n" -o /dev/null $URL`
HTTP_CODE=`expr substr $HTTP_CODE 1 1`
if [ $HTTP_CODE != 0 -a $HTTP_CODE != 4 -a $HTTP_CODE != 5 ]
then
CROPPED_SIZE='1265x1024+0+0'
case $SIZE in
'XXL')
FINISHED_SIZE='1280x1024'
;;
'XL')
FINISHED_SIZE='1024x768'
;;
'L')
FINISHED_SIZE='640x480'
;;
'M')
FINISHED_SIZE='320x240'
;;
'S')
FINISHED_SIZE='80x60'
;;
*)
;;
esac
# start Xvfb if necessary
XVFB_PID=`pidof Xvfb`
if [ -z $XVFB_PID ]
then
Xvfb :2 -screen 0 1280x1024x24 -ac -fbdir /tmp/ &
fi
# default settings
export DISPLAY=:2.0
xsetroot -solid White
OUPUT_FILE=`echo $URL | tr ':/\.' '-'`
OUPUT_FILE=`echo '/tmp/'$OUPUT_FILE'.png'`
google-chrome --start-maximized --kiosk --disable-java --disable-logging --disable-metrics-reporting --disable-dev-tools $URL &
sleep 7
import -window root -silent -quiet $OUPUT_FILE
convert $OUPUT_FILE -crop $CROPPED_SIZE -resize $FINISHED_SIZE\! PNG8:$OUPUT_FILE
# compression with optipng
#optipng -quiet -o5 $OUPUT_FILE -out $OUPUT_FILE
# compression with pngquant
pngquant -ext .png -force 256 $OUPUT_FILE
sleep 2
killall -q -u www-data chrome
COLOR_COUNT=`identify -format "%k" $OUPUT_FILE`
if [ -s $OUPUT_FILE -a $COLOR_COUNT -gt 20 ]
then
# as websnaper for wikio
echo "x-image-status: 1"
echo "Content-type: image/png"
echo ""
cat $OUPUT_FILE
else
# as websnaper for wikio
echo "x-image-status: 2"
echo "Content-type: text/html"
echo ""
echo "not content"
fi
rm -rf $OUPUT_FILE
else
echo "Content-type: text/html"
echo ""
echo "Content not available. Missing or wrong parameter 'url'."
echo $URL" returns an HTTP error: "$HTTP_CODE
fi
fi
rm -rf $PID_FILE
fi