/[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 60 by dpavlin, Wed Feb 6 19:48:56 2008 UTC revision 65 by dpavlin, Wed Feb 6 23:59:09 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
31  H=768  H=768
32    D=24
33    
34  # and then resize it to (comment out to disable)  # and then resize it to (comment out to disable)
35  #RESIZE=200x  #RESIZE=200x
36    
37    # turn locally visible server to watch progress
38    DEBUG=0
39    if [ ! -z "$3" ] ; then
40            echo "DEBUG turned on"
41            DEBUG=1
42            D=8
43    fi
44    
45    # wait for $WAIT seconds for page to load
46    WAIT=30
47    
48  # some configurable paths  # some configurable paths
49  BROWSER=mozilla-firefox  BROWSER=mozilla-firefox
50  XSERVER=Xvfb  XSERVER=Xvfb
 XWIT=xwit  
51  URL=$1  URL=$1
52  FILE=${2:-screenshot.jpg}  FILE=${2:-screenshot.jpg}
53    
# Line 44  FRAMESET="`mktemp`.html" Line 55  FRAMESET="`mktemp`.html"
55  PROFILE_DIR=`mktemp -d`  PROFILE_DIR=`mktemp -d`
56  PORT=8888  PORT=8888
57    
58    LOCAL_DISPLAY=$DISPLAY
59    
60  if [ -z "$URL" ] ; then  if [ -z "$URL" ] ; then
61          echo "usage: $0 http://url.to.capture [screenshot.jpg]"          echo "usage: $0 http://url.to.capture [screenshot.jpg]"
62          exit 1          exit 1
63  fi  fi
64    
65  if [ -z "`which $XWIT`" ] ; then  if [ -z "`which xdotool`" ] ; then
66          echo "$0 really need $XWIT to operate. please install it."          echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
67          exit 1          exit 1
68  fi  fi
69    
# Line 59  if [ -z "`which $XSERVER`" ] ; then Line 72  if [ -z "`which $XSERVER`" ] ; then
72          exit 1          exit 1
73  fi  fi
74    
75    while netstat -ln | grep ":$PORT " >/dev/null ; do
76            PORT=`expr $PORT + 1`
77    done
78    
79    echo "### using port $PORT"
80    
81  echo -n "testing URL $URL "  echo -n "testing URL $URL "
82  if HEAD $URL >/dev/null ; then  if HEAD $URL >/dev/null ; then
83          echo "ok"          echo "ok"
# Line 67  else Line 86  else
86          exit 1          exit 1
87  fi  fi
88    
89  echo "starting X server $XSERVER"  DISPLAY_PORT=6042
90    
91    while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do
92            DISPLAY_PORT=`expr $DISPLAY_PORT + 1`
93    done
94    
95    DISPLAY=:`expr $DISPLAY_PORT - 6000`
96    
97  export DISPLAY=:42  echo "using DISPLAY=$DISPLAY"
98    
99  $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY &  if [ "$DEBUG" == 1 ] ; then
100  # debug server          echo "using locally visible debug server on $LOCAL_DISPLAY"
101  #Xnest -display :0 -ac -geometry ${W}x${H} -depth 24 $DISPLAY &          DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x${D} $DISPLAY 2>/dev/null &
102    else
103            echo "starting $XSERVER"
104            $XSERVER -ac -screen 0 ${W}x${H}x${D} $DISPLAY 2>/dev/null &
105    fi
106    
107    if [ -z "$!" ] ; then
108            echo "ABORT: can't start X server!"
109            exit
110    fi
111  XSERVER_PID=$!  XSERVER_PID=$!
112  echo "using pid $xserver_pid for X server"  
113    echo "X server pid $XSERVER_PID"
114    
115  function kill_x_server() {  function kill_x_server() {
116          kill $BROWSER_PID          echo "Killing server $XSERVER_PID"
117          kill $XSERVER_PID          kill $XSERVER_PID
118          rm -f $FRAMESET          rm -f $FRAMESET
119          rm -fr $PROFILE_DIR          rm -fr $PROFILE_DIR
120            trap '' EXIT
121            exit 1
122  }  }
123  trap 'echo "QUIT!" >&2; kill_x_server; exit 1' INT QUIT TERM SEGV  trap 'kill_x_server' INT QUIT TERM SEGV EXIT
124    
125  # 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
126  cat > $FRAMESET <<END_OF_FRAMESET  cat > $FRAMESET <<END_OF_FRAMESET
# Line 103  echo "Using frameset html $FRAMESET" Line 139  echo "Using frameset html $FRAMESET"
139  echo "preview available with: xwd -display $DISPLAY -root | xwud"  echo "preview available with: xwd -display $DISPLAY -root | xwud"
140    
141  echo "making 'Screenshot' profile in $PROFILE_DIR"  echo "making 'Screenshot' profile in $PROFILE_DIR"
142  $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" | grep Success  $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
143    
144  echo "launching browser $BROWSER with $URL"  echo "launching browser $BROWSER with $URL"
145  $BROWSER -P Screenshot -width $W -height $H $FRAMESET &  $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
146  BROWSER_PID=$!  BROWSER_PID=$!
147    
148  echo "staring web server (wait for page to load)"  function kill_browser() {
149            echo "Killing browser $BROWSER_PID"
150            kill $BROWSER_PID
151            echo "Killing server $XSERVER_PID"
152            kill $XSERVER_PID
153            rm -f $FRAMESET
154            rm -fr $PROFILE_DIR
155            trap '' EXIT
156            exit 1
157    }
158    trap 'kill_browser' INT QUIT TERM SEGV EXIT
159    
160  # there is hard-coded limit here:  function ping_browser() {
161  # we will wait 60sec for page to load and render          echo -n "ping browser"
162            while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
163                    RID=`xdotool search Restore 2>/dev/null`
164                    if [ ! -z "$RID" ] ; then
165                            echo -n "Esc"
166                            xdotool focus $RID
167                            xdotool key Escape
168                            sleep 1
169                    fi
170                    echo -n "."
171                    sleep 1
172            done
173            echo " OK"
174    }
175    
176  echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null  ping_browser
177    
178  # get Mozilla Firefox window id (for resize)  # get Mozilla Firefox window id (for resize)
179  WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`  WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
# Line 122  WINDOW_ID=`xwininfo -display $DISPLAY -r Line 181  WINDOW_ID=`xwininfo -display $DISPLAY -r
181  if [ -z "$WINDOW_ID" ] ; then  if [ -z "$WINDOW_ID" ] ; then
182          echo "can't find window with name 'Mozilla Firefox'"          echo "can't find window with name 'Mozilla Firefox'"
183          exit 1          exit 1
184    else
185            echo "working on firefox window $WINDOW_ID"
186  fi  fi
187    
188  # move window to foreground  xdotool search firefox
 $XWIT -display $DISPLAY -id $WINDOW_ID -pop  
189    
190  echo "resizing window $WINDOW_ID to maximum size"  xdotool focus $WINDOW_ID
191  $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0  xdotool key F11
 $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H  
192    
193    ping_browser
194    
195  echo -n "wating for browser ping..."  $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
196  while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do  
197          echo -n "."  echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
198          sleep 1  
199  done  # there is hard-coded limit here:
200  echo  # we will wait $WAIT sec for page to load and render
201    
202    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!"
203    
204  # try to deduce inside area of window  # try to deduce inside area of window
205    
# Line 155  fi Line 217  fi
217    
218  import -window $DUMP_ID $RESIZE $FILE  import -window $DUMP_ID $RESIZE $FILE
219    
220  kill_x_server  if [ "$DEBUG" == 1 ] ; then
221            echo -n "press enter to exit! "
222            read
223    fi

Legend:
Removed from v.60  
changed lines
  Added in v.65

  ViewVC Help
Powered by ViewVC 1.1.26