Search in sources :

Example 61 with MagicEdition

use of org.magic.api.beans.MagicEdition 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)

Example 62 with MagicEdition

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

the class MTGPricePricer method getPrice.

@Override
public List<MagicPrice> getPrice(MagicEdition ed, MagicCard card) throws IOException {
    if (getString("API_KEY").equals(""))
        throw new NullPointerException("API_KEY must be filled");
    MagicEdition selected = ed;
    if (ed == null)
        selected = card.getEditions().get(0);
    String set = selected.getSet().replaceAll(" ", "_");
    String url = getString("WS_URL") + "?apiKey=" + getString("API_KEY") + "&s=" + set;
    InputStream stream = new URL(url).openConnection().getInputStream();
    List<MagicPrice> ret = new ArrayList<>();
    logger.info(getName() + " looking for price at " + url);
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(stream));
        reader.setLenient(true);
        reader.beginObject();
        reader.nextName();
        reader.beginArray();
        String name = "";
        String fairPrice = "";
        String mtgpriceID = "";
        while (reader.hasNext()) {
            reader.beginObject();
            reader.nextName();
            mtgpriceID = (reader.nextString());
            reader.nextName();
            name = (reader.nextString());
            reader.nextName();
            fairPrice = (reader.nextString());
            reader.endObject();
            if (name.equalsIgnoreCase(card.getName())) {
                MagicPrice price = new MagicPrice();
                price.setCurrency("$");
                price.setSeller("MTGPrice");
                price.setUrl(getString("WEBSITE") + "/sets/" + set + "/" + mtgpriceID.substring(0, mtgpriceID.indexOf(set)));
                price.setValue(Double.parseDouble(fairPrice.replaceAll("\\$", "")));
                price.setQuality("NM");
                price.setFoil(mtgpriceID.substring(mtgpriceID.indexOf(set) + set.length()).startsWith("true"));
                price.setSite(getName());
                ret.add(price);
                reader.close();
                logger.info(getName() + " found " + ret.size() + " items");
                return ret;
            }
        }
        reader.close();
    } catch (Exception e) {
        return ret;
    }
    return ret;
}
Also used : MagicPrice(org.magic.api.beans.MagicPrice) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) URL(java.net.URL) IOException(java.io.IOException)

Example 63 with MagicEdition

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

the class DeckbrewProvider method getSetById.

public MagicEdition getSetById(String id) throws IOException {
    String url = urldeckbrewJSON + "/sets/" + id;
    Reader reader = new InputStreamReader(new URL(url).openStream(), encoding);
    JsonObject root = new JsonParser().parse(reader).getAsJsonObject();
    MagicEdition ed = new MagicEdition();
    ed.setId(root.get("id").getAsString());
    ed.setBorder(root.get("border").getAsString());
    ed.setSet(root.get("name").getAsString());
    ed.setType(root.get("type").getAsString());
    ed.setCardCount(0);
    return ed;
}
Also used : InputStreamReader(java.io.InputStreamReader) MagicEdition(org.magic.api.beans.MagicEdition) JsonReader(com.google.gson.stream.JsonReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) JsonParser(com.google.gson.JsonParser)

Example 64 with MagicEdition

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

the class MagicTheGatheringIOProvider method generateEdition.

private MagicEdition generateEdition(JsonObject obj) throws IOException {
    MagicEdition ed = new MagicEdition();
    ed.setId(obj.get("code").getAsString());
    ed.setSet(obj.get("name").getAsString());
    ed.setType(obj.get("type").getAsString());
    ed.setBorder(obj.get("border").getAsString());
    ed.setReleaseDate(obj.get("releaseDate").getAsString());
    if (obj.get("mkm_id") != null) {
        ed.setMkm_id(obj.get("mkm_id").getAsInt());
        ed.setMkm_name(obj.get("mkm_name").getAsString());
    }
    if (obj.get("magicCardsInfoCode") != null)
        ed.setMagicCardsInfoCode(obj.get("magicCardsInfoCode").getAsString());
    if (propsCache.getProperty(ed.getId()) != null)
        ed.setCardCount(Integer.parseInt(propsCache.getProperty(ed.getId())));
    else
        ed.setCardCount(getCount(ed.getId()));
    return ed;
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition)

Example 65 with MagicEdition

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

the class TappedOutDeckSniffer method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    HttpGet get = new HttpGet(info.getUrl());
    logger.debug("sniff deck : " + info.getName() + " at " + info.getUrl());
    String responseBody = EntityUtils.toString(httpclient.execute(get, httpContext).getEntity());
    MagicDeck deck = new MagicDeck();
    deck.setDateCreation(new Date());
    JsonElement root = new JsonParser().parse(responseBody);
    deck.setName(root.getAsJsonObject().get("name").getAsString());
    deck.setDescription(root.getAsJsonObject().get("url").getAsString());
    for (int i = 0; i < root.getAsJsonObject().get("inventory").getAsJsonArray().size(); i++) {
        JsonArray inv = root.getAsJsonObject().get("inventory").getAsJsonArray().get(i).getAsJsonArray();
        String cardName = inv.get(0).getAsString();
        String position = inv.get(1).getAsJsonObject().get("b").getAsString();
        int qte = inv.get(1).getAsJsonObject().get("qty").getAsInt();
        // remove foil if present
        cardName = StringUtils.replaceAll(cardName, "\\*.+?\\*", "").trim();
        // ged ed if present
        String idSet = null;
        Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(cardName);
        while (m.find()) {
            idSet = (m.group(1));
        }
        cardName = StringUtils.replaceAll(cardName, "\\(([^)]+)\\)", "").trim();
        // remove behavior if present
        if (cardName.contains("#"))
            cardName = cardName.substring(0, cardName.indexOf('#')).trim();
        if (cardName.contains("//"))
            cardName = cardName.substring(0, cardName.indexOf("//")).trim();
        List<MagicCard> ret;
        if (idSet == 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"));
                ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true);
            } else {
                ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, null, true);
            }
        } else {
            MagicEdition ed = new MagicEdition();
            ed.setId(idSet);
            ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true);
        }
        if (!ret.isEmpty()) {
            setChanged();
            notifyObservers(deck.getMap());
            if (position.equalsIgnoreCase("main"))
                deck.getMap().put(ret.get(0), qte);
            else
                deck.getMapSideBoard().put(ret.get(0), qte);
        }
    }
    return deck;
}
Also used : JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) Matcher(java.util.regex.Matcher) JsonElement(com.google.gson.JsonElement) HttpGet(org.apache.http.client.methods.HttpGet) MagicEdition(org.magic.api.beans.MagicEdition) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck) JsonParser(com.google.gson.JsonParser)

Aggregations

MagicEdition (org.magic.api.beans.MagicEdition)67 MagicCard (org.magic.api.beans.MagicCard)38 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)18 MagicCollection (org.magic.api.beans.MagicCollection)11 MagicDeck (org.magic.api.beans.MagicDeck)10 JsonObject (com.google.gson.JsonObject)9 InputStreamReader (java.io.InputStreamReader)9 JsonReader (com.google.gson.stream.JsonReader)8 List (java.util.List)8 JsonParser (com.google.gson.JsonParser)7 MalformedURLException (java.net.MalformedURLException)7 SQLException (java.sql.SQLException)7 Date (java.util.Date)7 JLabel (javax.swing.JLabel)7 Before (org.junit.Before)7 JsonArray (com.google.gson.JsonArray)6 BufferedImage (java.awt.image.BufferedImage)6 URL (java.net.URL)6 Map (java.util.Map)6