use of 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;
}
}
use of 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;
}
}
use of 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();
}
}
use of 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();
}
}
use of 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();
}
}
Aggregations