All pastes #577278 Raw Edit

Stuff

public text v1 · immutable
·published 2007-06-19 21:00 UTC
rendered paste body
String sMessage = tsSender + ", I couldn't find a wikipedia entry. Try being more specific.";

try
{
    WebRequest wreqWikiSearch = new GetMethodWebRequest(m_sAddress + "?search=" + tsParameters + "&go=Go");
    WebResponse wrespResult = new WebConversation().getResponse(wreqWikiSearch);

    String sURL = wrespResult.getURL().toString();
    TextBlock[] aBlocks = wrespResult.getTextBlocks();

    String sPage = "";
    for (TextBlock tbCurrent : aBlocks)
        sPage += tbCurrent.getText() + "\n";
    
    Pattern p = Pattern.compile("From Wikipedia, the free encyclopedia\n(.*)", Pattern.DOTALL);
    Matcher m = p.matcher(sPage);

    if (m.find())
    {
        if (
            !(
             m.group(1).contains("No page with that title exists") ||
             m.group(1).contains("You can create this article or request it") ||
             m.group(1).contains("Please wikify (format) this article")
            )
           )
        {
            String desc = m.group(1).replaceAll("\\s*\\(.+?\\)", ""); // Delete anything in Parens.
            desc = desc.replaceAll("\n", " ");
            System.out.println(desc);

            sMessage = tsSender + ", " + trimText(desc, 446 - tsSender.length() - 3 - 5 - sURL.length()) + " [ " + sURL + " ]";
        }
    }
    else
    {
        sMessage = "The return from Wikipedia doesn't seem to be a Wikipedia page. Check the config file's URL.";
    }
}
catch (SAXException e)
{
    sMessage = "Caught a SAXException [" + e.getMessage() + "].";
}
catch (MalformedURLException e)
{
    sMessage = "Caught a MalformedURLException [" + e.getMessage() + "]. Check the config file.";
}
catch (IOException e)
{
    sMessage = "Caught an IOException [" + e.getMessage() + "]. The outbound network is probably down.";
}
catch (RuntimeException e)
{
    sMessage = "Caught a RuntimeException [" + e.getMessage() + "].";
}

if (tsChannel != null)
    toBot.sendMessage(tsChannel, sMessage);
else
    toBot.sendMessage(tsSender, sMessage);