Search in sources :

Example 86 with MagicCard

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

the class ScryFallProvider method searchCardByCriteria.

@Override
public List<MagicCard> searchCardByCriteria(String att, String crit, MagicEdition me, boolean exact) throws IOException {
    List<MagicCard> list = new ArrayList<>();
    String comparator = crit;
    if (exact)
        comparator = "!\"" + crit + "\"";
    String url = baseURI + "/cards/";
    if (att.equals("name"))
        url += "search?q=" + URLEncoder.encode("++" + comparator + " include:extras", encoding);
    else if (att.equals("custom"))
        url += "search?q=" + URLEncoder.encode(crit, encoding);
    else if (att.equals("set"))
        url += "search?q=" + URLEncoder.encode("++e:" + crit, encoding);
    else if (att.equals("id"))
        url += URLEncoder.encode(crit, encoding);
    else
        url += "search?q=" + URLEncoder.encode(att + ":" + comparator + " include:extras", encoding);
    if (me != null)
        url += "%20" + URLEncoder.encode("e:" + me.getId(), encoding);
    HttpURLConnection con;
    JsonReader reader;
    boolean hasMore = true;
    while (hasMore) {
        logger.debug(URLDecoder.decode(url, encoding));
        con = (HttpURLConnection) getConnection(url);
        if (!isCorrectConnection(con))
            return list;
        try {
            reader = new JsonReader(new InputStreamReader(con.getInputStream(), encoding));
            JsonElement el = parser.parse(reader);
            if (att.equals("id")) {
                list.add(generateCard(el.getAsJsonObject(), exact, crit));
                hasMore = false;
            } else {
                JsonArray jsonList = el.getAsJsonObject().getAsJsonArray("data");
                for (int i = 0; i < jsonList.size(); i++) {
                    MagicCard mc = generateCard(jsonList.get(i).getAsJsonObject(), exact, crit);
                    list.add(mc);
                }
                hasMore = el.getAsJsonObject().get("has_more").getAsBoolean();
                if (hasMore)
                    url = el.getAsJsonObject().get("next_page").getAsString();
                Thread.sleep(50);
            }
        } catch (Exception e) {
            logger.error("erreur", e);
            hasMore = false;
        }
    }
    return list;
}
Also used : JsonArray(com.google.gson.JsonArray) HttpURLConnection(java.net.HttpURLConnection) MagicCard(org.magic.api.beans.MagicCard) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 87 with MagicCard

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

the class CockatriceTokenProvider method generateEmblemFor.

@Override
public MagicCard generateEmblemFor(MagicCard mc) throws IOException {
    String expression = "//card[reverse-related=\"" + mc.getName() + "\"][contains(name,'emblem')]";
    logger.debug(expression);
    try {
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
        Element value = (Element) nodeList.item(0);
        MagicCard tok = new MagicCard();
        tok.setLayout(MagicCard.LAYOUT.Emblem.toString());
        tok.setCmc(0);
        tok.setName(value.getElementsByTagName("name").item(0).getTextContent().replaceAll("\\(emblem\\)", "").trim());
        String types = value.getElementsByTagName("type").item(0).getTextContent();
        tok.getSupertypes().add(MagicCard.LAYOUT.Emblem.toString());
        tok.getSubtypes().add(types.substring(types.indexOf("\u2014") + 1));
        tok.setText(value.getElementsByTagName("text").item(0).getTextContent());
        tok.setNumber("E");
        tok.getEditions().add(mc.getEditions().get(0));
        logger.debug("Create token " + tok);
        return tok;
    } catch (XPathExpressionException e) {
        logger.error("Erreur XPath", e);
        return null;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 88 with MagicCard

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

the class DeckstatsDeckSniffer method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    // 
    MagicDeck deck = new MagicDeck();
    logger.debug("get deck " + info.getUrl());
    Document d = Jsoup.connect(info.getUrl().toString()).userAgent(getString("USER_AGENT")).timeout(getInt("TIMEOUT")).get();
    deck.setDescription(info.getUrl().toString());
    deck.setName(info.getName());
    deck.setDateCreation(new Date());
    for (Element a : d.select("a.deck_tags_list_tag")) deck.getTags().add(a.text());
    boolean side = false;
    Elements e = d.select("div#deck_overview_cards").select("div.deck_overview_section_chunk");
    for (Element block : e) {
        if (block.hasClass("deck_overview_section_chunk__sideboard"))
            side = true;
        else
            side = false;
        for (Element cont : block.select("div.deck_overview_card_header")) {
            Integer qte = Integer.parseInt(cont.getElementsByClass("deck_overview_card_amount").get(0).text());
            String cardName = cont.getElementsByClass("deck_overview_card_name").get(0).text().trim();
            if (cardName.contains("//"))
                cardName = cardName.substring(0, cardName.indexOf("//")).trim();
            MagicCard mc = null;
            if (cardName.trim().equalsIgnoreCase("Plains") || cardName.trim().equalsIgnoreCase("Island") || cardName.trim().equalsIgnoreCase("Swamp") || cardName.trim().equalsIgnoreCase("Mountain") || cardName.trim().equalsIgnoreCase("Forest")) {
                MagicEdition ed = new MagicEdition();
                ed.setId(MTGControler.getInstance().get("default-land-deck"));
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
            } else {
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, null, true).get(0);
            }
            if (side)
                deck.getMapSideBoard().put(mc, qte);
            else
                deck.getMap().put(mc, qte);
        }
    }
    if (d.select("div#deck_overview_info") != null)
        deck.setDescription(d.select("div#deck_overview_info").select("div.deck_text_editable_container").text());
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Element(org.jsoup.nodes.Element) MagicEdition(org.magic.api.beans.MagicEdition) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck)

