}
@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) {