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