use of org.magic.api.beans.MagicPrice 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.MagicPrice in project MtgDesktopCompanion by nicho92.
the class MagicBazarPricer method getPrice.
@Override
public List<MagicPrice> getPrice(MagicEdition me, MagicCard card) throws IOException {
list.clear();
String url = getString("URL") + URLEncoder.encode(card.getName(), "UTF-8");
logger.info(getName() + " looking for prices " + url);
try {
doc = Jsoup.connect(url).userAgent(getString("USER_AGENT")).timeout(0).get();
Elements els = doc.select("div.filterElement");
for (int i = 0; i < els.size(); i++) {
Element e = els.get(i);
MagicPrice mp = new MagicPrice();
mp.setLanguage(e.getElementsByClass("langue").get(0).getElementsByTag("img").get(0).attr("alt"));
mp.setQuality(e.getElementsByClass("etat").html());
mp.setValue(Double.parseDouble(clean(e.select("div.prix").text())));
mp.setCurrency("EUR");
mp.setCountry("France");
mp.setSite(getName());
mp.setUrl(url);
mp.setSeller(e.getElementsByClass("edition").get(0).getElementsByIndexEquals(0).get(0).text());
mp.setFoil(!e.getElementsByClass("logo").isEmpty());
list.add(mp);
}
return list;
} catch (Exception e) {
logger.trace("Error loading price for " + url, e);
logger.info(getName() + " no item : " + e.getMessage());
return list;
}
}
use of org.magic.api.beans.MagicPrice in project MtgDesktopCompanion by nicho92.
the class MagicTradersPricer method getPrice.
public List<MagicPrice> getPrice(MagicEdition me, MagicCard card) throws IOException {
URL link = new URL(getString("URL"));
logger.info(getName() + " looking for prices " + link);
InputStream is = link.openStream();
try (BufferedReader read = new BufferedReader(new InputStreamReader(is))) {
String line;
List<MagicPrice> list = new ArrayList<>();
while ((line = read.readLine()) != null) {
String[] fields = line.split("\\|");
if (fields.length < 8)
continue;
String name = fields[0].trim();
String price = fields[1].trim();
try {
double f = Double.parseDouble(price);
String cname = getCorrectName(card.getName());
if (name.startsWith(cname)) {
logger.info(getName() + " found " + cname);
MagicPrice mp = new MagicPrice();
mp.setSeller(getName());
mp.setUrl("http://store.eudogames.com/products/search?query=" + URLEncoder.encode(card.getName(), "UTF-8"));
mp.setSite(getName());
mp.setValue(f);
mp.setCurrency("$");
list.add(mp);
return list;
}
} catch (NumberFormatException e) {
continue;
}
}
return list;
}
}
use of org.magic.api.beans.MagicPrice in project MtgDesktopCompanion by nicho92.
the class TCGPlayerPricer method getPrice.
@Override
public List<MagicPrice> getPrice(MagicEdition me, MagicCard card) throws IOException {
List<MagicPrice> list = new ArrayList<>();
String url = getString("URL");
url = url.replaceAll("%API_KEY%", getString("API_KEY"));
String set = "";
if (me == null)
set = URLEncoder.encode(card.getEditions().get(0).getSet(), getString("ENCODING"));
else
set = URLEncoder.encode(me.getSet(), getString("ENCODING"));
if (set.contains("Edition"))
set = set.replaceAll("Edition", "");
String name = card.getName();
name = name.replaceAll(" \\(.*$", "");
name = name.replaceAll("'", "%27");
name = name.replaceAll(" ", "+");
setProperty("KEYWORD", "s=" + set + "p=" + name);
String link = url.replaceAll("%SET%", set);
link = link.replaceAll("%CARTE%", name);
logger.info(getName() + " looking " + " for " + link);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
throw new IOException(e1);
}
Document doc = null;
try {
doc = dBuilder.parse(new URL(link).openStream());
logger.debug(doc);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("product");
MagicPrice mp = new MagicPrice();
mp.setCurrency("$");
mp.setSite(getName());
mp.setUrl(nodes.item(0).getChildNodes().item(11).getTextContent());
mp.setSeller(getName());
mp.setValue(Double.parseDouble(nodes.item(0).getChildNodes().item(7).getTextContent()));
list.add(mp);
logger.info(getName() + " found " + list.size() + " item(s)");
if (list.size() > Integer.parseInt(getString("MAX")) && Integer.parseInt(getString("MAX")) > -1)
return list.subList(0, Integer.parseInt(getString("MAX")));
} catch (Exception e) {
logger.error(e);
return list;
}
return list;
}
use of org.magic.api.beans.MagicPrice in project MtgDesktopCompanion by nicho92.
the class CSVExport method exportPriceCatalog.
public void exportPriceCatalog(List<MagicCard> cards, File f, MTGPricesProvider prov) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
exportedProperties = getString("exportedProperties").split(",");
exportedPricesProperties = getString("exportedPricesProperties").split(",");
for (String k : exportedProperties) bw.write(k + ";");
for (String k : exportedPricesProperties) bw.write(k + ";");
bw.write("\n");
int i = 0;
for (MagicCard mc : cards) {
for (MagicPrice prices : prov.getPrice(mc.getEditions().get(0), mc)) {
for (String k : exportedProperties) {
String val;
try {
val = BeanUtils.getProperty(mc, k);
if (val == null)
val = "";
bw.write(val.replaceAll("\n", "") + ";");
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IOException(e);
}
}
for (String p : exportedPricesProperties) {
String val;
try {
val = BeanUtils.getProperty(prices, p);
if (val == null)
val = "";
bw.write(val.replaceAll("\n", "") + ";");
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IOException(e);
}
}
bw.write("\n");
}
setChanged();
notifyObservers(i++);
}
}
}
Aggregations