Search in sources :

Example 1 with CardShake

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

the class MTGPriceDashBoard method getShakeForEdition.

@Override
public List<CardShake> getShakeForEdition(MagicEdition edition) throws IOException {
    String name = convert(edition.getSet()).replaceAll(" ", "_");
    String url = "http://www.mtgprice.com/spoiler_lists/" + name;
    logger.debug("get Prices for " + name + " " + url);
    Document doc = Jsoup.connect(url).userAgent(getString("USER_AGENT")).timeout(Integer.parseInt(getString("TIMEOUT"))).get();
    Element table = doc.getElementsByTag("body").get(0).getElementsByTag("script").get(2);
    List<CardShake> list = new ArrayList<>();
    String data = table.html();
    data = data.substring(data.indexOf('['), data.indexOf(']') + 1);
    JsonElement root = new JsonParser().parse(data);
    JsonArray arr = root.getAsJsonArray();
    for (int i = 0; i < arr.size(); i++) {
        JsonObject card = arr.get(i).getAsJsonObject();
        CardShake shake = new CardShake();
        shake.setName(card.get("name").getAsString());
        shake.setEd(edition.getId());
        shake.setPrice(card.get("fair_price").getAsDouble());
        try {
            shake.setPercentDayChange(card.get("percentageChangeSinceYesterday").getAsDouble());
        } catch (Exception e) {
            MTGLogger.printStackTrace(e);
        }
        try {
            shake.setPercentWeekChange(card.get("percentageChangeSinceOneWeekAgo").getAsDouble());
        } catch (Exception e) {
            MTGLogger.printStackTrace(e);
        }
        shake.setPriceDayChange(card.get("absoluteChangeSinceYesterday").getAsDouble());
        shake.setPriceWeekChange(card.get("absoluteChangeSinceOneWeekAgo").getAsDouble());
        list.add(shake);
    }
    return list;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Document(org.jsoup.nodes.Document) CardShake(org.magic.api.beans.CardShake) ParseException(java.text.ParseException) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 2 with CardShake

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

the class MTGPriceDashBoard method getShakerFor.

@Override
public List<CardShake> getShakerFor(String gameFormat) throws IOException {
    List<CardShake> list = new ArrayList<>();
    String url = getString("WEBSITE") + "/taneLayout/mtg_price_tracker.jsp?period=" + getString("PERIOD");
    Document doc = Jsoup.connect(url).userAgent(getString("USER_AGENT")).timeout(Integer.parseInt(getString("TIMEOUT"))).get();
    try {
        logger.debug("Get Shake for " + url);
        String date = doc.getElementsByClass("span6").get(1).text().replaceAll("Updated:", "").replaceAll("UTC ", "").trim();
        SimpleDateFormat forma = new SimpleDateFormat("E MMMM dd hh:mm:ss yyyy", Locale.ENGLISH);
        updateTime = forma.parse(date);
    } catch (ParseException e1) {
        logger.error(e1);
    }
    if (gameFormat.equalsIgnoreCase("STANDARD"))
        gameFormat = "Standard";
    else if (gameFormat.equalsIgnoreCase("MODERN"))
        gameFormat = "Modern";
    else if (gameFormat.equalsIgnoreCase("VINTAGE"))
        gameFormat = "Vintage";
    else if (gameFormat.equalsIgnoreCase("LEGACY"))
        gameFormat = "All";
    Element table = doc.getElementById("top50" + gameFormat);
    Element table2 = doc.getElementById("bottom50" + gameFormat);
    try {
        for (Element e : table.select(MTGConstants.HTML_TAG_TR)) {
            CardShake cs = new CardShake();
            cs.setName(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(0).text().trim());
            cs.setPrice(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(2).text()));
            cs.setPriceDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(4).text()));
            String set = e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(1).text();
            set = set.replaceAll("_\\(Foil\\)", "");
            cs.setEd(getCodeForExt(set));
            list.add(cs);
        }
        for (Element e : table2.select(MTGConstants.HTML_TAG_TR)) {
            CardShake cs = new CardShake();
            cs.setName(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(0).text().trim());
            cs.setPrice(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(2).text()));
            cs.setPriceDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(4).text()));
            String set = e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(1).text();
            set = set.replaceAll("_\\(Foil\\)", "");
            cs.setEd(getCodeForExt(set));
            list.add(cs);
        }
    } catch (Exception e) {
        logger.error("error retrieve cardshake for " + gameFormat, e);
    }
    return list;
}
Also used : JsonElement(com.google.gson.JsonElement) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) Document(org.jsoup.nodes.Document) SimpleDateFormat(java.text.SimpleDateFormat) CardShake(org.magic.api.beans.CardShake) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 3 with CardShake

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

the class MTGStockDashBoard method getShakerFor.

