package com.techcavern.wavetact.ircCommands.minecraft;

import com.techcavern.wavetact.annot.IRCCMD;
import com.techcavern.wavetact.objects.IRCCommand;
import com.techcavern.wavetact.utils.ErrorUtils;
import com.techcavern.wavetact.utils.GeneralUtils;
import com.techcavern.wavetact.utils.IRCUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.User;

@IRCCMD
public class MCWiki extends IRCCommand {

    public MCWiki() {
        super(GeneralUtils.toArray("mcwiki mwiki"), 0, "mcwiki [string to search minecraft wikis]", "Searches official minecraft wiki & official & unofficial ftb wikis", false);
    }

    @Override
    public void onCommand(User user, PircBotX network, String prefix, Channel channel, boolean isPrivate, int userPermLevel, String... args) throws Exception {
        Document doc = null;
        Elements content = null;
        for (int i = 0; i < args.length; i++) {
            args[i] = StringUtils.capitalize(args[i]);
        }
        String url = "http://minecraft.gamepedia.com/" + StringUtils.join(args, "%20");
        try {
            doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17").get();
        } catch (Exception eee) {
            url = "http://ftb.gamepedia.com/" + StringUtils.join(args, "%20");
            try {
                doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17").get();
            } catch (Exception e) {
                try {
                    url = "http://ftbwiki.org/" + StringUtils.join(args, "%20");
                    doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17").get();
                } catch (Exception ee) {
                    ErrorUtils.sendError(user, "Query returned no results or wikis are Down");
                    return;
                }
            }
        }
        content = doc.select("#mw-content-text");
        content.select(".notaninfobox").remove();
        content.select(".infobox").remove();
        content = content.select("p");
        String title = doc.title().replace(" - Feed The Beast wiki", "").replace(" - Feed The Beast Wiki", "").replace(" - Minecraft Wiki", "");
        String text = "";
        int i = 0;
        while (text.replaceAll(" ", "").isEmpty() && i <= content.size()) {
            text = content.get(i).toString().replaceAll("<.*?>", "").replaceAll("&.*?;", "").replaceAll("\\[.*\\]", "");
            i++;
        }
        if (text.length() > 700) {
            text = StringUtils.substring(text, 0, 697) + "...";
        }
        IRCUtils.sendMessage(user, network, channel, title + ": " + text, prefix);
        IRCUtils.sendMessage(user, network, channel, url, prefix);
    }
}

@@ -2039,24 +2039,66 @@
 ption ee) %7B%0A
+                    ee.printStackTrace();%0A
             
@@ -2176,24 +2176,24 @@
 are Down%22);%0A
-
             
@@ -2296,24 +2296,71 @@
 ent-text%22);%0A
+        content.select(%22.atemplate%22).remove();%0A
         cont
@@ -2485,153 +2485,8 @@
 %22);%0A
-        String title = doc.title().replace(%22 - Feed The Beast wiki%22, %22%22).replace(%22 - Feed The Beast Wiki%22, %22%22).replace(%22 - Minecraft Wiki%22, %22%22);%0A
     
@@ -2851,32 +2851,32 @@
 ...%22;%0A        %7D%0A
+
         IRCUtils
@@ -2915,23 +2915,8 @@
 nel,
- title + %22: %22 +
  tex

Remove Title from @MCWiki Command