1 16 package org.apache.roller.util; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import org.jdom.Document; 30 import org.jdom.Element; 31 import org.jdom.input.SAXBuilder; 32 import org.jdom.xpath.XPath; 33 import org.xml.sax.EntityResolver ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 37 38 public class Technorati { 39 private String mKey = null; 40 41 public Technorati(String key) { 42 mKey = key; 43 } 44 45 public Technorati(String key, String proxy, int proxyPort) { 46 this(key); 47 System.setProperty("proxySet", "true"); 48 System.setProperty("http.proxyHost", proxy); 49 System.setProperty("http.proxyPort", Integer.toString(proxyPort)); 50 } 51 52 53 public Technorati() throws IOException { 54 InputStream is = getClass().getResourceAsStream("/technorati.license"); 55 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 56 mKey = br.readLine(); 57 } 58 59 public Technorati(String proxy, int proxyPort) throws Exception { 60 this(); 61 System.setProperty("proxySet", "true"); 62 System.setProperty("http.proxyHost", proxy); 63 System.setProperty("http.proxyPort", Integer.toString(proxyPort)); 64 } 65 66 public Result getLinkCosmos(String url) throws Exception { 67 return new Result("http://api.technorati.com/cosmos",url,"links"); 68 } 69 70 public Result getWeblogCosmos(String url) throws Exception { 71 return new Result("http://api.technorati.com/cosmos",url,"weblog"); 72 } 73 74 public Result getBloginfo(String url) throws Exception { 75 return new Result("http://api.technorati.com/bloginfo",url,null); 76 } 77 78 public Result getOutbound(String url) throws Exception { 79 return new Result("http://api.technorati.com/outbound",url,null); 80 } 81 82 83 public class Result { 84 private Weblog mWeblog = null; 85 private Collection mWeblogs = new ArrayList (); 86 87 protected Result(String apiUrl, String url, String type) throws Exception { 88 Map args = new HashMap (); 89 args.put("url", url); 90 args.put("type", type); 91 args.put("format", "xml"); 92 args.put("key", mKey); 93 94 int start = 0; 95 boolean repeat = true; 96 XPath itemsPath = XPath.newInstance("/tapi/document/item/weblog"); 97 98 while (repeat) { 99 Document doc = getRawResults(apiUrl,args); 100 Element elem = doc.getRootElement(); 101 String error = getString(elem,"/tapi/document/result/error"); 102 if ( error != null ) throw new Exception (error); 103 if (mWeblog == null) { 104 XPath p = XPath.newInstance("/tapi/document/result/weblog"); 105 Element w = (Element) p.selectSingleNode(doc); 106 mWeblog = new Weblog(w); 107 } 108 int count=0; 109 Iterator iter = itemsPath.selectNodes(doc).iterator(); 110 while (iter.hasNext()) { 111 Element element = (Element) iter.next(); 112 Weblog w = new Weblog(element); 113 mWeblogs.add(w); 114 count++; 115 } 116 if ( count < 20 ) { 117 repeat = false; 118 } 119 else { 120 start += 20; 121 args.put("start",new Integer (start)); 122 } 123 } 124 } 125 126 public Weblog getWeblog() {return mWeblog;} 127 public Collection getWeblogs() {return mWeblogs;} 128 } 129 130 131 public class Weblog { 132 private String mName = null; 133 private String mUrl = null; 134 private String mRssurl = null; 135 private String mLastupdate = null; 136 private String mNearestpermalink = null; 137 private String mExcerpt = null; 138 private int mInboundlinks = 0; 139 private int mInboundblogs = 0; 140 141 public Weblog(Element elem) throws Exception { 142 mName = getString(elem,"name"); 143 mUrl = getString(elem,"url"); 144 mRssurl = getString(elem,"rssurl"); 145 mLastupdate = getString(elem,"lastupdate"); 146 mNearestpermalink = getString(elem,"nearestpermalink"); 147 mExcerpt = getString(elem,"excerpt"); 148 try { 149 mInboundlinks = getInt(elem,"inboundlinks"); 150 } catch (Exception ignored) {} 151 try { 152 mInboundblogs = getInt(elem,"inboundblogs"); 153 } catch (Exception ignored) {} 154 } 155 156 public String getName() {return mName;} 157 public String getUrl() {return mUrl;} 158 public String getRssurl() {return mRssurl;} 159 public int getInboundblogs() {return mInboundblogs;} 160 public int getInboundlinks() {return mInboundlinks;} 161 public String getLastupdate() {return mLastupdate;} 162 public String getNearestpermalink() {return mNearestpermalink;} 163 public String getExcerpt() {return mExcerpt;} 164 } 165 166 protected Document getRawResults(String urlString, Map args) throws Exception { 167 int count = 0; 168 Iterator keys = args.keySet().iterator(); 169 while (keys.hasNext()) { 170 String sep = count++==0 ? "?" : "&"; 171 String name = (String )keys.next(); 172 if ( args.get(name) != null ) { 173 urlString += sep + name + "=" + args.get(name); 174 } 175 } 176 URL url = new URL (urlString); 177 URLConnection conn = url.openConnection(); 178 conn.connect(); 179 SAXBuilder builder = new SAXBuilder(); 180 return builder.build(conn.getInputStream()); 181 } 182 183 protected String getString(Element elem, String path) throws Exception { 184 XPath xpath = XPath.newInstance(path); 185 Element e = (Element)xpath.selectSingleNode(elem); 186 return e!=null ? e.getText() : null; 187 } 188 189 protected int getInt(Element elem, String path) throws Exception { 190 XPath xpath = XPath.newInstance(path); 191 Element e = (Element)xpath.selectSingleNode(elem); 192 return e!=null ? Integer.parseInt(e.getText()) : 0; 193 } 194 } 195 | Popular Tags |