Transfer of metrics from Knowledge Forum to Bb
From AnalysisWiki
NOTE: the following URL is not yet implemented.
A GET request to the following URL:
http://analysis.ikit.org/bb/report?course=ABC1234F07&user=myutorid
On success:
<results> <result description="totalNotes">10</result> <result description="buildonNotes">8</result> <result description="viewsWorkedIn">2</result> <result description="percentageNotesRead">72.3</result> </results>
On failure:
<results> </results>
No substantive error messages will be provided in this iteration of the software.
Some sample Java code to parse through an XML doc (from http://www.simongbrown.com/blog/2005/06/22/yahoo_term_extraction.html)
package pebble.event.blogentry;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import pebble.blog.Blog;
import pebble.blog.BlogEntry;
public class YahooTermExtractionListener extends BlogEntryListenerSupport {
private static final String URL =
"http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction";
private static final Log log =
LogFactory.getLog(YahooTermExtractionListener.class);
public void blogEntryAdded(BlogEntryEvent event) {
tag(event.getBlogEntry());
}
private void tag(BlogEntry blogEntry) {
Blog blog = blogEntry.getRootBlog();
try {
// post to the service using HttpClient
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(URL);
postMethod.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=" +
blog.getCharacterEncoding());
NameValuePair[] data = {
new NameValuePair("appid", "PebbleWeblog"),
new NameValuePair("context", blogEntry.getBody()),
};
postMethod.addParameters(data);
int responseCode = httpClient.executeMethod(postMethod);
if (responseCode != 200) {
return;
}
// now parse the response and extract the Result elements
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
log.warn(e);
throw e;
}
public void error(SAXParseException e) throws SAXException {
log.error(e);
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
log.fatal(e);
throw e;
}
});
// add the terms to the list of tags for the blog entry
StringBuffer tags = new StringBuffer(blogEntry.getTags());
InputStream in = postMethod.getResponseBodyAsStream();
Document doc = builder.parse(in);
in.close();
NodeList results = doc.getElementsByTagName("Result");
if (results != null) {
for (int i = 0; i < results.getLength(); i++) {
Node node = results.item(i);
String tag = getTextValue(node);
if (tags.length() > 0) {
tags.append(", ");
}
tags.append(tag);
}
}
blogEntry.setTags(tags.toString());
blogEntry.store();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
private String getTextValue(Node node) {
if (node.hasChildNodes()) {
return node.getFirstChild().getNodeValue();
} else {
return "";
}
}
}
