Search in sources :

Example 66 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class LogCommand method execute.

@Override
public void execute(MessageReceivedEvent event, String[] args) {
    HttpUrl url = new HttpUrl.Builder().scheme("https").host("www.warcraftlogs.com").addPathSegments("/v1/reports/guild/" + getBot().getGuildSettings(event.getGuild()).getGuildName() + "/" + getBot().getGuildSettings(event.getGuild()).getWowServerName() + "/" + getBot().getGuildSettings(event.getGuild()).getRegionName()).addQueryParameter("api_key", props.getProperty("warcraftlogs.key")).build();
    Request webRequest = new Request.Builder().url(url).build();
    try {
        String request = client.newCall(webRequest).execute().body().string();
        if (request == null) {
            event.getChannel().sendMessage(getBot().getTranslateManager().translate(event.getGuild(), "command.log.guild.not.found")).queue();
            return;
        }
        try {
            // { "status": 400, "error": "Invalid guild name/server/region specified." }
            JSONParser parser = new JSONParser();
            try {
                JSONArray jsonArray = (JSONArray) parser.parse(request);
                if (jsonArray.size() == 0) {
                    event.getChannel().sendMessage(getBot().getTranslateManager().translate(event.getGuild(), "command.log.no.log.found")).queue();
                    return;
                }
                JSONObject jsonObject = (JSONObject) jsonArray.toArray()[jsonArray.size() - 1];
                Calendar calendar = Calendar.getInstance();
                // Todo, proper timezone
                calendar.setTimeZone(TimeZone.getTimeZone("America/Montreal"));
                calendar.setTimeInMillis((Long) jsonObject.get("start"));
                event.getChannel().sendMessage(getBot().getTranslateManager().translate(event.getGuild(), "command.log.message", jsonObject.get("title").toString(), jsonObject.get("owner").toString(), calendar.get(Calendar.DAY_OF_MONTH) + "", (calendar.get(Calendar.MONTH) + 1) + "", calendar.get(Calendar.YEAR) + "") + " https://www.warcraftlogs.com/reports/" + jsonObject.get("id")).queue();
            } catch (ClassCastException e) {
                event.getChannel().sendMessage(getBot().getTranslateManager().translate(event.getGuild(), "command.log.guild.not.found")).queue();
            }
        } catch (ParseException e) {
            e.printStackTrace();
            getBot().getStacktraceHandler().sendStacktrace(e, "guildId:" + event.getGuild().getId(), "guildName:" + getBot().getGuildSettings(event.getGuild()).getGuildName(), "serverName:" + getBot().getGuildSettings(event.getGuild()).getWowServerName(), "region:" + getBot().getGuildSettings(event.getGuild()).getRegionName());
        }
    } catch (IOException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e, "guildId:" + event.getGuild().getId(), "guildName:" + getBot().getGuildSettings(event.getGuild()).getGuildName(), "serverName:" + getBot().getGuildSettings(event.getGuild()).getWowServerName(), "region:" + getBot().getGuildSettings(event.getGuild()).getRegionName());
        event.getChannel().sendMessage(getBot().getTranslateManager().translate(event.getGuild(), "error.occurred.try.again.later")).queue();
    }
}
Also used : JSONObject(org.json.simple.JSONObject) Calendar(java.util.Calendar) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Example 67 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class StreamersPlugin method isStreaming.

/**
 * Checks if a username is streaming on a specific platform
 * @param username The Username of the user we want to check
 * @param platform The platform we want to check if the user is online on.
 * @return If the user is streaming, a Map containing {@link #STATUS_KEY} giving the current status of the user, {@link #GAME_KEY} containing the game the user is playing. Else a empty map.
 */
