org.manalith.ircbot.plugin.weather.WeatherPlugin.java Source code

Java tutorial

Introduction

Here is the source code for org.manalith.ircbot.plugin.weather.WeatherPlugin.java

Source

/*
org.manalith.ircbot.plugin.weather/WeatherPlugin.java
ManalithBot - An open source IRC bot based on the PircBot Framework.
Copyright (C) 2011  Ki-Beom, Kim
    
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.weather;

import java.io.IOException;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.manalith.ircbot.annotation.Option;
import org.manalith.ircbot.common.stereotype.BotCommand;
import org.manalith.ircbot.plugin.SimplePlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class WeatherPlugin extends SimplePlugin {

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

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

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

    @BotCommand("")
    public String getYahooWeather(@Option(name = "", help = " ? ? ") String keyword) {
        try {
            // TODO WOEID  ?
            final String url_woeid = "http://query.yahooapis.com/v1/public/yql"
                    + "?q=select%20woeid%20from%20geo.places%20where%20text%3D%22"
                    + URLEncoder.encode(keyword, "UTF-8") + "%20ko-KR%22%20limit%201";
            final String url_forecast = "http://weather.yahooapis.com/forecastrss?w=%s&u=c";
            final String error_woeid = "23424868";

            Document doc = Jsoup.connect(url_woeid).get();
            // example : http://query.yahooapis.com/v1/public/yql?q=select woeid
            // from geo.places where text%3D\"%2C ko-KR\" limit 1
            String woeid = doc.select("woeid").text();

            if (!woeid.equals(error_woeid)) {
                // example:
                // http://weather.yahooapis.com/forecastrss?w=1132599&u=c
                doc = Jsoup.connect(String.format(url_forecast, woeid)).get();
                String location = doc.getElementsByTag("yweather:location").attr("city");
                String condition = doc.getElementsByTag("yweather:condition").attr("text");
                String temp = doc.getElementsByTag("yweather:condition").attr("temp");
                String humidity = doc.getElementsByTag("yweather:atmosphere").attr("humidity");
                String windCondition = doc.getElementsByTag("yweather:wind").attr("speed");

                return String.format("[%s] %s ? %s, ? %s%%, ?? %skm/h", location, condition, temp,
                        humidity, windCondition);

            } else {
                return String.format(
                        "[%s] ? ?  . ?   ?.",
                        keyword);
            }

        } catch (IOException e) {
            logger.error("failed to run command", e);
            return " ? : " + e.getMessage();
        }
    }
}