Search in sources :

Example 1 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class CommandJoke method execute.

@Override
protected Message execute(CommandManager.ParsedCommandInvocation parsedCommandInvocation, UserPermissions userPermissions) {
    HttpRequestBuilder request = new HttpRequestBuilder("https://icanhazdadjoke.com/", RequestType.GET);
    RequestHeader header = new RequestHeader();
    header.addField("Accept", "application/json");
    header.addField("User-Agent", "RubiconBot (https://github.com/Rubicon-Bot/Rubicon)");
    request.setRequestHeader(header);
    try {
        RequestResponse response = request.sendRequest();
        JSONObject json = (JSONObject) new JSONParser().parse(response.getResponseMessage());
        SafeMessage.sendMessage(parsedCommandInvocation.getTextChannel(), new EmbedBuilder().setTitle("Joke").setDescription((String) json.get("joke")).setColor(Colors.COLOR_SECONDARY).build());
    } catch (Exception e) {
        e.printStackTrace();
        SafeMessage.sendMessage(parsedCommandInvocation.getTextChannel(), EmbedUtil.error("Error 404", "No joke was found!").build(), 30);
    }
    return null;
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) JSONObject(org.json.simple.JSONObject) RequestHeader(de.foryasee.httprequest.RequestHeader) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse)

Example 2 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class CoinhiveManager method withdrawUser.

public static void withdrawUser(CoinhiveUser coinhiveUser, int amount) {
    try {
        HttpRequestBuilder balanceRequest = new HttpRequestBuilder(BASE_URL + "/user/withdraw", RequestType.POST);
        balanceRequest.addParameter("name", coinhiveUser.getName());
        balanceRequest.addParameter("amount", String.valueOf(amount));
        RequestResponse balanceResponse = balanceRequest.sendRequest();
        JSONObject balanceObj = (JSONObject) new JSONParser().parse(balanceResponse.getResponseMessage());
        if (balanceObj.get("success").equals("false")) {
            Logger.error((String) balanceObj.get("error"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse)

Example 3 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class CoinhiveManager method getCoinhiveUser.

public static CoinhiveUser getCoinhiveUser(User user) {
    try {
        HttpRequestBuilder balanceRequest = new HttpRequestBuilder(BASE_URL + "/user/balance", RequestType.GET);
        balanceRequest.addParameter("name", user.getId());
        RequestResponse balanceResponse = balanceRequest.sendRequest();
        JSONObject balanceObj = (JSONObject) new JSONParser().parse(balanceResponse.getResponseMessage());
        if (balanceObj.get("success").equals("false") || balanceObj.get("balance") == null) {
            return new CoinhiveUser() {

                @Override
                public String getName() {
                    return "Invalid user.";
                }

                @Override
                public int getTotal() {
                    return 0;
                }

                @Override
                public int getWithdrawn() {
                    return 0;
                }

                @Override
                public long getBalance() {
                    return 0;
                }
            };
        }
        return new CoinhiveUser() {

            @Override
            public String getName() {
                return (String) balanceObj.get("name");
            }

            @Override
            public int getTotal() {
                return (int) balanceObj.get("total");
            }

            @Override
            public int getWithdrawn() {
                return (int) balanceObj.get("withdrawn");
            }

            @Override
            public long getBalance() {
                return ((long) balanceObj.get("balance"));
            }
        };
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse)

Example 4 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class CommandVideo method execute.

@Override
protected Message execute(CommandManager.ParsedCommandInvocation invocation, UserPermissions userPermissions) {
    String[] args = invocation.getArgs();
    if (args.length < 1)
        createHelpMessage(invocation);
    String query = invocation.getMessage().getContentDisplay().replace(invocation.getPrefix() + invocation.getCommandInvocation(), "");
    HttpRequestBuilder request = new HttpRequestBuilder("https://www.googleapis.com/youtube/v3/search", RequestType.GET);
    request.addParameter("type", "video");
    request.addParameter("q", query);
    request.addParameter("part", "snippet");
    request.addParameter("key", Info.GOOGLE_TOKEN);
    try {
        RequestResponse response = request.sendRequest();
        JSONObject json = (JSONObject) new JSONParser().parse(response.getResponseMessage());
        JSONArray data = (JSONArray) json.get("items");
        JSONObject snippet = (JSONObject) ((JSONObject) data.get(0)).get("snippet");
        JSONObject id = (JSONObject) ((JSONObject) data.get(0)).get("id");
        JSONObject thumbnails = (JSONObject) snippet.get("thumbnails");
        EmbedBuilder message = new EmbedBuilder().setColor(Colors.COLOR_PRIMARY).setTitle((String) snippet.get("title"), "https://youtu.be/" + id.get("videoId")).setAuthor(invocation.getAuthor().getName(), null, invocation.getAuthor().getAvatarUrl()).setTimestamp(new Date().toInstant()).setThumbnail((String) ((JSONObject) thumbnails.get("default")).get("url")).addField("Video Description", (String) snippet.get("description"), false).addField("Channel Name", (String) snippet.get("channelTitle"), true);
        invocation.getTextChannel().sendMessage(message.build()).queue();
    } catch (Exception e) {
        e.printStackTrace();
        return EmbedUtil.message(EmbedUtil.error("Error!", "Found no Video."));
    }
    return null;
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse) Date(java.util.Date)

Example 5 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class CommandGiphy method execute.

@Override
protected Message execute(CommandManager.ParsedCommandInvocation parsedCommandInvocation, UserPermissions userPermissions) {
    String[] args = parsedCommandInvocation.getArgs();
    if (args.length < 1) {
        return createHelpMessage();
    }
    String query = parsedCommandInvocation.getMessage().getContentDisplay().replace(parsedCommandInvocation.getPrefix() + parsedCommandInvocation.getCommandInvocation(), "");
    HttpRequestBuilder request = new HttpRequestBuilder("https://api.giphy.com/v1/gifs/search", RequestType.GET);
    request.addParameter("api_key", Info.GIPHY_TOKEN);
    request.addParameter("q", query);
    try {
        RequestResponse response = request.sendRequest();
        JSONObject json = (JSONObject) new JSONParser().parse(response.getResponseMessage());
        JSONArray data = (JSONArray) json.get("data");
        parsedCommandInvocation.getTextChannel().sendMessage((String) ((JSONObject) data.get(0)).get("url")).queue();
    } catch (Exception e) {
        return EmbedUtil.message(EmbedUtil.error("Error!", "Found no gif."));
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse)

Aggregations

RequestResponse (de.foryasee.httprequest.RequestResponse)11 HttpRequestBuilder (de.foryasee.httprequest.HttpRequestBuilder)10 JSONObject (org.json.simple.JSONObject)7 JSONParser (org.json.simple.parser.JSONParser)5 ParseException (org.json.simple.parser.ParseException)4 JSONArray (org.json.simple.JSONArray)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 RequestHeader (de.foryasee.httprequest.RequestHeader)1 Date (java.util.Date)1 HTTPException (javax.xml.ws.http.HTTPException)1 JSONObject (org.json.JSONObject)1