Search in sources :

Example 1 with SteamFreeGame

use of com.sx4.bot.entities.info.SteamFreeGame 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

HttpException (club.minnced.discord.webhook.exception.HttpException)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 com.mongodb.client.model (com.mongodb.client.model)1 Sx4 (com.sx4.bot.core.Sx4)1 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)1 Operators (com.sx4.bot.database.mongo.model.Operators)1 EpicFreeGame (com.sx4.bot.entities.info.EpicFreeGame)1 FreeGame (com.sx4.bot.entities.info.FreeGame)1 FreeGameType (com.sx4.bot.entities.info.FreeGameType)1 SteamFreeGame (com.sx4.bot.entities.info.SteamFreeGame)1 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)1 WebhookClient (com.sx4.bot.entities.webhook.WebhookClient)1 Formatter (com.sx4.bot.formatter.Formatter)1 JsonFormatter (com.sx4.bot.formatter.JsonFormatter)1 HttpCallback (com.sx4.bot.http.HttpCallback)1 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)1 FreeGameUtility (com.sx4.bot.utility.FreeGameUtility)1 FutureUtility (com.sx4.bot.utility.FutureUtility)1 MessageUtility (com.sx4.bot.utility.MessageUtility)1 Duration (java.time.Duration)1