@Override
public List<CardShake> getShakerFor(String gameFormat) throws IOException {
    connect();
    List<CardShake> ret = new ArrayList<>();
    for (String filter : new String[] { "normal", "foil" }) for (JsonElement el : interests.get(getString("FORMAT_SHAKER")).getAsJsonObject().get(filter).getAsJsonArray()) {
        if (el.getAsJsonObject().get("print").getAsJsonObject().get("legal").getAsJsonObject().get(gameFormat.toLowerCase()) != null && el.getAsJsonObject().get("print").getAsJsonObject().get("legal").getAsJsonObject().get(gameFormat.toLowerCase()).getAsString().equalsIgnoreCase("legal")) {
            CardShake cs = new CardShake();
            cs.setName(el.getAsJsonObject().get("print").getAsJsonObject().get("name").getAsString());
            cs.setPrice(el.getAsJsonObject().get("present_price").getAsDouble());
            cs.setPercentDayChange(el.getAsJsonObject().get("percentage").getAsDouble());
            cs.setPriceDayChange(el.getAsJsonObject().get("present_price").getAsDouble() - el.getAsJsonObject().get("past_price").getAsDouble());
            cs.setDateUpdate(new Date(el.getAsJsonObject().get("date").getAsLong()));
            correspondance.forEach((key, value) -> {
                if (value == el.getAsJsonObject().get("print").getAsJsonObject().get("set_id").getAsInt()) {
                    cs.setEd(key);
                }
            });
            ret.add(cs);
        }
    }
    return ret;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JsonObject(com.google.gson.JsonObject) GregorianCalendar(java.util.GregorianCalendar) URL(java.net.URL) Date(java.util.Date) CardShake(org.magic.api.beans.CardShake) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser) InputStreamReader(java.io.InputStreamReader) StringUtils(org.apache.commons.lang3.StringUtils) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) STATUT(org.magic.api.interfaces.MTGCardsProvider.STATUT) JsonElement(com.google.gson.JsonElement) List(java.util.List) MagicEdition(org.magic.api.beans.MagicEdition) JsonArray(com.google.gson.JsonArray) Calendar(java.util.Calendar) TreeMap(java.util.TreeMap) Map(java.util.Map) AbstractDashBoard(org.magic.api.interfaces.abstracts.AbstractDashBoard) MagicCard(org.magic.api.beans.MagicCard) CardDominance(org.magic.api.beans.CardDominance) MTGConstants(org.magic.services.MTGConstants) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) CardShake(org.magic.api.beans.CardShake) Date(java.util.Date)

Example 4 with CardShake

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

the class MTGStockDashBoard method getShakeForEdition.

@Override
public List<CardShake> getShakeForEdition(MagicEdition edition) throws IOException {
    connect();
    List<CardShake> list = new ArrayList<>();
    String url = MTGSTOCK_API_URI + "/card_sets/" + correspondance.get(edition.getId());
    HttpURLConnection con = getConnection(url);
    JsonObject obj = parser.parse(new JsonReader(new InputStreamReader(con.getInputStream()))).getAsJsonObject();
    JsonArray arr = obj.get("prints").getAsJsonArray();
    for (JsonElement el : arr) {
        CardShake cs = new CardShake();
        cs.setName(el.getAsJsonObject().get("name").getAsString());
        cs.setEd(edition.getId());
        cs.setPrice(el.getAsJsonObject().get("latest_price").getAsJsonObject().get("avg").getAsDouble());
        list.add(cs);
    }
    con.disconnect();
    return list;
}
Also used : JsonArray(com.google.gson.JsonArray) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) CardShake(org.magic.api.beans.CardShake)

Example 5 with CardShake

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

the class MTGoldFishDashBoard method getShakeForEdition.

public List<CardShake> getShakeForEdition(MagicEdition edition) throws IOException {
    String oldID = edition.getId();
    String urlEditionChecker = "";
    List<CardShake> list = new ArrayList<>();
    if (edition.isOnlineOnly())
        urlEditionChecker = getString("URL_EDITIONS") + replace(edition.getId().toUpperCase(), false) + "#online";
    else
        urlEditionChecker = getString("URL_EDITIONS") + replace(edition.getId().toUpperCase(), false) + "#" + getString("FORMAT");
    logger.debug("Parsing dashboard " + urlEditionChecker);
    Document doc = read(urlEditionChecker);
    Element table = null;
    try {
        table = doc.select(MTGConstants.HTML_TAG_TABLE).get(1).getElementsByTag(MTGConstants.HTML_TAG_TBODY).get(0);
        for (Element e : table.getElementsByTag(MTGConstants.HTML_TAG_TR)) {
            CardShake cs = new CardShake();
            cs.setName(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(0).text().replaceAll("\\(RL\\)", "").trim());
            cs.setImg(new URL("http://" + e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(0).getElementsByTag("a").get(0).attr("data-full-image")));
            cs.setPrice(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(3).text()));
            cs.setPriceDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(4).text()));
            cs.setPercentDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(5).text()));
            cs.setPriceWeekChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(6).text()));
            cs.setPercentWeekChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(7).text()));
            cs.setEd(oldID);
            cs.setDateUpdate(new Date());
            list.add(cs);
        }
    } catch (IndexOutOfBoundsException e) {
        logger.error(e);
    }
    return list;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) CardShake(org.magic.api.beans.CardShake) URL(java.net.URL) Date(java.util.Date)

Aggregations

CardShake (org.magic.api.beans.CardShake)9 ArrayList (java.util.ArrayList)8 JsonElement (com.google.gson.JsonElement)4 IOException (java.io.IOException)4 Document (org.jsoup.nodes.Document)4 Element (org.jsoup.nodes.Element)4 JsonArray (com.google.gson.JsonArray)3 JsonObject (com.google.gson.JsonObject)3 URL (java.net.URL)3 ParseException (java.text.ParseException)3 JsonParser (com.google.gson.JsonParser)2 JsonReader (com.google.gson.stream.JsonReader)2 InputStreamReader (java.io.InputStreamReader)2 HttpURLConnection (java.net.HttpURLConnection)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 MagicCard (org.magic.api.beans.MagicCard)2 MagicEdition (org.magic.api.beans.MagicEdition)2