Search in sources :

Example 1 with MTGPricesProvider

use of org.magic.api.interfaces.MTGPricesProvider 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 2 with MTGPricesProvider

use of org.magic.api.interfaces.MTGPricesProvider 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 3 with MTGPricesProvider

use of org.magic.api.interfaces.MTGPricesProvider 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 4 with MTGPricesProvider

use of org.magic.api.interfaces.MTGPricesProvider 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)4 MTGPricesProvider (org.magic.api.interfaces.MTGPricesProvider)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 FileWriter (java.io.FileWriter)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimerTask (java.util.TimerTask)1 MagicCard (org.magic.api.beans.MagicCard)1 MagicCardAlert (org.magic.api.beans.MagicCardAlert)1 MagicCollection (org.magic.api.beans.MagicCollection)1 MagicEdition (org.magic.api.beans.MagicEdition)1 Request (spark.Request)1 Response (spark.Response)1