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;
}
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;
}
}
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;
}
}
Aggregations