Package com.brewtab.ircbot.util

Examples of com.brewtab.ircbot.util.URLBuilder


        }

        String tags = sb.toString();

        try {
            URLBuilder urlBuilder = new URLBuilder(baseURL);
            urlBuilder.setParameter("s", Joiner.on(' ').join(symbols));
            urlBuilder.setParameter("f", tags);

            log.info("Stock request: {}", urlBuilder.toString());

            InputStream s = urlBuilder.toUrl().openStream();
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];

            while (true) {
                int n = s.read(buff);
View Full Code Here


    private static final Logger log = LoggerFactory.getLogger(WikiApplet.class);

    @Override
    public void run(Channel channel, User from, String command, String[] args, String unparsed) {
        Document doc;
        URLBuilder url;

        if (unparsed.trim().length() == 0) {
            channel.write("Missing article name");
            return;
        }

        if (unparsed.trim().equals("-r")) {
            unparsed = "Special:Random";
        }

        try {
            Connection con;

            url = new URLBuilder("http://en.wikipedia.org/w/index.php");
            url.setParameter("search", unparsed);

            while (true) {
                con = Jsoup.connect(url.toString());
                con.followRedirects(false);
                con.execute();

                if (con.response().statusCode() == 200) {
                    doc = con.response().parse();
                    break;
                } else if (con.response().statusCode() == 302) {
                    url = new URLBuilder(con.response().header("Location"));
                } else {
                    channel.write("Could not find page");
                    return;
                }
            }
        } catch (MalformedURLException e) {
            log.error("malformed url", e);
            return;
        } catch (IOException e) {
            log.warn("could not retrieve article", e);
            channel.write("No such article");
            return;
        }

        if (!doc.select("div.noarticletext").isEmpty()) {
            channel.write("No such article");
        } else {
            try {
                Elements elements = doc.select("div.mw-content-ltr > p");

                if (elements.size() > 0) {
                    Element p = elements.first();
                    String summary = p.text();

                    channel.write(url.toString());
                    channel.writeMultiple(BotAppletUtil.blockFormat(summary, 400, 10));
                } else {
                    channel.write("Can not get article summary");
                }
            } catch (Exception e) {
View Full Code Here

    }

    @Override
    public void run(Channel channel, User from, String command, String[] args, String unparsed) {
        Document doc;
        URLBuilder url;
        int definitionIndex = 0;
        boolean random = false;

        if (unparsed.trim().length() == 0) {
            random = true;
        }

        if (args.length == 1 && args[0].equals("-r")) {
            random = true;
        }

        if (args.length >= 2 && args[0].startsWith("-")) {
            String s = args[0].substring(1);
            try {
                definitionIndex = Integer.valueOf(s) - 1;
                unparsed = unparsed.replaceFirst(Pattern.quote(args[0]), "");
            } catch (NumberFormatException e) {
                // -
            }
        }

        try {
            Connection con;

            if (random) {
                url = new URLBuilder("http://www.urbandictionary.com/random.php");
            } else {
                url = new URLBuilder("http://www.urbandictionary.com/define.php");
                url.setParameter("term", unparsed);
            }

            while (true) {
                con = Jsoup.connect(url.toString());
                con.followRedirects(false);
                con.execute();

                if (con.response().statusCode() == 200) {
                    doc = con.response().parse();
                    break;
                } else if (con.response().statusCode() == 302) {
                    url = new URLBuilder(con.response().header("Location"));
                } else {
                    channel.write("Error loading page");
                    return;
                }
            }
        } catch (IOException e) {
            channel.write("Unknown error occured");
            log.error("caught exception while loading page", e);
            return;
        }

        if (!doc.select("div#not_defined_yet").isEmpty()) {
            channel.write("No definitions found");
        } else {
            try {
                Elements elements = doc.select("div.definition");

                if (elements.size() > definitionIndex) {
                    Element def = elements.get(definitionIndex);
                    String definition = this.textWithBreaks(def);
                    String[] lines = definition.split("\n");
                    String longestLine = lines[0];
                    String response;

                    for (int i = 1; i < lines.length && longestLine.length() < 15; i++) {
                        if (lines[i].length() > longestLine.length()) {
                            longestLine = lines[i];
                        }
                    }

                    response = url.getQueryParameter("term") + ": " + longestLine;
                    channel.writeMultiple(BotAppletUtil.blockFormat(response, 400, 10));
                } else {
                    channel.write("Can not get definition");
                }
            } catch (Exception e) {
View Full Code Here

        this.counter = 0;
    }

    @Override
    public void run(Channel channel, User from, String command, String[] args, String unparsed) {
        URLBuilder url;
        String query = unparsed;

        if (query.trim().length() == 0) {
            channel.write("Missing query");
            return;
        }

        try {
            url = new URLBuilder("http://google.com/complete/search");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        url.setParameter("output", "toolbar");
        url.setParameter("q", query);

        try {
            Document doc = this.documentBuilder.parse(url.toString());
            NodeList suggestions = (NodeList) this.xpath.evaluate("//CompleteSuggestion", doc, XPathConstants.NODESET);

            if (suggestions.getLength() > 0) {
                int i = this.counter % suggestions.getLength();
                String suggestion = this.xpath.evaluate(
View Full Code Here

        this.appid = appid;
    }

    @Override
    public void run(Channel channel, User from, String command, String[] args, String unparsed) {
        URLBuilder url;
        String query = unparsed;
        boolean foundResult = false;

        /* Search for a valid result at the following paths in this order */
        String[] resultSearchPaths = { "//pod[@primary]/subpod/plaintext", "//pod[2]/subpod/plaintext",
                "//pod[2]/subpod/img/@src" };

        if (query.trim().length() == 0) {
            channel.write("Missing query");
            return;
        }

        try {
            url = new URLBuilder("http://api.wolframalpha.com/v2/query");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        url.setParameter("appid", this.appid);
        url.setParameter("reinterpret", "true");
        url.setParameter("input", query);

        try {
            Document doc = this.documentBuilder.parse(url.toString());
            String success = this.xpath.evaluate("/queryresult/@success", doc);

            if (success.equals("true")) {
                for (String path : resultSearchPaths) {
                    String result = this.xpath.evaluate(path, doc);
View Full Code Here

        parser = new PosixParser();
    }

    private URLBuilder makeUrl(String base) {
        try {
            return new URLBuilder(base);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

        if (StringUtils.isBlank(query)) {
            String location = properties.<String> get(buildKey(channel, from) + "location");
            return location;
        }

        URLBuilder url = makeUrl(AUTOCOMPLETE_URL);
        url.setParameter("format", "xml");
        url.setParameter("query", query);

        Document doc = documentBuilder.parse(url.toString());
        String location = xpath.evaluate("/RESULTS/l[1]", doc);

        if (StringUtils.isNotBlank(location) && saveSettings) {
            properties.set(buildKey(channel, from) + "location", location);
        }
View Full Code Here

TOP

Related Classes of com.brewtab.ircbot.util.URLBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.