use of com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor 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 com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor 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 com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor in project legendarybot by greatman.
the class AffixPlugin method getWeekAffixes.
public Map<Long, String> getWeekAffixes(String region) throws IOException, ParseException {
Map<Long, String> affixes = new HashMap<>();
OkHttpClient clientBattleNet = new OkHttpClient.Builder().addInterceptor(new BattleNetAPIInterceptor(getBot())).build();
HttpUrl url = new HttpUrl.Builder().scheme("https").host(region + ".api.battle.net").addPathSegments("/data/wow/mythic-challenge-mode/").addQueryParameter("namespace", "dynamic-" + region).build();
Request request = new Request.Builder().url(url).build();
String result = clientBattleNet.newCall(request).execute().body().string();
JSONParser parser = new JSONParser();
JSONObject mythicPlusDocument = (JSONObject) parser.parse(result);
if (mythicPlusDocument.containsKey("current_keystone_affixes")) {
JSONArray array = (JSONArray) mythicPlusDocument.get("current_keystone_affixes");
for (Object keystoneAffixObject : array) {
JSONObject keystoneAffixJson = (JSONObject) keystoneAffixObject;
JSONObject keystoneAffixNameObject = (JSONObject) keystoneAffixJson.get("keystone_affix");
affixes.put((long) keystoneAffixJson.get("starting_level"), (String) keystoneAffixNameObject.get("name"));
}
}
return affixes;
}
Aggregations