service seacat start
service seacat stop
service seacat status
usage: seacatctl command where command is: 1) list list current settings 2) set [port|rcj|vxsmin|vxsmax|pictureHome|captureWaitTime|waterMark|maxMatch|maxWidth|maxHeight] value set config 3) proxy [ on <IP> <PORT> | off ] set or clear proxy setting 4) help print this help info
Example 1: seacatctl list List current settings. Example 2: seacatctl set port 4444 Set Seacat listen on 4444 port. Example 3: seacatctl set rcj 4444-5555-6666 Set register code to 4444-5555-6666. Example 4: seacatctl set vxsmin 5 Start 5 virtual browser when Seacat starts. Example 5: seacatctl set pictureHome /root Default picture storage home is /root. Example 6: seacatctl set captureWaitTime 1500 Set wait time before capture to 1500ms. Example 7: seacatctl set waterMark zhuatang.com Set watermark text to zhuatang.com. Example 8: seacatctl proxy on 192.168.28.91 8080 Set proxy : 192.168.28.91, port 8080. Example 9: seacatctl proxy off Clear proxy. Example 10: seacatctl set maxMatch 2 Set maximum acceptable elements number matched on class or xpath Example 11: seacatctl set maxWidth 1200 Set maximum acceptable width of full page. when the real width of full page exceeds maxWidth, Seacat will do nothing. No limits when maxWidth=0. Example 12: seacatctl set maxHeight 2000 Set maximum acceptable height of full page. when the real height of full page exceeds maxHeight, Seacat will do nothing. No limits when maxHeight=0. Note:vxsmax not used.
usage: usage: capture [-h host] [-p port] [-f] [-s WWWxHHH...] [-w timeToWait] [-o imageFormat] [-P prefix] [-i ids ] [-c classes] [-x xpath] [-r rects] url1 url2 ...
Options: -h host : seacat host, default: localhost -p port : seacat port, default: 5060 -f : enable full page capture -s WWWxHHH... : one or more thumbnail size list, WWW is thumbnail width, HHH is thumbnail height -w timeToWait : wait time before capture, time unit: millisecond -o imageFormat : default value: png -P prefix : prefix of picture -i ids : ids is one or more element id in page -c classes : classes is one or more element class name in page -x xpath : xpath is one xpath expression which target element must match -r rects: one or more rectangle to capture, default is "0 0 1024 768", meas x=0,y=0,width=1024,height=768 url1 url2 ... are URLs you want to take snapshot on.
Output: <url> {+++|---} <scid> +++ capture OK --- capture failed
Example1: capture http://www.zhuatang.com Example2: capture -s "109x82 330x220" http://www.zhuatang.com Example3: capture -f -s "82x78 400x300" http://www.zhuatang.com http://www.zaobao.com
Default listen on port: 5060
Request - client send snapshot request as follows (Each line ends with <LF>, <LF> stands for new line character, request ends with blank line)
get <url> <LF>
viewport-id <id1> <id2> ... or viewport-class <class1> <class2> ... or viewport-xpath <xpath> or viewport-rect: <x> <y> <width> <height>... <LF>
thumb-sizes <size1> <size2> ... <LF>
enable-full <LF>
keep-origin <LF>
picture-home <pictureHome> <LF>
prefix <prefix> <LF>
suffix <suffix> <LF>
content-length <length> <LF>
<LF>
<contents>
Note:<url> is url you'll take snapshot on, it's a must; <id1> <id2>... are one or more element id values; <class1> <class2> ... are one or more element class values. If class value has space character, it must by surrounded by single quote. <xpath> is for locate. <x> and <y> are the top left corner of capture region, <width> is image width, <height> is image height; <sizeN> is thumbnail size, it's format: WWWxHHH, WWW is snapshot width, HHH is snapshot height. Take full page snapshot when enable-full is on; when keep-origin is on, Seacat will keep the original picture for thumbnail. <pictureHome> is directory of image storage; <prefix> specify prefix of snapshot file; <suffix> specify suffix of snapshot file; When you want post some data or arguments to specified url, you must specify <length> and <contents>.
Response message (Snapshot OK)
SEACAT/5.5 200 OK <LF>
SCID: <SCID> <LF>
<LF>
Note: <SCID> is prefix of snapshot file (including path), snapshot filename format is <SCID>-WWWxHHH.<suffix>, full page snapshot filename format is <SCID>-full.<suffix>. <suffix> is image format, default png.
Response message(Snapshot failed)
SEACAT/5.5 <code> <message><LF>
<LF>
package com.zhsoft88.commons; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FilenameFilter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Arrays; import org.apache.commons.lang.math.NumberUtils; /** * seacat capture * @author zhsoft88 * @since 2008-4-13 * @update 2008-12-13 */ public class Seacat { public static final int PORT = 5060; /** * capture result * @author zhsoft88 * @since 2008-4-13 */ public static class SeacatResult { private int statusCode; private String statusText; private String scid; private String geometry; private long elaspedTime; private String prefix; private String pictureHome; private String[] suffixList; public SeacatResult() { // TODO Auto-generated constructor stub } public int getStatusCode() { return statusCode; } public String getStatusText() { return statusText; } public String getScid() { return scid; } public String getGeometry() { return geometry; } public long getElaspedTime() { return elaspedTime; } public String getPrefix() { return prefix; } public String getPictureHome() { return pictureHome; } public String[] getSuffixList() { return suffixList; } protected void setStatusCode(int statusCode) { this.statusCode = statusCode; } protected void setStatusText(String statusText) { this.statusText = statusText; } protected void setScid(String scid) { this.scid = scid; } protected void setGeometry(String geometry) { this.geometry = geometry; } protected void setElaspedTime(long elaspedTime) { this.elaspedTime = elaspedTime; } protected void setPrefix(String prefix) { this.prefix = prefix; } protected void setPictureHome(String pictureHome) { this.pictureHome = pictureHome; } protected void setSuffixList(String[] suffixList) { this.suffixList = suffixList; } public void dump() { System.out.println("SeacatResult:"); System.out.println("statusCode="+statusCode); System.out.println("statusText="+statusText); System.out.println("scid="+scid); System.out.println("geometry="+geometry); System.out.println("prefix="+prefix); System.out.println("suffixList length="+suffixList.length); for (int i=0;i<suffixList.length;i++) { System.out.println("suffixList["+i+"]="+suffixList[i]); } } } /** * capture configuration * @author zhsoft88 * @since 2008-4-13 * @update 2008-12-13 */ public static class SeacatConf { private String url; private String viewportId; private String viewportClass; private String viewportXpath; private String viewportRect; private String thumbSizes; private int waitTime; private boolean enableFull; private String pictureHome; private String prefix; private String suffix; private String postData; private boolean keepOrigin; public SeacatConf() { waitTime = -1; enableFull = false; keepOrigin = false; pictureHome = "/tmp"; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getThumbSizes() { return thumbSizes; } public void setThumbSizes(String thumbSizes) { this.thumbSizes = thumbSizes; } public int getWaitTime() { return waitTime; } public void setWaitTime(int waitTime) { this.waitTime = waitTime; } public boolean isEnableFull() { return enableFull; } public void setEnableFull(boolean enableFull) { this.enableFull = enableFull; } public String getPictureHome() { return pictureHome; } public void setPictureHome(String pictureHome) { this.pictureHome = pictureHome; } public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getPostData() { return postData; } public void setPostData(String postData) { this.postData = postData; } public String getViewportId() { return viewportId; } public void setViewportId(String viewportId) { this.viewportId = viewportId; } public String getViewportClass() { return viewportClass; } public void setViewportClass(String viewportClass) { this.viewportClass = viewportClass; } public String getViewportXpath() { return viewportXpath; } public void setViewportXpath(String viewportXpath) { this.viewportXpath = viewportXpath; } public String getViewportRect() { return viewportRect; } public void setViewportRect(String viewportRect) { this.viewportRect = viewportRect; } public boolean isKeepOrigin() { return keepOrigin; } public void setKeepOrigin(boolean keepOrigin) { this.keepOrigin = keepOrigin; } } private String host; private int port; public Seacat() { this("localhost"); } public Seacat(String host) { this(host,PORT); } public Seacat(String host,int port) { this.host = host; this.port = port; } /** * capture web page * @param conf * @return * @throws Exception */ public SeacatResult capture(SeacatConf conf) throws Exception { long t1 = System.currentTimeMillis(); Socket socket = new Socket(host,port); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); if (conf.getUrl()!=null) { bw.write("get "+conf.getUrl()+"\r\n"); } if (conf.getViewportId()!=null) { bw.write("viewport-id "+conf.getViewportId()+"\r\n"); } if (conf.getViewportClass()!=null) { bw.write("viewport-class "+conf.getViewportClass()+"\r\n"); } if (conf.getViewportXpath()!=null) { bw.write("viewport-xpath "+conf.getViewportXpath()+"\r\n"); } if (conf.getViewportRect()!=null) { bw.write("viewport-rect "+conf.getViewportRect()+"\r\n"); } if (conf.getThumbSizes()!=null) { bw.write("thumb-sizes "+conf.getThumbSizes()+"\r\n"); } if (conf.isEnableFull()) { bw.write("enable-full\r\n"); } if (conf.getWaitTime()!=-1) { bw.write("wait-time "+conf.getWaitTime()+"\r\n"); } if (conf.getPictureHome()!=null) { bw.write("picture-home "+conf.getPictureHome()+"\r\n"); } if (conf.getPrefix()!=null) { bw.write("prefix "+conf.getPrefix()+"\r\n"); } if (conf.getSuffix()!=null) { bw.write("suffix "+conf.getSuffix()+"\r\n"); } if (conf.getPostData()!=null&&conf.getPostData().length()>0) { bw.write("content-length "+conf.getPostData().length()+"\r\n"); } if (conf.isKeepOrigin()) { bw.write("keep-origin\r\n"); } bw.write("\r\n"); if (conf.getPostData()!=null&&conf.getPostData().length()>0) { bw.write(conf.getPostData()); } bw.flush(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8")); String line = br.readLine(); char[] ca = line.toCharArray(); int i = 0; while (i<ca.length&&ca[i]!=' ') i++; while (i<ca.length&&ca[i]==' ') i++; // get status code StringBuilder sb = new StringBuilder(4); while (i<ca.length&&ca[i]!=' ') { sb.append(ca[i]); i++; } int statusCode = NumberUtils.toInt(sb.toString()); // get status text while (i<ca.length&&ca[i]==' ') i++; String statusText = line.substring(i); // get scid & geometry String scid = null; String geometry = null; while ((line=br.readLine())!=null) { if (line.length()==0) break; if (line.startsWith("SCID: ")) { scid = line.substring(6); } else if (line.startsWith("Geometry: ")) { geometry = line.substring(10); } } socket.close(); // String pictureHome = null; String prefix = null; String[] suffixList = new String[0]; if (scid!=null) { pictureHome = scid.substring(0,scid.lastIndexOf('/')); prefix = scid.substring(scid.lastIndexOf('/')+1); final String aPrefix = prefix; // get suffix list File[] list = new File(conf.getPictureHome()).listFiles(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { return name.startsWith(aPrefix); } }); suffixList = new String[list.length]; for (int k=0;k<list.length;k++) { suffixList[k] = list[k].getName().substring(prefix.length()+1); } Arrays.sort(suffixList); } long t2 = System.currentTimeMillis(); SeacatResult result = new SeacatResult(); result.setStatusCode(statusCode); result.setStatusText(statusText); result.setScid(scid); result.setElaspedTime(t2-t1); result.setGeometry(geometry); result.setPictureHome(pictureHome); result.setPrefix(prefix); result.setSuffixList(suffixList); return result; } }
package com.zhsoft88.commons.tests; import com.zhsoft88.commons.Seacat; import com.zhsoft88.commons.Seacat.SeacatConf; import com.zhsoft88.commons.Seacat.SeacatResult; /** * seacat tester * @author zhsoft88 * * @since 2008-12-13 */ public class TestSeacat { /** * @param args */ public static void main(String[] args) throws Exception { Seacat seacat = new Seacat(); { // get snapshot by id SeacatConf conf = new SeacatConf(); conf.setUrl("http://www.google.com"); conf.setViewportId("gbar"); conf.setThumbSizes("160x120"); conf.setKeepOrigin(true); SeacatResult result = seacat.capture(conf); result.dump(); } { // get snapshot by class SeacatConf conf = new SeacatConf(); conf.setUrl("http://news.google.com"); conf.setViewportClass("lh"); conf.setThumbSizes("160x120"); conf.setEnableFull(true); conf.setKeepOrigin(true); SeacatResult result = seacat.capture(conf); result.dump(); } { // get snapshot by xpath SeacatConf conf = new SeacatConf(); conf.setUrl("http://news.google.com"); conf.setViewportXpath("//*[@id='topSection']"); conf.setThumbSizes("160x120"); conf.setKeepOrigin(true); SeacatResult result = seacat.capture(conf); result.dump(); } { // get snapshot by specific region SeacatConf conf = new SeacatConf(); conf.setUrl("http://news.google.com"); conf.setViewportRect("200 200 400 300"); conf.setThumbSizes("160x120"); conf.setKeepOrigin(true); SeacatResult result = seacat.capture(conf); result.dump(); } } }