Search in sources :

Example 6 with MagicPrice

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

the class ChannelFireballPricer method getPrice.

@Override
public List<MagicPrice> getPrice(MagicEdition me, MagicCard card) throws IOException {
    String keyword = card.getName();
    String url = getString("URL");
    keyword = URLEncoder.encode(keyword, getString("ENCODING"));
    setProperty("KEYWORD", keyword);
    if (me != null)
        keyword += "&setname=" + URLEncoder.encode(me.getSet(), getString("ENCODING"));
    String link = url.replaceAll("%CARDNAME%", keyword);
    logger.info(getName() + " Looking for price " + link);
    JsonReader reader = new JsonReader(new InputStreamReader(new URL(link).openStream(), getString("ENCODING")));
    JsonElement root = new JsonParser().parse(reader);
    String value = root.getAsJsonArray().get(0).getAsString();
    MagicPrice mp = new MagicPrice();
    mp.setUrl("http://store.channelfireball.com/products/search?query=" + URLEncoder.encode(card.getName(), getString("ENCODING")));
    mp.setSite(getName());
    mp.setCurrency(value.substring(0, 1));
    mp.setValue(Double.parseDouble(value.substring(1).replaceAll(",", "")));
    ArrayList<MagicPrice> list = new ArrayList<>();
    list.add(mp);
    logger.info(getName() + " found " + list.size() + " item(s)");
    return list;
}
Also used : InputStreamReader(java.io.InputStreamReader) MagicPrice(org.magic.api.beans.MagicPrice) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) URL(java.net.URL) JsonParser(com.google.gson.JsonParser)

Example 7 with MagicPrice

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

the class PricesCheckerTimer method start.

public void start() {
    running = true;
    tache = new TimerTask() {

        public void run() {
            StringBuilder message = new StringBuilder();
            boolean notify = false;
            if (MTGControler.getInstance().getEnabledDAO().listAlerts() != null)
                for (MagicCardAlert alert : MTGControler.getInstance().getEnabledDAO().listAlerts()) {
                    alert.getOffers().clear();
                    for (MTGPricesProvider prov : MTGControler.getInstance().getEnabledPricers()) {
                        List<MagicPrice> okz = new ArrayList<>();
                        try {
                            List<MagicPrice> list = prov.getPrice(alert.getCard().getEditions().get(0), alert.getCard());
                            for (MagicPrice p : list) {
                                if (p.getValue() <= alert.getPrice() && p.getValue() > Double.parseDouble(MTGControler.getInstance().get("min-price-alert"))) {
                                    alert.getOffers().add(p);
                                    okz.add(p);
                                    logger.info("Found offer " + prov + ":" + alert.getCard() + " " + p.getValue() + p.getCurrency());
                                    notify = true;
                                }
                            }
                            prov.alertDetected(okz);
                            alert.orderDesc();
                        } catch (Exception e) {
                            MTGLogger.printStackTrace(e);
                            logger.error(e);
                        }
                    }
                    message.append(alert.getCard()).append(" : ").append(alert.getOffers().size()).append(" offers").append("\n");
                }
            if (enableNotify && notify)
                MTGControler.getInstance().notify("New offers", message.toString(), MessageType.INFO);
        }
    };
    timer.scheduleAtFixedRate(tache, 0, Long.parseLong(getString("TIMEOUT_MINUTE")) * 60000);
    logger.info("Server start with " + getString("TIMEOUT_MINUTE") + " min timeout");
}
Also used : MTGPricesProvider(org.magic.api.interfaces.MTGPricesProvider) TimerTask(java.util.TimerTask) MagicPrice(org.magic.api.beans.MagicPrice) ArrayList(java.util.ArrayList) MagicCardAlert(org.magic.api.beans.MagicCardAlert)

Example 8 with MagicPrice

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

the class MagicWebSiteGenerator method generateCardsTemplate.

