Search in sources :

Example 26 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class GayCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url", endless = true, acceptEmpty = true) @ImageUrl String imageUrl) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("gay")).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
}
Also used : ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 27 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class HotCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url", endless = true, acceptEmpty = true) @ImageUrl String imageUrl) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("hot")).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
}
Also used : ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 28 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class ResizeCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url") @ImageUrl String imageUrl, @Argument(value = "width") @Limit(min = 0, max = 5000) double width, @Argument(value = "height") @Limit(min = 0, max = 5000) @DefaultNumber(1) double height) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("resize")).addQuery("w", width).addQuery("h", height).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        ImageUtility.getImageMessage(event, response, (body, error) -> {
            if (error == ImageError.INVALID_QUERY_VALUE) {
                return event.replyFailure(body.getString("message"));
            }
            return null;
        }).queue();
    });
}
Also used : Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) ImageError(com.sx4.bot.entities.image.ImageError) Permission(net.dv8tion.jda.api.Permission) ModuleCategory(com.sx4.bot.category.ModuleCategory) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 29 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class SkinPortManager method retrieveCSRFData.

public void retrieveCSRFData() {
    Request request = new Request.Builder().url("https://skinport.com/api/data").build();
    this.bot.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        List<String> cookies = response.headers("Set-Cookie");
        String cookie = cookies.get(0);
        this.cookie = cookie.substring(0, cookie.indexOf(';'));
        Document data = Document.parse(response.body().string());
        this.csrf = data.getString("csrf");
        this.currency = data.getString("currency");
        this.currencies = data.get("rates", Document.class);
    });
}
Also used : Document(org.bson.Document) TimeUnit(java.util.concurrent.TimeUnit) Request(okhttp3.Request) List(java.util.List) Sx4(com.sx4.bot.core.Sx4) HttpCallback(com.sx4.bot.http.HttpCallback) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Executors(java.util.concurrent.Executors) Request(okhttp3.Request) List(java.util.List) Document(org.bson.Document)

Example 30 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class FreeGameManager method ensureSteamFreeGames.

public void ensureSteamFreeGames() {
    this.steamExecutor.scheduleAtFixedRate(() -> {
        Request resultRequest = new Request.Builder().url("https://store.steampowered.com/search/?maxprice=free&specials=1&cc=gb").addHeader("Accept-Language", "en").build();
        this.bot.getHttpClient().newCall(resultRequest).enqueue((HttpCallback) resultResponse -> {
            org.jsoup.nodes.Document resultsDocument = Jsoup.parse(resultResponse.body().string());
            Element results = resultsDocument.getElementById("search_resultsRows");
            if (results == null) {
                return;
            }
            List<CompletableFuture<SteamFreeGame>> futures = new ArrayList<>();
            for (Element result : results.children()) {
                Element discount = result.getElementsByClass("col search_discount responsive_secondrow").first();
                if (!discount.text().equals("-100%")) {
                    continue;
                }
                int id = Integer.parseInt(result.attr("data-ds-appid"));
                Request gameRequest = new Request.Builder().url("https://store.steampowered.com/app/" + id + "?cc=gb").addHeader("Accept-Language", "en").build();
                CompletableFuture<SteamFreeGame> future = new CompletableFuture<>();
                this.bot.getHttpClient().newCall(gameRequest).enqueue((HttpCallback) gameResponse -> {
                    org.jsoup.nodes.Document document = Jsoup.parse(gameResponse.body().string());
                    Element content = document.getElementsByClass("page_content_ctn").first();
                    SteamFreeGame game = SteamFreeGame.fromData(id, content);
                    if (this.isAnnounced(game)) {
                        future.complete(null);
                        return;
                    }
                    future.complete(game);
                });
                futures.add(future);
            }
            FutureUtility.allOf(futures, Objects::nonNull).whenComplete((games, exception) -> {
                if (ExceptionUtility.sendErrorMessage(exception) || games.isEmpty()) {
                    return;
                }
                this.sendFreeGameNotifications(games).whenComplete(MongoDatabase.exceptionally());
            });
        });
    }, 0, 30, TimeUnit.MINUTES);
}
Also used : Document(org.bson.Document) java.util(java.util) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame) TextChannel(net.dv8tion.jda.api.entities.TextChannel) FreeGame(com.sx4.bot.entities.info.FreeGame) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) Formatter(com.sx4.bot.formatter.Formatter) Sx4(com.sx4.bot.core.Sx4) SteamFreeGame(com.sx4.bot.entities.info.SteamFreeGame) Element(org.jsoup.nodes.Element) Duration(java.time.Duration) FreeGameUtility(com.sx4.bot.utility.FreeGameUtility) com.mongodb.client.model(com.mongodb.client.model) FutureUtility(com.sx4.bot.utility.FutureUtility) ZoneOffset(java.time.ZoneOffset) FreeGameType(com.sx4.bot.entities.info.FreeGameType) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) Operators(com.sx4.bot.database.mongo.model.Operators) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) MessageUtility(com.sx4.bot.utility.MessageUtility) Collectors(java.util.stream.Collectors) HttpException(club.minnced.discord.webhook.exception.HttpException) OkHttpClient(okhttp3.OkHttpClient) OffsetDateTime(java.time.OffsetDateTime) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Jsoup(org.jsoup.Jsoup) SteamFreeGame(com.sx4.bot.entities.info.SteamFreeGame) Element(org.jsoup.nodes.Element) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Document(org.bson.Document)

Aggregations

HttpCallback (com.sx4.bot.http.HttpCallback)73 Request (okhttp3.Request)71 ModuleCategory (com.sx4.bot.category.ModuleCategory)62 Sx4Command (com.sx4.bot.core.Sx4Command)62 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)62 Argument (com.jockie.bot.core.argument.Argument)57 Permission (net.dv8tion.jda.api.Permission)57 Document (org.bson.Document)40 ImageRequest (com.sx4.bot.entities.image.ImageRequest)35 ImageUtility (com.sx4.bot.utility.ImageUtility)33 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)27 ImageUrl (com.sx4.bot.annotations.argument.ImageUrl)24 PagedResult (com.sx4.bot.paged.PagedResult)18 StandardCharsets (java.nio.charset.StandardCharsets)17 URLEncoder (java.net.URLEncoder)16 List (java.util.List)16 Option (com.jockie.bot.core.option.Option)13 OffsetDateTime (java.time.OffsetDateTime)11 ZoneOffset (java.time.ZoneOffset)11 Limit (com.sx4.bot.annotations.argument.Limit)10