001: /*
002: * Copyright 2005 David M Johnson
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.roller.util;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.net.URL;
023: import java.net.URLConnection;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import org.jdom.Document;
030: import org.jdom.Element;
031: import org.jdom.input.SAXBuilder;
032: import org.jdom.xpath.XPath;
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: /** Simple Technorati wrapper for Java. @author David M Johnson */
038: public class Technorati {
039: private String mKey = null;
040:
041: public Technorati(String key) {
042: mKey = key;
043: }
044:
045: public Technorati(String key, String proxy, int proxyPort) {
046: this (key);
047: System.setProperty("proxySet", "true");
048: System.setProperty("http.proxyHost", proxy);
049: System.setProperty("http.proxyPort", Integer
050: .toString(proxyPort));
051: }
052:
053: /** Looks for key in classpath using "/technorati.license" */
054: public Technorati() throws IOException {
055: InputStream is = getClass().getResourceAsStream(
056: "/technorati.license");
057: BufferedReader br = new BufferedReader(
058: new InputStreamReader(is));
059: mKey = br.readLine();
060: }
061:
062: public Technorati(String proxy, int proxyPort) throws Exception {
063: this ();
064: System.setProperty("proxySet", "true");
065: System.setProperty("http.proxyHost", proxy);
066: System.setProperty("http.proxyPort", Integer
067: .toString(proxyPort));
068: }
069:
070: public Result getLinkCosmos(String url) throws Exception {
071: return new Result("http://api.technorati.com/cosmos", url,
072: "links");
073: }
074:
075: public Result getWeblogCosmos(String url) throws Exception {
076: return new Result("http://api.technorati.com/cosmos", url,
077: "weblog");
078: }
079:
080: public Result getBloginfo(String url) throws Exception {
081: return new Result("http://api.technorati.com/bloginfo", url,
082: null);
083: }
084:
085: public Result getOutbound(String url) throws Exception {
086: return new Result("http://api.technorati.com/outbound", url,
087: null);
088: }
089:
090: /** Technorati result with header info and collection of weblog items */
091: public class Result {
092: private Weblog mWeblog = null;
093: private Collection mWeblogs = new ArrayList();
094:
095: protected Result(String apiUrl, String url, String type)
096: throws Exception {
097: Map args = new HashMap();
098: args.put("url", url);
099: args.put("type", type);
100: args.put("format", "xml");
101: args.put("key", mKey);
102:
103: int start = 0;
104: boolean repeat = true;
105: XPath itemsPath = XPath
106: .newInstance("/tapi/document/item/weblog");
107:
108: while (repeat) {
109: Document doc = getRawResults(apiUrl, args);
110: Element elem = doc.getRootElement();
111: String error = getString(elem,
112: "/tapi/document/result/error");
113: if (error != null)
114: throw new Exception(error);
115: if (mWeblog == null) {
116: XPath p = XPath
117: .newInstance("/tapi/document/result/weblog");
118: Element w = (Element) p.selectSingleNode(doc);
119: mWeblog = new Weblog(w);
120: }
121: int count = 0;
122: Iterator iter = itemsPath.selectNodes(doc).iterator();
123: while (iter.hasNext()) {
124: Element element = (Element) iter.next();
125: Weblog w = new Weblog(element);
126: mWeblogs.add(w);
127: count++;
128: }
129: if (count < 20) {
130: repeat = false;
131: } else {
132: start += 20;
133: args.put("start", new Integer(start));
134: }
135: }
136: }
137:
138: public Weblog getWeblog() {
139: return mWeblog;
140: }
141:
142: public Collection getWeblogs() {
143: return mWeblogs;
144: }
145: }
146:
147: /** Technorati weblog representation */
148: public class Weblog {
149: private String mName = null;
150: private String mUrl = null;
151: private String mRssurl = null;
152: private String mLastupdate = null;
153: private String mNearestpermalink = null;
154: private String mExcerpt = null;
155: private int mInboundlinks = 0;
156: private int mInboundblogs = 0;
157:
158: public Weblog(Element elem) throws Exception {
159: mName = getString(elem, "name");
160: mUrl = getString(elem, "url");
161: mRssurl = getString(elem, "rssurl");
162: mLastupdate = getString(elem, "lastupdate");
163: mNearestpermalink = getString(elem, "nearestpermalink");
164: mExcerpt = getString(elem, "excerpt");
165: try {
166: mInboundlinks = getInt(elem, "inboundlinks");
167: } catch (Exception ignored) {
168: }
169: try {
170: mInboundblogs = getInt(elem, "inboundblogs");
171: } catch (Exception ignored) {
172: }
173: }
174:
175: public String getName() {
176: return mName;
177: }
178:
179: public String getUrl() {
180: return mUrl;
181: }
182:
183: public String getRssurl() {
184: return mRssurl;
185: }
186:
187: public int getInboundblogs() {
188: return mInboundblogs;
189: }
190:
191: public int getInboundlinks() {
192: return mInboundlinks;
193: }
194:
195: public String getLastupdate() {
196: return mLastupdate;
197: }
198:
199: public String getNearestpermalink() {
200: return mNearestpermalink;
201: }
202:
203: public String getExcerpt() {
204: return mExcerpt;
205: }
206: }
207:
208: protected Document getRawResults(String urlString, Map args)
209: throws Exception {
210: int count = 0;
211: Iterator keys = args.keySet().iterator();
212: while (keys.hasNext()) {
213: String sep = count++ == 0 ? "?" : "&";
214: String name = (String) keys.next();
215: if (args.get(name) != null) {
216: urlString += sep + name + "=" + args.get(name);
217: }
218: }
219: URL url = new URL(urlString);
220: URLConnection conn = url.openConnection();
221: conn.connect();
222: SAXBuilder builder = new SAXBuilder();
223: return builder.build(conn.getInputStream());
224: }
225:
226: protected String getString(Element elem, String path)
227: throws Exception {
228: XPath xpath = XPath.newInstance(path);
229: Element e = (Element) xpath.selectSingleNode(elem);
230: return e != null ? e.getText() : null;
231: }
232:
233: protected int getInt(Element elem, String path) throws Exception {
234: XPath xpath = XPath.newInstance(path);
235: Element e = (Element) xpath.selectSingleNode(elem);
236: return e != null ? Integer.parseInt(e.getText()) : 0;
237: }
238: }
|