/[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 63 by dpavlin, Wed Feb 6 23:34:35 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=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
63    fi
64    
65    if [ -z "`which $XSERVER`" ] ; then
66            echo "$0 really need $XSERVER to operate. please install it."
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 61  else Line 81  else
81          exit 1          exit 1
82  fi  fi
83    
84  echo "starting vnc X server"  echo "starting X server $XSERVER"
85    
86    export DISPLAY=:42
87    
 export DISPLAY=`$XSERVER -geometry ${W}x${H} -depth 24 2>&1 \  
         | grep : | head -1 | cut -d: -f2 | sed 's/^/:/'`  
88    
89  function kill_vnc() {  if [ "$DEBUG" == 1 ] ; then
90          vncserver -kill $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=$!
98    echo "X server pid $XSERVER_PID"
99    
100    function kill_x_server() {
101            echo "Killing server $XSERVER_PID"
102            kill $XSERVER_PID
103          rm -f $FRAMESET          rm -f $FRAMESET
104            rm -fr $PROFILE_DIR
105            trap '' EXIT
106            exit 1
107  }  }
108  trap 'echo "QUIT!" >&2; kill_vnc; 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 86  END_OF_FRAMESET Line 121  END_OF_FRAMESET
121    
122  echo "Using frameset html $FRAMESET"  echo "Using frameset html $FRAMESET"
123    
124  echo "preview available with: vncviewer $DISPLAY"  echo "preview available with: xwd -display $DISPLAY -root | xwud"
125    
126  echo "making 'Screenshot' profile"  echo "making 'Screenshot' profile in $PROFILE_DIR"
127  $BROWSER -CreateProfile Screenshot | 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 -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 &
131    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    $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
164    
165    echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
166    
167  # there is hard-coded limit here:  # there is hard-coded limit here:
168  # we will wait 60sec for page to load and render  # we will wait $WAIT sec for page to load and render
169    
170  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!"
171    
172  # get Mozilla Firefox window id (for resize)  # get Mozilla Firefox window id (for resize)
173    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`  
174    
175  if [ -z "$WINDOW_ID" ] ; then  if [ -z "$WINDOW_ID" ] ; then
176          echo "can't find window with name 'Mozilla Firefox'"          echo "can't find window with name 'Mozilla Firefox'"
177          exit 1          exit 1
178    else
179            echo "working on firefox window $WINDOW_ID"
180  fi  fi
181    
182  # move window to foreground  xdotool search firefox
 $XWIT -display $DISPLAY -id $WINDOW_ID -pop  
183    
184  echo "resizing window $WINDOW_ID to maximum size"  xdotool focus $WINDOW_ID
185  $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0  xdotool key F11
 $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H  
186    
187    ping_browser
 echo -n "wating for browser ping..."  
 while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do  
         echo -n "."  
         sleep 1  
 done  
 echo  
188    
189  # try to deduce inside area of window  # try to deduce inside area of window
190    
# Line 138  echo "saving window $DUMP_ID to $FILE" Line 199  echo "saving window $DUMP_ID to $FILE"
199  if [ ! -z "$RESIZE" ] ; then  if [ ! -z "$RESIZE" ] ; then
200          RESIZE="-geometry $RESIZE"          RESIZE="-geometry $RESIZE"
201  fi  fi
 import -window $DUMP_ID $RESIZE $FILE  
202    
203  kill_vnc  import -window $DUMP_ID $RESIZE $FILE
204    
205    if [ "$DEBUG" == 1 ] ; then
206            echo -n "press enter to exit! "
207            read
208    fi

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

  ViewVC Help
Powered by ViewVC 1.1.26