scores API - java tool
To access scores via it's API, you can use the direct URL based access or the java-tool provided here.
You will need a locally installed Java-VM, that can be downloaded here.
The tool is available in a precompiled form, that may be executed via
java -jar scoresAPI.jar <parameters ... >
The required parameters are:
parameter |
description |
---|---|
url |
the scores url |
user |
the scores user |
pass |
the scores password |
start |
the startday of the report in yyyy-MM-dd |
end |
the endday (not included) of the report in yyyy-MM-dd |
res |
the resolution of the report in hour, day, week, month |
site |
the website to be reported |
path |
the path within the website to be reported. use "" for the entire site |
Example:
java -jar scoresAPI.jar https://scores.spring.de/scores-test testuser secret 2010-01-01 2010-01-02 day testsite homepage
this gives something like
timerange users clients sessions pis hits fis lis vtime vcnt clicks cent viewtime viewspersession sessionduration interval 2010-01-01 00:00:00.000 Fr GMT - 2010-01-02 00:00:00.000 Sa GMT 86400000 0.0 10.0 29.0 1464.0 0.0 28.0 29.0 7.7409E7 1273.0 0.0 0.0 60.808326787117046 50.48275862068966 3069.772083322047
The java source code is as follows
package de.spring.reports; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ScoresAPI { public static void usage() { System.out.println("please provide the following parameters"); System.out.println("url - the scores url"); System.out.println("user - the scores user"); System.out.println("pass - the scores password"); System.out.println("start - the startday of the report in yyyy-MM-dd"); System.out.println("end - the endday (not included) of the report in yyyy-MM-dd"); System.out.println("res - the resolution of the report in hour|day|week|month"); System.out.println("site - the website to be reported"); System.out.println("path - the path within the website to be reported. use \"\" for the entire site"); System.out.println("\nExample:"); System.out.println("https://scores.spring.de/scores-test testuser secret 2010-01-01 2010-01-02 hour testsite homepage"); } public static void main(String[] args) throws Exception { // set the timezone to UTC, as scores is running in UTC TimeZone.setDefault(TimeZone.getTimeZone("UTC")); DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); if(args.length != 8) { usage(); return; } String url = args[0] ; String user= args[1]; String pass= args[2]; ScoresAPI api = new ScoresAPI(); // // Perform the login and get a session-token for further requests // String session = api.doLogin(url, user, pass); String group = "visitors"; String report= "overview"; Date start = dateformat.parse(args[3]); Date end = dateformat.parse(args[4]); String res = args[5]; String site = args[6]; String cp = args[7]; // // request the report defined by the arguments, this returns a DOM Document // Document document = api.getData(url, session, group, report, start, end, api.getResolution(res), site, cp); // // the document can now be queried, this example shows xpath-queries // and gives the data as TAB-separated text values, incl. a header line. // XPath xpath = XPathFactory.newInstance().newXPath(); NodeList lines = (NodeList)xpath.evaluate("/scores/report/data/vector", document, XPathConstants.NODESET); boolean firstLine = true; for(int i=0;i<lines.getLength();i++) { NodeList columns = (NodeList)xpath.evaluate("dimension", lines.item(i), XPathConstants.NODESET); if(firstLine) { for(int j=0;j<columns.getLength();j++) { Node column = columns.item(j); System.out.print(xpath.evaluate("@name", column, XPathConstants.STRING)); System.out.print("\t"); } System.out.print("\n"); firstLine = false; } for(int j=0;j<columns.getLength();j++) { Node column = columns.item(j); System.out.print(xpath.evaluate("value", column, XPathConstants.STRING)); System.out.print("\t"); } System.out.print("\n"); } } public long getResolution(String res) { if("hour".equals(res)) return 3600000L; if("day".equals(res)) return 86400000L; if("week".equals(res)) return 7*86400000L; if("month".equals(res)) return 2592000000L; throw new UnsupportedOperationException("unsupported resolution " + res); } public Document getData(String scoresurl, String session, String group, String report, Date start, Date end, long resolution, String site, String page) throws IOException, SAXException, ParserConfigurationException { String TZ = TimeZone.getDefault().getID(); String data = "format=api&tz="+TZ+"&" + "&session=" + URLEncoder.encode(session, "UTF-8") + "&group=" +URLEncoder.encode(group, "UTF-8") + "&report=" +URLEncoder.encode(report, "UTF-8") + "&0d_timerange=" + URLEncoder.encode(start.getTime() + " " + end.getTime() + " " + resolution, "UTF-8") + "&0d_site=" + URLEncoder.encode(site, "UTF-8") + "&0d_cp=" + URLEncoder.encode(page, "UTF-8"); URL url = new URL(scoresurl + "/export?" + data); URLConnection conn = url.openConnection(); return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(conn.getInputStream()); } public String doLogin(String scoresurl, String user, String pass) throws IOException { // Construct data String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8"); data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8"); // Send data URL url = new URL(scoresurl + "/export"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; String session = null; while ((line = rd.readLine()) != null) { session = line; break; } wr.close(); rd.close(); return session; } }