/[scripts]/trunk/webthumb
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/webthumb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide annotations)
Wed Feb 6 23:34:35 2008 UTC (13 years, 7 months ago) by dpavlin
File size: 4994 byte(s)
rest of changes

1 dpavlin 1 #!/bin/sh
2     #
3     # This script is based on work of mendel from
4     # http://www.lafferty.ca/software/webshots/webshot
5     # webshot: hackish thing to take a picture of a whole webpage.
6     #
7     # idea from sippey (http://www.sippey.com/2004/blog-snaps/)
8     # inspiration from bradfitz
9     #
10     # usage: webshot url filename
11     #
12     # JPEG seems to be the best format.
13     #
14     # Modified by Dobrica Pavlinusic 2004-07-25
15     # - include signal grabbing
16     # - dynamic vncserver display
17     # - correct WindowID parsing
18     # - wait for browser to load full page (useful on slow links)
19     #
20     # It will need follwing external utilities:
21     # vncserver
22     # mozilla-firefox
23     # HEAD (part or LWP perl library, it can be commented out)
24     # nc (netcat, to simulate web server)
25     # xwininfo (standard part of X-clients)
26     # import (part of ImageMagick package)
27 dpavlin 62 # xdotool (much more useful than xwit for this purpose)
28 dpavlin 1 #
29     # It will create snapshot of following size
30 dpavlin 2 W=1024
31     H=768
32 dpavlin 1
33 dpavlin 2 # and then resize it to (comment out to disable)
34     #RESIZE=200x
35 dpavlin 1
36 dpavlin 61 # turn locally visible Xnest server to watch progress
37     DEBUG=0
38     test ! -z "$3" && DEBUG=1
39    
40     # wait for $WAIT seconds for page to load
41     WAIT=30
42    
43 dpavlin 1 # some configurable paths
44     BROWSER=mozilla-firefox
45 dpavlin 60 XSERVER=Xvfb
46 dpavlin 1 URL=$1
47     FILE=${2:-screenshot.jpg}
48    
49     FRAMESET="`mktemp`.html"
50 dpavlin 60 PROFILE_DIR=`mktemp -d`
51 dpavlin 1 PORT=8888
52    
53 dpavlin 61 LOCAL_DISPLAY=$DISPLAY
54    
55 dpavlin 2 if [ -z "$URL" ] ; then
56     echo "usage: $0 http://url.to.capture [screenshot.jpg]"
57     exit 1
58     fi
59    
60 dpavlin 62 if [ -z "`which xdotool`" ] ; then
61     echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
62 dpavlin 2 exit 1
63     fi
64    
65 dpavlin 60 if [ -z "`which $XSERVER`" ] ; then
66     echo "$0 really need $XSERVER to operate. please install it."
67     exit 1
68     fi
69    
70 dpavlin 61 while netstat -ln | grep ":$PORT " >/dev/null ; do
71     PORT=`expr $PORT + 1`
72     done
73    
74     echo "### using port $PORT"
75    
76 dpavlin 1 echo -n "testing URL $URL "
77     if HEAD $URL >/dev/null ; then
78     echo "ok"
79     else
80     echo "FAILED"
81     exit 1
82     fi
83    
84 dpavlin 60 echo "starting X server $XSERVER"
85 dpavlin 1
86 dpavlin 60 export DISPLAY=:42
87 dpavlin 1
88 dpavlin 60
89 dpavlin 61 if [ "$DEBUG" == 1 ] ; then
90 dpavlin 62 echo "using locally visible debug server on $LOCAL_DISPLAY"
91 dpavlin 61 Xnest -display $LOCAL_DISPLAY -ac -geometry ${W}x${H} -depth 24 $DISPLAY 2>/dev/null &
92     else
93 dpavlin 62 echo "starting $XSERVER"
94 dpavlin 61 $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
95     fi
96    
97 dpavlin 60 XSERVER_PID=$!
98 dpavlin 62 echo "X server pid $XSERVER_PID"
99 dpavlin 60
100     function kill_x_server() {
101 dpavlin 61 echo "Killing server $XSERVER_PID"
102 dpavlin 60 kill $XSERVER_PID
103 dpavlin 1 rm -f $FRAMESET
104 dpavlin 60 rm -fr $PROFILE_DIR
105 dpavlin 62 trap '' EXIT
106 dpavlin 61 exit 1
107 dpavlin 1 }
108 dpavlin 61 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
109 dpavlin 1
110     # create frameset to load site and after site is loaded trigger this script
111     cat > $FRAMESET <<END_OF_FRAMESET
112     <html>
113     <head>
114     <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
115     <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
116     <frame src="$URL" name="page_frame" scrolling=no>
117     </frameset>
118     </head>
119     </html>
120     END_OF_FRAMESET
121    
122     echo "Using frameset html $FRAMESET"
123    
124 dpavlin 60 echo "preview available with: xwd -display $DISPLAY -root | xwud"
125 dpavlin 1
126 dpavlin 60 echo "making 'Screenshot' profile in $PROFILE_DIR"
127 dpavlin 61 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
128 dpavlin 1
129     echo "launching browser $BROWSER with $URL"
130 dpavlin 62 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
131 dpavlin 60 BROWSER_PID=$!
132 dpavlin 1
133 dpavlin 61 function kill_browser() {
134     echo "Killing browser $BROWSER_PID"
135     kill $BROWSER_PID
136     echo "Killing server $XSERVER_PID"
137     kill $XSERVER_PID
138     rm -f $FRAMESET
139     rm -fr $PROFILE_DIR
140 dpavlin 62 trap '' EXIT
141 dpavlin 61 exit 1
142     }
143     trap 'kill_browser' INT QUIT TERM SEGV EXIT
144 dpavlin 1
145 dpavlin 62 function ping_browser() {
146     echo -n "ping browser"
147     while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
148     RID=`xdotool search Restore 2>/dev/null`
149     if [ ! -z "$RID" ] ; then
150     echo -n "Esc"
151     xdotool focus $RID
152     xdotool key Escape
153     sleep 1
154     fi
155     echo -n "."
156     sleep 1
157     done
158     echo " OK"
159     }
160    
161     ping_browser
162    
163 dpavlin 63 $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
164 dpavlin 62
165 dpavlin 61 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
166    
167 dpavlin 1 # there is hard-coded limit here:
168 dpavlin 61 # we will wait $WAIT sec for page to load and render
169 dpavlin 1
170 dpavlin 61 echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w $WAIT -p $PORT >/dev/null || echo "Timeout after $WAIT sec!"
171 dpavlin 1
172     # get Mozilla Firefox window id (for resize)
173 dpavlin 60 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
174 dpavlin 1
175     if [ -z "$WINDOW_ID" ] ; then
176     echo "can't find window with name 'Mozilla Firefox'"
177     exit 1
178 dpavlin 62 else
179     echo "working on firefox window $WINDOW_ID"
180 dpavlin 1 fi
181    
182 dpavlin 62 xdotool search firefox
183 dpavlin 1
184 dpavlin 62 xdotool focus $WINDOW_ID
185     xdotool key F11
186 dpavlin 1
187 dpavlin 62 ping_browser
188 dpavlin 1
189     # try to deduce inside area of window
190    
191     DUMP_ID=`xwininfo -display $DISPLAY -id $WINDOW_ID -tree | grep 0x | grep -v ${W}x${H} | grep ${W}x | head -1 | sed 's/^ *//' | cut -d' ' -f1`
192    
193     if [ -z "$DUMP_ID" ] ; then
194     echo "can't find inside area of window. Using whole browser!"
195     DUMP_ID=$WINDOW_ID
196     fi
197    
198     echo "saving window $DUMP_ID to $FILE"
199 dpavlin 2 if [ ! -z "$RESIZE" ] ; then
200     RESIZE="-geometry $RESIZE"
201     fi
202 dpavlin 60
203 dpavlin 2 import -window $DUMP_ID $RESIZE $FILE
204 dpavlin 1
205 dpavlin 62 if [ "$DEBUG" == 1 ] ; then
206     echo -n "press enter to exit! "
207     read
208     fi

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26