package dk.thoerup.telmorewidget; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.jsoup.Connection; import org.jsoup.Connection.Method; import org.jsoup.Connection.Response; import org.jsoup.helper.HttpConnection; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class TelmoreFetcher { public static class Result { public String saldo; public String internetUsage; } public Result fetch(String username, String password) throws IOException { Map params = new HashMap(); params.put("j_username", username); params.put("j_password", password); String url = "https://www.telmore.dk/t2/j_security_check"; Connection con = HttpConnection.connect(url); con.data(params); con.followRedirects(false); con.method( Method.POST ); Response resp = con.execute(); //System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); String JSESSIONID = resp.cookie("JSESSIONID"); String location = resp.header( "Location" ); /////////////////////////////////////////////////////////////////////////////////// resp = HttpConnection.connect(location) .followRedirects(false) .cookie("JSESSIONID", JSESSIONID) .method( Method.GET ) .execute(); //System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); location = resp.header( "Location" ); /////////////////////////////////////////////////////////////////////////////////// resp = HttpConnection.connect(location) .followRedirects(false) .cookie("JSESSIONID", JSESSIONID) .method( Method.GET ) .execute(); //System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); Document doc = resp.parse(); String iframeTarget = doc.getElementById("remoteIframePage").attr("abs:src"); /////////////////////////////////////////////////////////////////////////////////// resp = HttpConnection.connect(iframeTarget) .referrer(location) .cookie("JSESSIONID", JSESSIONID) .cookie("SS_X_JSESSIONID", resp.cookie("SS_X_JSESSIONID")) .followRedirects(false) .method( Method.GET ) .execute(); //System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); doc = resp.parse(); Element iframeBody = doc.getElementById("iframeBody"); Element saldo = iframeBody.getElementsByClass("saldo").first(); //System.out.println("Saldo:" + saldo.text() ); //TODO: der må være nogle bedre selectors end style Elements internet = iframeBody.getElementsByAttributeValue("style", "padding-top: 1px;"); Element internetMax = iframeBody.getElementsByAttributeValue("style", "width:36px;").get(1); //System.out.println( "internet count: " + internet.size() ); //System.out.println( "internet: " + internet.first().text() ); Result res = new Result(); res.saldo = saldo.text() + " kr."; res.internetUsage = internet.first().text() + " of " + internetMax.text() ; return res; } /* void dumpHeaders(Response resp) { dumpStringmap( resp.headers() ); } void dumpCookies(Response resp) { dumpStringmap(resp.cookies()); } void dumpStringmap(Map map) { for (String key : map.keySet() ) { String val = map.get(key); System.out.println(">>" + key + ": " + val ); } }*/ }