/[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 62 by dpavlin, Wed Feb 6 23:34:04 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 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  # some configurable paths  # some configurable paths
44  BROWSER=mozilla-firefox  BROWSER=mozilla-firefox
45  XSERVER=Xvfb  XSERVER=Xvfb
 XWIT=xwit  
46  URL=$1  URL=$1
47  FILE=${2:-screenshot.jpg}  FILE=${2:-screenshot.jpg}
48    
# Line 44  FRAMESET="`mktemp`.html" Line 50  FRAMESET="`mktemp`.html"
50  PROFILE_DIR=`mktemp -d`  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    
# Line 59  if [ -z "`which $XSERVER`" ] ; then Line 67  if [ -z "`which $XSERVER`" ] ; then
67          exit 1          exit 1
68  fi  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 71  echo "starting X server $XSERVER" Line 85  echo "starting X server $XSERVER"
85    
86  export DISPLAY=:42  export DISPLAY=:42
87    
88  $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY &  
89  # debug server  if [ "$DEBUG" == 1 ] ; then
90  #Xnest -display :0 -ac -geometry ${W}x${H} -depth 24 $DISPLAY &          echo "using locally visible debug server on $LOCAL_DISPLAY"
91            Xnest -display $LOCAL_DISPLAY -ac -geometry ${W}x${H} -depth 24 $DISPLAY 2>/dev/null &
92    else
93            echo "starting $XSERVER"
94            $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
95    fi
96    
97  XSERVER_PID=$!  XSERVER_PID=$!
98  echo "using pid $xserver_pid for X server"  echo "X server pid $XSERVER_PID"
99    
100  function kill_x_server() {  function kill_x_server() {
101          kill $BROWSER_PID          echo "Killing server $XSERVER_PID"
102          kill $XSERVER_PID          kill $XSERVER_PID
103          rm -f $FRAMESET          rm -f $FRAMESET
104          rm -fr $PROFILE_DIR          rm -fr $PROFILE_DIR
105            trap '' EXIT
106            exit 1
107  }  }
108  trap 'echo "QUIT!" >&2; kill_x_server; exit 1' INT QUIT TERM SEGV  trap 'kill_x_server' INT QUIT TERM SEGV EXIT
109    
110  # 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
111  cat > $FRAMESET <<END_OF_FRAMESET  cat > $FRAMESET <<END_OF_FRAMESET
# Line 103  echo "Using frameset html $FRAMESET" Line 124  echo "Using frameset html $FRAMESET"
124  echo "preview available with: xwd -display $DISPLAY -root | xwud"  echo "preview available with: xwd -display $DISPLAY -root | xwud"
125    
126  echo "making 'Screenshot' profile in $PROFILE_DIR"  echo "making 'Screenshot' profile in $PROFILE_DIR"
127  $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" | grep Success  $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
128    
129  echo "launching browser $BROWSER with $URL"  echo "launching browser $BROWSER with $URL"
130  $BROWSER -P Screenshot -width $W -height $H $FRAMESET &  $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
131  BROWSER_PID=$!  BROWSER_PID=$!
132    
133  echo "staring web server (wait for page to load)"  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            trap '' EXIT
141            exit 1
142    }
143    trap 'kill_browser' INT QUIT TERM SEGV EXIT
144    
145    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    xdotool search 'Restore' && xdotool key Escape && echo "simulated Esc to firefox restore sesison"
164    
165    $BROWSER -remote "openURL($FRAMESET)"
166    
167    echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
168    
169  # there is hard-coded limit here:  # there is hard-coded limit here:
170  # we will wait 60sec for page to load and render  # we will wait $WAIT sec for page to load and render
171    
172  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!"
173    
174  # get Mozilla Firefox window id (for resize)  # get Mozilla Firefox window id (for resize)
175  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 177  WINDOW_ID=`xwininfo -display $DISPLAY -r
177  if [ -z "$WINDOW_ID" ] ; then  if [ -z "$WINDOW_ID" ] ; then
178          echo "can't find window with name 'Mozilla Firefox'"          echo "can't find window with name 'Mozilla Firefox'"
179          exit 1          exit 1
180    else
181            echo "working on firefox window $WINDOW_ID"
182  fi  fi
183    
184  # move window to foreground  xdotool search firefox
 $XWIT -display $DISPLAY -id $WINDOW_ID -pop  
185    
186  echo "resizing window $WINDOW_ID to maximum size"  xdotool focus $WINDOW_ID
187  $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0  xdotool key F11
 $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H  
188    
189    ping_browser
 echo -n "wating for browser ping..."  
 while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do  
         echo -n "."  
         sleep 1  
 done  
 echo  
190    
191  # try to deduce inside area of window  # try to deduce inside area of window
192    
# Line 155  fi Line 204  fi
204    
205  import -window $DUMP_ID $RESIZE $FILE  import -window $DUMP_ID $RESIZE $FILE
206    
207  kill_x_server  if [ "$DEBUG" == 1 ] ; then
208            echo -n "press enter to exit! "
209            read
210    fi

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

  ViewVC Help
Powered by ViewVC 1.1.26