Search in sources :

Example 1 with CardDominance

use of org.magic.api.beans.CardDominance in project MtgDesktopCompanion by nicho92.

the class MTGoldFishDashBoard method getBestCards.

@Override
public List<CardDominance> getBestCards(FORMAT f, String filter) throws IOException {
    // spells, creatures, all, lands
    String u = getString("WEBSITE") + "/format-staples/" + f.toString().toLowerCase() + "/full/" + filter;
    Document doc = read(u);
    logger.debug("get best cards : " + u);
    Elements trs = doc.select("table tr");
    trs.remove(0);
    trs.remove(0);
    List<CardDominance> ret = new ArrayList<>();
    for (Element e : trs) {
        Elements tds = e.select(MTGConstants.HTML_TAG_TD);
        try {
            int correct = filter.equalsIgnoreCase("lands") ? 1 : 0;
            CardDominance d = new CardDominance();
            d.setPosition(Integer.parseInt(tds.get(0).text()));
            d.setCardName(tds.get(1).text());
            d.setDominance(Double.parseDouble(tds.get(3 - correct).text().replaceAll("\\%", "")));
            d.setDecksPercent(Double.parseDouble(tds.get(4 - correct).text().replaceAll("\\%", "")));
            d.setPlayers(Double.parseDouble(tds.get(5 - correct).text()));
            ret.add(d);
        } catch (Exception ex) {
            logger.error("Error parsing " + tds, ex);
        }
    }
    return ret;
}
Also used : CardDominance(org.magic.api.beans.CardDominance) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 2 with CardDominance

use of org.magic.api.beans.CardDominance in project MtgDesktopCompanion by nicho92.

the class MTGStockDashBoard method getBestCards.

@Override
public List<CardDominance> getBestCards(FORMAT f, String filter) throws IOException {
    if (!connected)
        connect();
    List<CardDominance> ret = new ArrayList<>();
    int id = 1;
    switch(f) {
        case LEGACY:
            id = 1;
            break;
        case MODERN:
            id = 3;
            break;
        case STANDARD:
            id = 4;
            break;
        case VINTAGE:
            id = 2;
            break;
        // 1=Legacy 2=Vintage 3=Modern 4=Standard 7=Pauper
        default:
            break;
    }
    String url = MTGSTOCK_API_URI + "/analytics/mostplayed/" + id;
    HttpURLConnection con = getConnection(url);
    JsonObject obj = parser.parse(new JsonReader(new InputStreamReader(con.getInputStream()))).getAsJsonObject();
    JsonArray arr = obj.get("mostplayed").getAsJsonArray();
    int i = 1;
    for (JsonElement el : arr) {
        CardDominance cd = new CardDominance();
        cd.setCardName(el.getAsJsonObject().get("card").getAsJsonObject().get("name").getAsString());
        cd.setPlayers(el.getAsJsonObject().get("quantity").getAsDouble());
        cd.setPosition(i++);
        ret.add(cd);
    }
    return ret;
}
Also used : JsonArray(com.google.gson.JsonArray) HttpURLConnection(java.net.HttpURLConnection) CardDominance(org.magic.api.beans.CardDominance) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader)

Aggregations

ArrayList (java.util.ArrayList)2 CardDominance (org.magic.api.beans.CardDominance)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonReader (com.google.gson.stream.JsonReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 ParseException (java.text.ParseException)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1