org.manalith.ircbot.plugin.uriinfo.UriInfoPlugin.java Source code

Java tutorial

Introduction

Here is the source code for org.manalith.ircbot.plugin.uriinfo.UriInfoPlugin.java

Source

/*
org.manalith.ircbot.plugin.uriinfo/UriInfoPlugin.java
ManalithBot - An open source IRC bot based on the PircBot Framework.
Copyright (C) 2012  Changwoo Ryu <cwryu@debian.org>
Copyright (C) 2012  Seong-ho Cho <darkcircle.0426@gmail.com>
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.manalith.ircbot.plugin.uriinfo;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.UnsupportedMimeTypeException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.manalith.ircbot.plugin.SimplePlugin;
import org.manalith.ircbot.resources.MessageEvent;
import org.manalith.ircbot.util.MessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UriInfoPlugin extends SimplePlugin {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private boolean enablePrintContentType;

    private static final String USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:12.0) "
            + "Gecko/20100101 Firefox/12.0";

    @Override
    public String getCommands() {
        return null;
    }

    @Override
    public String getName() {
        return "URI ";
    }

    @Override
    public String getHelp() {
        return "   URI?  ";
    }

    public void setEnablePrintContentType(boolean enablePrintContentType) {
        this.enablePrintContentType = enablePrintContentType;
    }

    public boolean enablePrintContentType() {
        return enablePrintContentType;
    }

    private String getSiteSpecificTitle(String uri, Document document) {
        if (uri.startsWith("http://mlbpark.donga.com/bbs/view.php?")
                || uri.startsWith("http://mlbpark.donga.com/mbs/articleV.php?")) {
            // MLB Park article
            Element element = document.getElementsByClass("D14").first();
            if (element != null) {
                try {
                    return element.child(0).text();
                } catch (IndexOutOfBoundsException e) {
                    return null;
                }
            }
        } else if (uri.startsWith("http://www.slrclub.com/bbs/vx2.php?")) {
            Element element = document.getElementsByClass("sbj").first();
            if (element != null) {
                return element.text();
            }
        }

        return null;
    }

    private String getInfo(String uri) {
        String result = null;
        Response response;

        try {
            // ? ?? User Agent  ? 
            response = Jsoup.connect(uri).userAgent(USER_AGENT).execute();
        } catch (UnsupportedMimeTypeException e) {
            return enablePrintContentType ? "[?? ?] " + e.getMimeType() : null;
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
            return null;
        }

        String contentType = response.contentType();

        // ? title?  ? .
        try {
            Document document = response.parse();
            String title = document.title();

            if (StringUtils.isBlank(title))
                throw new IOException();

            title = title.trim().replaceAll("(\\s){1,}", " ");

            //  ??  
            String stitle = getSiteSpecificTitle(uri, document);
            if (stitle == null)
                result = "[?? ] " + title;
            else
                result = "[?? ] " + stitle + " | " + title;
        } catch (IOException e) {
            // parse  ?  title -- HTML?
            //   ???  
            // content type  
            if (contentType.startsWith("text/html"))
                result = "[?? ]";
        }

        if (result == null && enablePrintContentType) {
            result = "[?? ?] " + contentType;
        }

        return result;
    }

    @Override
    public void onMessage(MessageEvent event) {
        String message = event.getMessage();

        String uri = MessageUtils.findUri(message);
        if (uri == null)
            return;

        String info = getInfo(uri);
        if (info != null) {
            // This plugin runs implicitly
            event.respond(info, false);
        }
    }
}