Search in sources :

Example 11 with MagicPrice

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;
}
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 12 with MagicPrice

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;
    }
}
Also used : MagicPrice(org.magic.api.beans.MagicPrice) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) IOException(java.io.IOException)

Example 13 with MagicPrice

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;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MagicPrice(org.magic.api.beans.MagicPrice) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 14 with MagicPrice

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MagicPrice(org.magic.api.beans.MagicPrice) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 15 with MagicPrice

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++);
        }
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) MagicPrice(org.magic.api.beans.MagicPrice) FileWriter(java.io.FileWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BufferedWriter(java.io.BufferedWriter)

Aggregations

MagicPrice (org.magic.api.beans.MagicPrice)17 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 URL (java.net.URL)5 InputStreamReader (java.io.InputStreamReader)4 MTGPricesProvider (org.magic.api.interfaces.MTGPricesProvider)4 JsonElement (com.google.gson.JsonElement)3 JsonReader (com.google.gson.stream.JsonReader)3 Elements (org.jsoup.select.Elements)3 JsonArray (com.google.gson.JsonArray)2 JsonParser (com.google.gson.JsonParser)2 FileWriter (java.io.FileWriter)2 InputStream (java.io.InputStream)2 MkmException (org.api.mkm.exceptions.MkmException)2 Article (org.api.mkm.modele.Article)2 Element (org.jsoup.nodes.Element)2 MagicCard (org.magic.api.beans.MagicCard)2 MagicEdition (org.magic.api.beans.MagicEdition)2 JsonObject (com.google.gson.JsonObject)1 Template (freemarker.template.Template)1