/[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

Diff of /trunk/webthumb

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Mon Jul 26 13:28:53 2004 UTC revision 64 by dpavlin, Wed Feb 6 23:45:58 2008 UTC
# Line 23  Line 23 
23  # HEAD (part or LWP perl library, it can be commented out)  # HEAD (part or LWP perl library, it can be commented out)
24  # nc (netcat, to simulate web server)  # nc (netcat, to simulate web server)
25  # xwininfo (standard part of X-clients)  # xwininfo (standard part of X-clients)
 # xwit (not so standard command, but very useful)  
26  # import (part of ImageMagick package)  # import (part of ImageMagick package)
27    # xdotool (much more useful than xwit for this purpose)
28  #  #
29  # It will create snapshot of following size  # It will create snapshot of following size
30  W=1024  W=1024
# Line 33  H=768 Line 33  H=768
33  # and then resize it to (comment out to disable)  # and then resize it to (comment out to disable)
34  #RESIZE=200x  #RESIZE=200x
35    
36    # turn locally visible 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  # some configurable paths  # some configurable paths
44  BROWSER=mozilla-firefox  BROWSER=mozilla-firefox
45  XSERVER=vncserver  XSERVER=Xvfb
 XWIT=xwit  
46  URL=$1  URL=$1
47  FILE=${2:-screenshot.jpg}  FILE=${2:-screenshot.jpg}
48    
49  FRAMESET="`mktemp`.html"  FRAMESET="`mktemp`.html"
50    PROFILE_DIR=`mktemp -d`
51  PORT=8888  PORT=8888
52    
53    LOCAL_DISPLAY=$DISPLAY
54    
55  if [ -z "$URL" ] ; then  if [ -z "$URL" ] ; then
56          echo "usage: $0 http://url.to.capture [screenshot.jpg]"          echo "usage: $0 http://url.to.capture [screenshot.jpg]"
57          exit 1          exit 1
58  fi  fi
59    
60  if [ -z "`which $XWIT`" ] ; then  if [ -z "`which xdotool`" ] ; then
61          echo "$0 really need xwit to operate. please install it."          echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
62          exit 1          exit 1
63  fi  fi
64    
65    if [ -z "`which $XSERVER`" ] ; then
66            echo "$0 really need $XSERVER to operate. please install it."
67            exit 1
68    fi
69    
70    while netstat -ln | grep ":$PORT " >/dev/null ; do
71            PORT=`expr $PORT + 1`
72    done
73    
74    echo "### using port $PORT"
75    
76  echo -n "testing URL $URL "  echo -n "testing URL $URL "
77  if HEAD $URL >/dev/null ; then  if HEAD $URL >/dev/null ; then
78          echo "ok"          echo "ok"
# Line 61  else Line 81  else
81          exit 1          exit 1
82  fi  fi
83    
84  echo "starting vnc X server"  DISPLAY_PORT=6042
85    
86    while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do
87            DISPLAY_PORT=`expr $DISPLAY_PORT + 1`
88    done
89    
90    DISPLAY=:`expr $DISPLAY_PORT - 6000`
91    
92    echo "using DISPLAY=$DISPLAY"
93    
94    if [ "$DEBUG" == 1 ] ; then
95            echo "using locally visible debug server on $LOCAL_DISPLAY"
96            DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x24 $DISPLAY 2>/dev/null &
97    else
98            echo "starting $XSERVER"
99            $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
100    fi
101    
102    if [ -z "$!" ] ; then
103            echo "ABORT: can't start X server!"
104            exit
105    fi
106    XSERVER_PID=$!
107    
108  export DISPLAY=`$XSERVER -geometry ${W}x${H} -depth 24 2>&1 \  echo "X server pid $XSERVER_PID"
         | grep : | head -1 | cut -d: -f2 | sed 's/^/:/'`  
109    
110  function kill_vnc() {  function kill_x_server() {
111          vncserver -kill $DISPLAY          echo "Killing server $XSERVER_PID"
112            kill $XSERVER_PID
113          rm -f $FRAMESET          rm -f $FRAMESET
114            rm -fr $PROFILE_DIR
115            trap '' EXIT
116            exit 1
117  }  }
118  trap 'echo "QUIT!" >&2; kill_vnc; exit 1' INT QUIT TERM SEGV  trap 'kill_x_server' INT QUIT TERM SEGV EXIT
119    
120  # create frameset to load site and after site is loaded trigger this script  # create frameset to load site and after site is loaded trigger this script
121  cat > $FRAMESET <<END_OF_FRAMESET  cat > $FRAMESET <<END_OF_FRAMESET
# Line 86  END_OF_FRAMESET Line 131  END_OF_FRAMESET
131    
132  echo "Using frameset html $FRAMESET"  echo "Using frameset html $FRAMESET"
133    
134  echo "preview available with: vncviewer $DISPLAY"  echo "preview available with: xwd -display $DISPLAY -root | xwud"
135    
136  echo "making 'Screenshot' profile"  echo "making 'Screenshot' profile in $PROFILE_DIR"
137  $BROWSER -CreateProfile Screenshot | grep Success  $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
138    
139  echo "launching browser $BROWSER with $URL"  echo "launching browser $BROWSER with $URL"
140  $BROWSER -id Screenshot -P Screenshot -width $W -height $H $FRAMESET 2>&1 >/dev/null &  $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
141    BROWSER_PID=$!
142    
143    function kill_browser() {
144            echo "Killing browser $BROWSER_PID"
145            kill $BROWSER_PID
146            echo "Killing server $XSERVER_PID"
147            kill $XSERVER_PID
148            rm -f $FRAMESET
149            rm -fr $PROFILE_DIR
150            trap '' EXIT
151            exit 1
152    }
153    trap 'kill_browser' INT QUIT TERM SEGV EXIT
154    
155    function ping_browser() {
156            echo -n "ping browser"
157            while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
158                    RID=`xdotool search Restore 2>/dev/null`
159                    if [ ! -z "$RID" ] ; then
160                            echo -n "Esc"
161                            xdotool focus $RID
162                            xdotool key Escape
163                            sleep 1
164                    fi
165                    echo -n "."
166                    sleep 1
167            done
168            echo " OK"
169    }
170    
171    ping_browser
172    
173  echo "staring web server (wait for page to load)"  $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
174    
175    echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
176    
177  # there is hard-coded limit here:  # there is hard-coded limit here:
178  # we will wait 60sec for page to load and render  # we will wait $WAIT sec for page to load and render
179    
180  echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null  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!"
181    
182  # get Mozilla Firefox window id (for resize)  # get Mozilla Firefox window id (for resize)
183    WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
 WINDOW_ID=`xwininfo -display $DISPLAY -name "Mozilla Firefox" | grep "Window id:" | cut -d" " -f4`  
184    
185  if [ -z "$WINDOW_ID" ] ; then  if [ -z "$WINDOW_ID" ] ; then
186          echo "can't find window with name 'Mozilla Firefox'"          echo "can't find window with name 'Mozilla Firefox'"
187          exit 1          exit 1
188    else
189            echo "working on firefox window $WINDOW_ID"
190  fi  fi
191    
192  # move window to foreground  xdotool search firefox
 $XWIT -display $DISPLAY -id $WINDOW_ID -pop  
   
 echo "resizing window $WINDOW_ID to maximum size"  
 $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0  
 $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H  
193    
194    xdotool focus $WINDOW_ID
195    xdotool key F11
196    
197  echo -n "wating for browser ping..."  ping_browser
 while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do  
         echo -n "."  
         sleep 1  
 done  
 echo  
198    
199  # try to deduce inside area of window  # try to deduce inside area of window
200    
# Line 138  echo "saving window $DUMP_ID to $FILE" Line 209  echo "saving window $DUMP_ID to $FILE"
209  if [ ! -z "$RESIZE" ] ; then  if [ ! -z "$RESIZE" ] ; then
210          RESIZE="-geometry $RESIZE"          RESIZE="-geometry $RESIZE"
211  fi  fi
 import -window $DUMP_ID $RESIZE $FILE  
212    
213  kill_vnc  import -window $DUMP_ID $RESIZE $FILE
214    
215    if [ "$DEBUG" == 1 ] ; then
216            echo -n "press enter to exit! "
217            read
218    fi

Legend:
Removed from v.2  
changed lines
  Added in v.64

  ViewVC Help
Powered by ViewVC 1.1.26