Example 89 with MagicCard

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

the class LotusNoirDecks method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    MagicDeck deck = new MagicDeck();
    logger.debug("get deck at " + info.getUrl());
    Document d = Jsoup.connect(info.getUrl().toString()).userAgent(getString("USER_AGENT")).timeout(Integer.parseInt(getString("TIMEOUT"))).get();
    deck.setDescription(info.getUrl().toString());
    deck.setName(info.getName());
    deck.setDateCreation(new Date());
    Elements e = d.select("div.demi_page>table").select(MTGConstants.HTML_TAG_TR);
    boolean sideboard = false;
    for (Element cont : e) {
        Elements cont2 = cont.select("span.card_title_us");
        if (cont.text().startsWith("R\u00E9serve"))
            sideboard = true;
        if (cont2.text().length() > 0) {
            Integer qte = Integer.parseInt(cont2.text().substring(0, cont2.text().indexOf(' ')));
            String cardName = cont2.text().substring(cont2.text().indexOf(' '), cont2.text().length()).trim();
            if (// for transformatble cards
            cardName.contains("//"))
                cardName = cardName.substring(0, cardName.indexOf("//")).trim();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, null, true).get(0);
            if (!sideboard)
                deck.getMap().put(mc, qte);
            else
                deck.getMapSideBoard().put(mc, qte);
        }
    }
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck)

Example 90 with MagicCard

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

the class MTGoldFishDeck method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    logger.debug("sniff url : " + info.getUrl());
    MagicDeck deck = new MagicDeck();
    deck.setName(info.getName());
    deck.setDescription(info.getUrl().toString());
    deck.setDateCreation(new Date());
    Document d = Jsoup.connect(info.getUrl().toString()).userAgent(getString("USER_AGENT")).get();
    Elements e = d.select("table.deck-view-deck-table").get(0).select(MTGConstants.HTML_TAG_TR);
    boolean sideboard = false;
    for (Element tr : e) {
        if (tr.select("td.deck-header").text().contains("Sideboard"))
            sideboard = true;
        if ((tr.select("td.deck-col-qty").text() + " " + tr.select("td.deck-col-card").text()).length() > 1) {
            int qte = Integer.parseInt(tr.select("td.deck-col-qty").text());
            String cardName = tr.select("td.deck-col-card").text();
            MagicEdition ed = null;
            if (cardName.trim().equalsIgnoreCase("Plains") || cardName.trim().equalsIgnoreCase("Island") || cardName.trim().equalsIgnoreCase("Swamp") || cardName.trim().equalsIgnoreCase("Mountain") || cardName.trim().equalsIgnoreCase("Forest")) {
                ed = new MagicEdition();
                ed.setId(MTGControler.getInstance().get("default-land-deck"));
            }
            if (cardName.contains("//"))
                cardName = cardName.substring(0, cardName.indexOf("//")).trim();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
            if (!sideboard) {
                deck.getMap().put(mc, qte);
            } else {
                deck.getMapSideBoard().put(mc, qte);
            }
        }
    }
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Element(org.jsoup.nodes.Element) MagicEdition(org.magic.api.beans.MagicEdition) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck)

Aggregations

MagicCard (org.magic.api.beans.MagicCard)106 MagicEdition (org.magic.api.beans.MagicEdition)40 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)28 MagicDeck (org.magic.api.beans.MagicDeck)21 MagicCollection (org.magic.api.beans.MagicCollection)14 SQLException (java.sql.SQLException)12 Date (java.util.Date)11 MagicCardStock (org.magic.api.beans.MagicCardStock)10 DisplayableCard (org.magic.game.gui.components.DisplayableCard)10 JsonArray (com.google.gson.JsonArray)8 List (java.util.List)8 FileReader (java.io.FileReader)7 Before (org.junit.Before)7 BufferedReader (java.io.BufferedReader)6 File (java.io.File)6 FileWriter (java.io.FileWriter)6 MalformedURLException (java.net.MalformedURLException)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6