public Map<String, String> isStreaming(String username, StreamPlatform platform) {
    Map<String, String> map = new HashMap<>();
    JSONParser parser = new JSONParser();
    switch(platform) {
        case TWITCH:
            Request request = new Request.Builder().url("https://api.twitch.tv/kraken/streams/" + username).addHeader("Client-ID", props.getProperty("twitch.key")).build();
            try {
                String result = client.newCall(request).execute().body().string();
                JSONObject json = (JSONObject) parser.parse(result);
                JSONObject stream = (JSONObject) json.get("stream");
                if (stream != null) {
                    map.put(STATUS_KEY, (String) ((JSONObject) stream.get("channel")).get("status"));
                    map.put(GAME_KEY, (String) stream.get("game"));
                    map.put("created_at", (String) stream.get("created_at"));
                }
            } catch (ParseException | IOException e) {
                e.printStackTrace();
                getBot().getStacktraceHandler().sendStacktrace(e, "username:" + username, "platform:" + platform);
            }
            break;
        case MIXER:
            request = new Request.Builder().url("https://mixer.com/api/v1/channels/" + username).build();
            try {
                String result = client.newCall(request).execute().body().string();
                JSONObject json = (JSONObject) parser.parse(result);
                if (json.containsKey("online") && (boolean) json.get("online")) {
                    JSONObject stream = (JSONObject) json.get("type");
                    map.put(STATUS_KEY, (String) json.get("name"));
                    map.put(GAME_KEY, (String) stream.get("name"));
                }
            } catch (ParseException | IOException e) {
                e.printStackTrace();
                getBot().getStacktraceHandler().sendStacktrace(e, "username:" + username, "platform:" + platform);
            }
            break;
        default:
            break;
    }
    return map;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) Request(okhttp3.Request) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 68 with ParseException

use of org.json.simple.parser.ParseException in project legendarybot by greatman.

the class WoWLinkPlugin method getWoWRank.

/**
 * Get the Discord rank linked to a WoW guild rank.
 * @param guild The guild to get the setting from.
 * @param character The character to get the rank from.
 * @return The Discord rank of a character.
 */
public String getWoWRank(Guild guild, String character) {
    int[] rank = new int[1];
    rank[0] = -1;
    String rankDiscord = null;
    HttpUrl url = new HttpUrl.Builder().scheme("https").host(getBot().getGuildSettings(guild).getRegionName() + ".api.battle.net").addPathSegments("/wow/guild/" + getBot().getGuildSettings(guild).getWowServerName() + "/" + getBot().getGuildSettings(guild).getGuildName()).addQueryParameter("fields", "members").build();
    Request request = new Request.Builder().url(url).build();
    try {
        String result = client.newCall(request).execute().body().string();
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = (JSONObject) parser.parse(result);
        if (!jsonObject.containsKey("status")) {
            JSONArray membersArray = (JSONArray) jsonObject.get("members");
            for (Object e : membersArray) {
                JSONObject entry = (JSONObject) e;
                JSONObject characterEntry = (JSONObject) entry.get("character");
                if (characterEntry.get("name").equals(character)) {
                    rank[0] = Long.valueOf((long) entry.get("rank")).intValue();
                    break;
                }
            }
        }
        // We found a rank. Translate it.
        if (rank[0] != -1) {
            rankDiscord = getBot().getGuildSettings(guild).getSetting(SETTING_RANK_PREFIX + rank[0]);
        }
    } catch (IOException | ParseException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e);
    }
    return rankDiscord;
}
Also used : ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) Request(okhttp3.Request) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException)

Example 69 with ParseException

use of org.json.simple.parser.ParseException 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 70 with ParseException

use of org.json.simple.parser.ParseException 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)

Aggregations

ParseException (org.json.simple.parser.ParseException)259 JSONObject (org.json.simple.JSONObject)193 JSONParser (org.json.simple.parser.JSONParser)186 JSONArray (org.json.simple.JSONArray)84 IOException (java.io.IOException)72 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)34 Map (java.util.Map)23 HashSet (java.util.HashSet)18 API (org.wso2.carbon.apimgt.api.model.API)18 BufferedReader (java.io.BufferedReader)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)13 List (java.util.List)12 File (java.io.File)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 InputStreamReader (java.io.InputStreamReader)10 URL (java.net.URL)10