Search in sources :

Example 1 with BattleNetAPIInterceptor

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;
    }
}
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 2 with BattleNetAPIInterceptor

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;
    }
}
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 3 with BattleNetAPIInterceptor

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) HttpUrl(okhttp3.HttpUrl) JSONObject(org.json.simple.JSONObject) BattleNetAPIInterceptor(com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Aggregations

BattleNetAPIInterceptor (com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor)3 HashMap (java.util.HashMap)3 HttpUrl (okhttp3.HttpUrl)3 OkHttpClient (okhttp3.OkHttpClient)3 Request (okhttp3.Request)3 JSONArray (org.json.simple.JSONArray)3 JSONObject (org.json.simple.JSONObject)3 JSONParser (org.json.simple.parser.JSONParser)3 IOException (java.io.IOException)2 Response (org.elasticsearch.client.Response)2 ParseException (org.json.simple.parser.ParseException)2 HttpEntity (org.apache.http.HttpEntity)1 NStringEntity (org.apache.http.nio.entity.NStringEntity)1