1 32 33 package websphinx.searchengine; 34 35 import websphinx.*; 36 import java.net.URL ; 37 import java.net.URLEncoder ; 38 import java.net.MalformedURLException ; 39 40 43 public class HotBot implements SearchEngine { 44 45 static Pattern patTitle = new Regexp ("^"); 46 47 static Pattern patCount = new Regexp ( 48 "Returned <B>(\\d+)</b> matches" 49 ); 50 static Pattern patNoHits = new Regexp ( 51 "Sorry -- your search yielded no results" 52 ); 53 54 static Pattern patResult = new Tagexp ( 56 "<td><b>(?{rank})</b></td>" + "<td>(?:<a><img></a>)?" + "(?{link}<a>(?{title})</a>)</td>" + "</tr><tr><td><font>(?{score})</font></td><td>(?{description})<br></td>" ); 61 62 static Pattern patMoreLink = new Tagexp ( 63 "<input type=image name=act.next>" 64 ); 65 66 77 public void classify (Page page) { 78 String title = page.getTitle (); 79 if (title != null && title.startsWith ("HotBot results:")) { 80 page.setObjectLabel ("searchengine.source", this); 81 82 Region count = patCount.oneMatch (page); 83 if (count != null) 84 page.setField ("searchengine.count", count.getField ("0")); 85 86 Region[] results = patResult.allMatches (page); 87 SearchEngineResult[] ser = new SearchEngineResult[results.length]; 89 for (int i=0; i<results.length; ++i) { 90 ser[i] = new SearchEngineResult (results[i]); 91 } 93 page.setFields ("searchengine.results", ser); 94 95 PatternMatcher m = patMoreLink.match (page); 96 while (m.hasMoreElements ()) { 97 Link link = (Link)m.nextMatch (); 98 link.setLabel ("searchengine.more-results"); 99 link.setLabel ("hyperlink"); 100 } 101 } 102 else System.err.println ("not a HotBot page"); 103 104 } 105 106 109 public static final float priority = 0.0F; 110 111 115 public float getPriority () { 116 return priority; 117 } 118 119 124 public URL makeQuery (String keywords) { 125 try { 126 return new URL ("http://www.search.hotbot.com/hResult.html/?SM=MC&MT=" 127 + URLEncoder.encode(keywords) 128 + "&DV=7&RG=.com&DC=10&DE=2&OPs=MDRTP&_v=2&DU=days&SW=web"); 129 } catch (MalformedURLException e) { 130 throw new RuntimeException ("internal error"); 131 } 132 } 133 134 138 public int getResultsPerPage () { 139 return 10; 140 } 141 142 147 public static Search search (String keywords) { 148 return new Search (new HotBot(), keywords); 149 } 150 151 158 public static Search search (String keywords, int maxResults) { 159 return new Search (new HotBot(), keywords, maxResults); 160 } 161 } 162
| Popular Tags
|