private void generateCardsTemplate(MagicCard mc) throws IOException, TemplateException {
    Template cardTemplate = cfg.getTemplate("page-card.html");
    Map rootEd = new HashMap<>();
    rootEd.put("card", mc);
    rootEd.put("cols", cols);
    List<MagicPrice> prices = new ArrayList<>();
    if (!pricesProvider.isEmpty()) {
        for (MTGPricesProvider prov : pricesProvider) {
            try {
                prices.addAll(prov.getPrice(mc.getEditions().get(0), mc));
            } catch (Exception e) {
                logger.error("Generating card template for " + mc, e);
            }
        }
    }
    rootEd.put("prices", prices);
    FileWriter out = new FileWriter(Paths.get(dest, "page-card-" + mc.getId() + ".htm").toFile());
    cardTemplate.process(rootEd, out);
    setChanged();
    notifyObservers(i++);
    out.close();
}
Also used : MTGPricesProvider(org.magic.api.interfaces.MTGPricesProvider) MagicPrice(org.magic.api.beans.MagicPrice) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) TemplateException(freemarker.template.TemplateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 9 with MagicPrice

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

the class JSONHttpServer method start.

@Override
public void start() throws IOException {
    port(getInt("SERVER-PORT"));
    exception(Exception.class, new ExceptionHandler<Exception>() {

        @Override
        public void handle(Exception exception, Request req, Response res) {
            res.status(500);
            res.body("{\"error\":\"" + exception + "\"}");
        }
    });
    notFound((req, res) -> {
        res.status(404);
        return "{\"error\":\"not found\"}";
    });
    before("/*", (request, response) -> {
        response.type(getString("MIME"));
        logger.info("Received api call from " + request.ip());
    });
    get("/cards/search/:att/:val", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().searchCardByCriteria(request.params(":att"), request.params(":val"), null, false);
    }, transformer);
    post("/cards/move/:from/:to/:id", getString("MIME"), (request, response) -> {
        MagicCollection from = new MagicCollection(request.params(":from"));
        MagicCollection to = new MagicCollection(request.params(":to"));
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
        MTGControler.getInstance().getEnabledDAO().removeCard(mc, from);
        MTGControler.getInstance().getEnabledDAO().saveCard(mc, to);
        return "OK";
    }, transformer);
    get("/cards/list/:col/:idEd", getString("MIME"), (request, response) -> {
        MagicCollection col = new MagicCollection(request.params(":col"));
        MagicEdition ed = new MagicEdition();
        ed.setId(request.params(":idEd"));
        ed.setSet(request.params(":idEd"));
        return MTGControler.getInstance().getEnabledDAO().listCardsFromCollection(col, ed);
    }, transformer);
    get("/cards/:id", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
    }, transformer);
    get("/collections/list", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledDAO().getCollections();
    }, transformer);
    get("/collections/:name", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledDAO().getCollection(request.params(":name"));
    }, transformer);
    get("/editions/list", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().loadEditions();
    }, transformer);
    get("/editions/:idSet", getString("MIME"), (request, response) -> {
        return MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
    }, transformer);
    get("/editions/list/:colName", getString("MIME"), (request, response) -> {
        List<MagicEdition> eds = new ArrayList<>();
        List<String> list = MTGControler.getInstance().getEnabledDAO().getEditionsIDFromCollection(new MagicCollection(request.params(":colName")));
        for (String s : list) eds.add(MTGControler.getInstance().getEnabledProviders().getSetById(s));
        Collections.sort(eds);
        return eds;
    }, transformer);
    get("/prices/:idSet/:name", getString("MIME"), (request, response) -> {
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", request.params(":name"), null, false).get(0);
        MagicEdition ed = MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
        List<MagicPrice> pricesret = new ArrayList<>();
        for (MTGPricesProvider prices : MTGControler.getInstance().getEnabledPricers()) pricesret.addAll(prices.getPrice(ed, mc));
        return pricesret;
    }, transformer);
    if (getBoolean("ENABLE_GZIP")) {
        after((request, response) -> {
            response.header("Content-Encoding", "gzip");
        });
    }
    Spark.init();
    logger.info("Server start on port " + getString("SERVER-PORT"));
    running = true;
}
Also used : MTGPricesProvider(org.magic.api.interfaces.MTGPricesProvider) MagicPrice(org.magic.api.beans.MagicPrice) Request(spark.Request) MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Response(spark.Response) MagicCard(org.magic.api.beans.MagicCard) MagicCollection(org.magic.api.beans.MagicCollection)

Example 10 with MagicPrice

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

the class CardsPriceTableModel method addPrice.

private void addPrice(MagicCard mc, MagicEdition me) {
    for (MTGPricesProvider prov : providers) {
        try {
            if (prov.isEnable()) {
                List<MagicPrice> list = prov.getPrice(me, mc);
                if (list != null && !list.isEmpty())
                    prices.addAll(list);
                fireTableDataChanged();
            }
        } catch (Exception e) {
            logger.error("Error", e);
        }
    }
    fireTableDataChanged();
}
Also used : MTGPricesProvider(org.magic.api.interfaces.MTGPricesProvider) MagicPrice(org.magic.api.beans.MagicPrice)

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