use of org.json.simple.parser.ParseException 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();
}
}
use of org.json.simple.parser.ParseException in project legendarybot by greatman.
the class ServerCommand method getServerStatus.
/**
* Retrieve the server status of a World of Warcraft realm
* The Map returned will have the following values:
* name -> Realm Name
* status -> Online/Offline
* queue -> Yes/No
* population -> The population of the Realm (Low/Medium/High/Full)
* @param region The Region the server is hosted in.
* @param serverName The server name
* @return A {@link Map} containing the values above if it is found. Else an empty map.
*/
public Map<String, String> getServerStatus(String region, String serverName) throws IOException {
Map<String, String> map = new HashMap<>();
HttpUrl url = new HttpUrl.Builder().scheme("https").host(region + ".api.battle.net").addPathSegments("/wow/realm/status").addQueryParameter("realms", serverName).build();
Request request = new Request.Builder().url(url).build();
String result = client.newCall(request).execute().body().string();
try {
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(result);
JSONArray realms = (JSONArray) object.get("realms");
for (Object realmObject : realms) {
JSONObject realm = (JSONObject) realmObject;
map.put("population", (String) realm.get("population"));
map.put("queue", (Boolean) realm.get("queue") ? "Yes" : "No");
map.put("status", (Boolean) realm.get("status") ? "Online" : "Offline");
map.put("name", realm.get("name").toString());
map.put("region", region);
}
} catch (ParseException e) {
e.printStackTrace();
getBot().getStacktraceHandler().sendStacktrace(e, "region:" + region, "serverName:" + serverName);
}
return map;
}
use of org.json.simple.parser.ParseException in project legendarybot by greatman.
the class GifCommand method execute.
@Override
public void execute(MessageReceivedEvent event, String[] args) {
StringBuilder builder = new StringBuilder();
for (String s : args) {
if (builder.length() == 0) {
builder.append(s);
} else {
builder.append(" ").append(s);
}
}
HttpUrl url = new HttpUrl.Builder().scheme("https").host("rightgif.com").addPathSegments("search/web").build();
FormBody body = new FormBody.Builder().add("text", builder.toString()).build();
Request request = new Request.Builder().url(url).post(body).build();
try {
String result = client.newCall(request).execute().body().string();
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(result);
event.getChannel().sendMessage(object.get("url").toString()).queue();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
use of org.json.simple.parser.ParseException in project storm by apache.
the class RuncLibContainerManager method getContainerIdFromOciJson.
private String getContainerIdFromOciJson(String workerId) throws IOException {
String ociJson = ConfigUtils.workerRoot(conf, workerId) + FILE_SEPARATOR + OCI_CONFIG_JSON;
LOG.info("port unknown for workerId {}, looking up from {}", workerId, ociJson);
JSONParser parser = new JSONParser();
try (Reader reader = new FileReader(ociJson)) {
JSONObject jsonObject = (JSONObject) parser.parse(reader);
return (String) jsonObject.get("containerId");
} catch (ParseException e) {
throw new IOException("Unable to parse {}", e);
}
}
use of org.json.simple.parser.ParseException in project storm by apache.
the class ResourceUtils method getJsonWithUpdatedResources.
public static String getJsonWithUpdatedResources(String jsonConf, Map<String, Double> resourceUpdates) {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(jsonConf);
JSONObject jsonObject = (JSONObject) obj;
Map<String, Double> componentResourceMap = (Map<String, Double>) jsonObject.getOrDefault(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, new HashMap<String, Double>());
for (Map.Entry<String, Double> resourceUpdateEntry : resourceUpdates.entrySet()) {
if (NormalizedResources.RESOURCE_NAME_NORMALIZER.getResourceNameMapping().containsValue(resourceUpdateEntry.getKey())) {
// if there will be legacy values they will be in the outer conf
jsonObject.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
componentResourceMap.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
}
componentResourceMap.put(resourceUpdateEntry.getKey(), resourceUpdateEntry.getValue());
}
jsonObject.put(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, componentResourceMap);
return jsonObject.toJSONString();
} catch (ParseException ex) {
throw new RuntimeException("Failed to parse component resources with json: " + jsonConf);
}
}
Aggregations