9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Weather
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Weather
23 * Detects and responds to requests for current weather conditions in a
24 * particular location using data from a web service. Requires registering
25 * with weather.com to obtain authentication credentials, which must be
26 * stored in the configuration settings weather.partner_id and
27 * weather.license_key for the plugin to function.
30 * @package Phergie_Plugin_Weather
31 * @author Phergie Development Team <team@phergie.org>
32 * @license http://phergie.org/license New BSD License
33 * @link http://pear.phergie.org/package/Phergie_Plugin_Weather
34 * @link http://www.weather.com/services/xmloap.html
35 * @uses Phergie_Plugin_Command pear.phergie.org
36 * @uses Phergie_Plugin_Http pear.phergie.org
37 * @uses extension SimpleXML
39 class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
42 * Checks for dependencies.
46 public function onLoad()
48 $plugins = $this->getPluginHandler();
49 $plugins->getPlugin('Command');
50 $plugins->getPlugin('Http');
52 if (empty($this->config['weather.partner_id'])
53 || empty($this->config['weather.license_key'])) {
54 $this->fail('weather.partner_id and weather.license_key must be specified');
59 * Returns a weather report for a specified location.
61 * @param string $location Zip code or city/state/country specification
65 public function onCommandWeather($location)
67 $response = $this->plugins->http->get(
68 'http://xoap.weather.com/search/search',
69 array('where' => $location)
72 if ($response->isError()) {
74 $this->event->getNick(),
75 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
80 $nick = $this->event->getNick();
82 $xml = $response->getContent();
83 if (count($xml->loc) == 0) {
84 $this->doNotice($nick, 'No results for that location.');
88 $where = (string) $xml->loc[0]['id'];
89 $response = $this->plugins->http->get(
90 'http://xoap.weather.com/weather/local/' . $where,
95 'par' => $this->config['weather.partner_id'],
96 'key' => $this->config['weather.license_key'],
100 if ($response->isError()) {
102 $this->event->getNick(),
103 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
108 $xml = $response->getContent();
109 $weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
110 $weather .= 'Current temperature ' .
111 (string) $xml->cc->tmp .
112 (string) $xml->head->ut . ' / ';
113 if ((string) $xml->head->ut == 'F') {
114 $weather .= round(((((int) $xml->cc->tmp - 32) * 5) / 9)) . 'C';
116 $weather .= round(((((int) $xml->cc->tmp * 9) / 5) + 32)) . 'F';
119 ', Relative humidity ' . (string) $xml->cc->hmid . '%' .
120 ', Current conditions ' . (string) $xml->cc->t .
121 ', Last update ' . (string) $xml->cc->lsup .
122 ' [ http://weather.com/weather/today/' .
124 array('(', ')', ',', ' '),
125 array('', '', '', '+'),
126 (string) $xml->loc->dnam
130 $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);