KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > Technorati


1 /*
2  * Copyright 2005 David M Johnson
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.roller.util;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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 JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /** Simple Technorati wrapper for Java. @author David M Johnson */
38 public class Technorati {
39     private String JavaDoc mKey = null;
40
41     public Technorati(String JavaDoc key) {
42         mKey = key;
43     }
44     
45     public Technorati(String JavaDoc key, String JavaDoc 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     /** Looks for key in classpath using "/technorati.license" */
53     public Technorati() throws IOException JavaDoc {
54         InputStream JavaDoc is = getClass().getResourceAsStream("/technorati.license");
55         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
56         mKey = br.readLine();
57     }
58
59     public Technorati(String JavaDoc proxy, int proxyPort) throws Exception JavaDoc {
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 JavaDoc url) throws Exception JavaDoc {
67         return new Result("http://api.technorati.com/cosmos",url,"links");
68     }
69
70     public Result getWeblogCosmos(String JavaDoc url) throws Exception JavaDoc {
71         return new Result("http://api.technorati.com/cosmos",url,"weblog");
72     }
73
74     public Result getBloginfo(String JavaDoc url) throws Exception JavaDoc {
75         return new Result("http://api.technorati.com/bloginfo",url,null);
76     }
77
78     public Result getOutbound(String JavaDoc url) throws Exception JavaDoc {
79         return new Result("http://api.technorati.com/outbound",url,null);
80     }
81
82     /** Technorati result with header info and collection of weblog items */
83     public class Result {
84         private Weblog mWeblog = null;
85         private Collection JavaDoc mWeblogs = new ArrayList JavaDoc();
86
87         protected Result(String JavaDoc apiUrl, String JavaDoc url, String JavaDoc type) throws Exception JavaDoc {
88             Map JavaDoc args = new HashMap JavaDoc();
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 JavaDoc error = getString(elem,"/tapi/document/result/error");
102                 if ( error != null ) throw new Exception JavaDoc(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 JavaDoc 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 JavaDoc(start));
122                 }
123             }
124         }
125
126         public Weblog getWeblog() {return mWeblog;}
127         public Collection JavaDoc getWeblogs() {return mWeblogs;}
128     }
129
130     /** Technorati weblog representation */
131     public class Weblog {
132         private String JavaDoc mName = null;
133         private String JavaDoc mUrl = null;
134         private String JavaDoc mRssurl = null;
135         private String JavaDoc mLastupdate = null;
136         private String JavaDoc mNearestpermalink = null;
137         private String JavaDoc mExcerpt = null;
138         private int mInboundlinks = 0;
139         private int mInboundblogs = 0;
140
141         public Weblog(Element elem) throws Exception JavaDoc {
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 JavaDoc ignored) {}
151             try {
152                 mInboundblogs = getInt(elem,"inboundblogs");
153             } catch (Exception JavaDoc ignored) {}
154         }
155
156         public String JavaDoc getName() {return mName;}
157         public String JavaDoc getUrl() {return mUrl;}
158         public String JavaDoc getRssurl() {return mRssurl;}
159         public int getInboundblogs() {return mInboundblogs;}
160         public int getInboundlinks() {return mInboundlinks;}
161         public String JavaDoc getLastupdate() {return mLastupdate;}
162         public String JavaDoc getNearestpermalink() {return mNearestpermalink;}
163         public String JavaDoc getExcerpt() {return mExcerpt;}
164     }
165     
166     protected Document getRawResults(String JavaDoc urlString, Map JavaDoc args) throws Exception JavaDoc {
167         int count = 0;
168         Iterator JavaDoc keys = args.keySet().iterator();
169         while (keys.hasNext()) {
170             String JavaDoc sep = count++==0 ? "?" : "&";
171             String JavaDoc name = (String JavaDoc)keys.next();
172             if ( args.get(name) != null ) {
173                 urlString += sep + name + "=" + args.get(name);
174             }
175         }
176         URL JavaDoc url = new URL JavaDoc(urlString);
177         URLConnection JavaDoc conn = url.openConnection();
178         conn.connect();
179         SAXBuilder builder = new SAXBuilder();
180         return builder.build(conn.getInputStream());
181     }
182     
183     protected String JavaDoc getString(Element elem, String JavaDoc path) throws Exception JavaDoc {
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 JavaDoc path) throws Exception JavaDoc {
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