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;
}
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;
}
}
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;
}
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;
}
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;
}
Aggregations