Search in sources :

Example 1 with HttpException

use of net.dv8tion.jda.api.exceptions.HttpException in project Sx4 by sx4-discord-bot.

the class GoogleSearchCache method retrieveResultsByQuery.

public CompletableFuture<List<GoogleSearchResult>> retrieveResultsByQuery(String query, boolean imageSearch, boolean includeNSFW) {
    CompletableFuture<List<GoogleSearchResult>> future = new CompletableFuture<>();
    Map<Boolean, Map<String, List<GoogleSearchResult>>> cache = imageSearch ? this.imageCache : this.cache;
    Map<String, List<GoogleSearchResult>> actualCache = cache.get(includeNSFW);
    if (actualCache != null && actualCache.containsKey(query)) {
        future.complete(actualCache.get(query));
    } else {
        Request request = new Request.Builder().url("https://www.googleapis.com/customsearch/v1?key=" + this.bot.getConfig().getGoogle() + "&cx=014023765838117903829:mm334tqd3kg" + (imageSearch ? "&searchType=image" : "") + "&safe=" + (includeNSFW ? "off" : "active") + "&q=" + query).build();
        this.bot.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
            Document json = Document.parse(response.body().string());
            if (json.containsKey("error")) {
                Document error = json.get("error", Document.class);
                int code = error.getInteger("code");
                if (code == 429) {
                    future.completeExceptionally(new ForbiddenException("Daily quota reached (100)"));
                } else {
                    future.completeExceptionally(new HttpException(error.get("message", "Unknown error occurred with status " + code)));
                }
                return;
            }
            List<Document> items = json.getList("items", Document.class, Collections.emptyList());
            List<GoogleSearchResult> results = items.stream().map(GoogleSearchResult::new).collect(Collectors.toList());
            cache.compute(includeNSFW, (key, value) -> {
                if (value == null) {
                    Map<String, List<GoogleSearchResult>> newCache = new HashMap<>();
                    newCache.put(query, results);
                    return newCache;
                } else {
                    value.put(query, results);
                    return value;
                }
            });
            future.complete(results);
        });
    }
    return future;
}
Also used : HttpException(net.dv8tion.jda.api.exceptions.HttpException) Document(org.bson.Document) Request(okhttp3.Request) List(java.util.List) ForbiddenException(javax.ws.rs.ForbiddenException) Sx4(com.sx4.bot.core.Sx4) HttpCallback(com.sx4.bot.http.HttpCallback) Map(java.util.Map) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) ForbiddenException(javax.ws.rs.ForbiddenException) Request(okhttp3.Request) Document(org.bson.Document) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) HttpException(net.dv8tion.jda.api.exceptions.HttpException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with HttpException

use of net.dv8tion.jda.api.exceptions.HttpException in project SkyBot by duncte123.

the class PerspectiveApi method checkSwearFilter.

public static float checkSwearFilter(String text, String channelId, String apiKey, ProfanityFilterType filterType, ObjectMapper mapper) {
    if (text.isEmpty()) {
        return 0f;
    }
    try {
        final JsonNode json = makeRequest(text, channelId, apiKey, filterType, mapper);
        if (json.has("error")) {
            final String error = json.get("error").get("message").asText();
            if ("Unable to detect language.".equals(error)) {
                return 0f;
            }
            throw new HttpException("Error while handling perspective api request: " + json);
        }
        final JsonNode score = json.get("attributeScores").get(filterType.getType()).get("summaryScore");
        return Float.parseFloat(score.get("value").asText());
    } catch (Exception e) {
        Sentry.captureException(e);
        e.printStackTrace();
        return 0f;
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(net.dv8tion.jda.api.exceptions.HttpException) HttpException(net.dv8tion.jda.api.exceptions.HttpException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RequestException(com.github.natanbc.reliqua.request.RequestException)

Example 3 with HttpException

use of net.dv8tion.jda.api.exceptions.HttpException in project SkyBot by DuncteBot.

the class PerspectiveApi method checkSwearFilter.

public static float checkSwearFilter(String text, String channelId, String apiKey, ProfanityFilterType filterType, ObjectMapper mapper) {
    if (text.isEmpty()) {
        return 0f;
    }
    try {
        final JsonNode json = makeRequest(text, channelId, apiKey, filterType, mapper);
        if (json.has("error")) {
            final String error = json.get("error").get("message").asText();
            if ("Unable to detect language.".equals(error)) {
                return 0f;
            }
            throw new HttpException("Error while handling perspective api request: " + json);
        }
        final JsonNode score = json.get("attributeScores").get(filterType.getType()).get("summaryScore");
        return Float.parseFloat(score.get("value").asText());
    } catch (Exception e) {
        Sentry.captureException(e);
        e.printStackTrace();
        return 0f;
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(net.dv8tion.jda.api.exceptions.HttpException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RequestException(com.github.natanbc.reliqua.request.RequestException) HttpException(net.dv8tion.jda.api.exceptions.HttpException)

Aggregations

HttpException (net.dv8tion.jda.api.exceptions.HttpException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 RequestException (com.github.natanbc.reliqua.request.RequestException)2 Sx4 (com.sx4.bot.core.Sx4)1 HttpCallback (com.sx4.bot.http.HttpCallback)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Collectors (java.util.stream.Collectors)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 Request (okhttp3.Request)1 Document (org.bson.Document)1