Search in sources :

Example 16 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project legendarybot by greatman.

the class LegendaryCheckPlugin method isItemLegendary.

/**
 * Check if an item is a legendary. Checks in the ES cache if we have the item. If not, we retrieve the information from Battle.Net API and cache it.
 * @param regionName The region to check the item in.
 * @param itemID The Item ID to check.
 * @return True if the item is a legendary. Else false.
 */
public boolean isItemLegendary(String regionName, long itemID) {
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new BattleNetAPIInterceptor(getBot())).build();
    try {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("q", "id:" + itemID);
        Response response = getBot().getElasticSearch().performRequest("GET", "/wow/item/_search", paramMap);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONObject hits = (JSONObject) obj.get("hits");
            if ((long) hits.get("total") == 0) {
                HttpUrl url = new HttpUrl.Builder().scheme("https").host(regionName + ".api.battle.net").addPathSegments("/wow/item/" + itemID).build();
                Request webRequest = new Request.Builder().url(url).build();
                okhttp3.Response responseBattleNet = client.newCall(webRequest).execute();
                String itemRequest = responseBattleNet.body().string();
                responseBattleNet.close();
                if (itemRequest == null) {
                    return false;
                }
                JSONObject itemObject;
                try {
                    itemObject = (JSONObject) new JSONParser().parse(itemRequest);
                } catch (ParseException e) {
                    getBot().getStacktraceHandler().sendStacktrace(e, "itemID:" + itemID, "regionName:" + regionName, "itemRequest:" + itemRequest);
                    return false;
                }
                if (itemObject.containsKey("reason")) {
                    return false;
                }
                HttpEntity entity = new NStringEntity(itemObject.toJSONString(), ContentType.APPLICATION_JSON);
                Response indexResponse = getBot().getElasticSearch().performRequest("POST", "/wow/item/", Collections.emptyMap(), entity);
                long quality = (Long) itemObject.get("quality");
                return quality == 5;
            }
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            JSONObject source = (JSONObject) firstItem.get("_source");
            return (long) source.get("quality") == 5;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e);
        return false;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) JSONObject(org.json.simple.JSONObject) BattleNetAPIInterceptor(com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 17 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project legendarybot by greatman.

the class LegendaryCheckPlugin method getItemName.

/**
 * Retrieve the item name. Checks in the ES cache if we have the item. If not, we retrieve the information from Battle.Net API and cache it.
 * @param regionName The region to check in.
 * @param itemID The item ID.
 * @return The name of the item. Else null if not found.
 */
public String getItemName(String regionName, long itemID) {
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new BattleNetAPIInterceptor(getBot())).build();
    try {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("q", "id:" + itemID);
        Response response = getBot().getElasticSearch().performRequest("GET", "/wow/item/_search", paramMap);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONObject hits = (JSONObject) obj.get("hits");
            if ((long) hits.get("total") == 0) {
                HttpUrl url = new HttpUrl.Builder().scheme("https").host(regionName + ".api.battle.net").addPathSegments("/wow/item/" + itemID).build();
                Request webRequest = new Request.Builder().url(url).build();
                okhttp3.Response responseBattleNet = client.newCall(webRequest).execute();
                String itemRequest = responseBattleNet.body().string();
                responseBattleNet.close();
                if (itemRequest == null) {
                    return null;
                }
                JSONObject itemObject;
                try {
                    itemObject = (JSONObject) new JSONParser().parse(itemRequest);
                } catch (ParseException e) {
                    getBot().getStacktraceHandler().sendStacktrace(e, "itemID:" + itemID, "regionName:" + regionName, "itemRequest:" + itemRequest);
                    return null;
                }
                return (String) itemObject.get("name");
            }
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            JSONObject source = (JSONObject) firstItem.get("_source");
            return (String) source.get("name");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e);
        return null;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(org.elasticsearch.client.Response) JSONObject(org.json.simple.JSONObject) BattleNetAPIInterceptor(com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 18 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project legendarybot by greatman.

the class LookupAchievementCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    String query = String.join(" ", args);
    try {
        HttpEntity entity = new NStringEntity("{ \"query\": { \"match\" : { \"title\" : \"" + query + "\" } } }", ContentType.APPLICATION_JSON);
        Response response = plugin.getBot().getElasticSearch().performRequest("POST", "/wow/achievement/_search", Collections.emptyMap(), entity);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            event.getChannel().sendMessage("http://www.wowhead.com/achievement=" + firstItem.get("_id")).queue();
        } catch (ParseException e) {
            e.printStackTrace();
            event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "command.lookupachievement.notfound")).queue();
        }
    } catch (IOException e) {
        e.printStackTrace();
        plugin.getBot().getStacktraceHandler().sendStacktrace(e, "query:" + query);
        event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "error.occurred.try.again.later")).queue();
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 19 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project legendarybot by greatman.

the class LookupItemCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    String query = String.join(" ", args);
    try {
        HttpEntity entity = new NStringEntity("{ \"query\": { \"match\" : { \"name\" : \"" + query + "\" } } }", ContentType.APPLICATION_JSON);
        Response response = plugin.getBot().getElasticSearch().performRequest("POST", "/wow/item/_search", Collections.emptyMap(), entity);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            event.getChannel().sendMessage("http://www.wowhead.com/item=" + firstItem.get("_id")).queue();
        } catch (ParseException e) {
            e.printStackTrace();
            event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "command.lookupitem.notfound")).queue();
        }
    } catch (IOException e) {
        e.printStackTrace();
        plugin.getBot().getStacktraceHandler().sendStacktrace(e, "query:" + query);
        event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "error.occurred.try.again.later")).queue();
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 20 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project legendarybot by greatman.

the class LookupQuestCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    String query = String.join(" ", args);
    try {
        HttpEntity entity = new NStringEntity("{ \"query\": { \"match\" : { \"title\" : \"" + query + "\" } } }", ContentType.APPLICATION_JSON);
        Response response = plugin.getBot().getElasticSearch().performRequest("POST", "/wow/quest/_search", Collections.emptyMap(), entity);
        JSONParser jsonParser = new JSONParser();
        try {
            JSONObject obj = (JSONObject) jsonParser.parse(EntityUtils.toString(response.getEntity()));
            JSONArray hit = (JSONArray) ((JSONObject) obj.get("hits")).get("hits");
            JSONObject firstItem = (JSONObject) hit.get(0);
            event.getChannel().sendMessage("http://www.wowhead.com/quest=" + firstItem.get("_id")).queue();
        } catch (ParseException e) {
            e.printStackTrace();
            event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "command.lookupquest.notfound")).queue();
        }
    } catch (IOException e) {
        e.printStackTrace();
        plugin.getBot().getStacktraceHandler().sendStacktrace(e, "query:" + query);
        event.getChannel().sendMessage(plugin.getBot().getTranslateManager().translate(event.getGuild(), "error.occurred.try.again.later")).queue();
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Aggregations

Response (org.elasticsearch.client.Response)111 Request (org.elasticsearch.client.Request)38 IOException (java.io.IOException)33 HttpEntity (org.apache.http.HttpEntity)24 NStringEntity (org.apache.http.nio.entity.NStringEntity)21 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Map (java.util.Map)14 BasicHeader (org.apache.http.message.BasicHeader)14 ResponseException (org.elasticsearch.client.ResponseException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 RestClient (org.elasticsearch.client.RestClient)11 ArrayList (java.util.ArrayList)10 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)10 StringEntity (org.apache.http.entity.StringEntity)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 InputStream (java.io.InputStream)8 JSONObject (org.json.simple.JSONObject)8 MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7