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 Phergie_Plugin_Temperature pear.phergie.org
38 * @uses extension SimpleXML
40 class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
43 * Checks for dependencies.
47 public function onLoad()
49 $plugins = $this->getPluginHandler();
50 $plugins->getPlugin('Command');
51 $plugins->getPlugin('Http');
52 $plugins->getPlugin('Temperature');
54 if (empty($this->config['weather.partner_id'])
55 || empty($this->config['weather.license_key'])) {
56 $this->fail('weather.partner_id and weather.license_key must be specified');
61 * Returns a weather report for a specified location.
63 * @param string $location Zip code or city/state/country specification
67 public function onCommandWeather($location)
69 $response = $this->plugins->http->get(
70 'http://xoap.weather.com/search/search',
71 array('where' => $location)
74 if ($response->isError()) {
76 $this->event->getNick(),
77 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
82 $nick = $this->event->getNick();
84 $xml = $response->getContent();
85 if (count($xml->loc) == 0) {
86 $this->doNotice($nick, 'No results for that location.');
90 $where = (string) $xml->loc[0]['id'];
91 $response = $this->plugins->http->get(
92 'http://xoap.weather.com/weather/local/' . $where,
97 'par' => $this->config['weather.partner_id'],
98 'key' => $this->config['weather.license_key'],
102 if ($response->isError()) {
104 $this->event->getNick(),
105 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
110 $temperature = $this->plugins->getPlugin('Temperature');
111 $xml = $response->getContent();
112 $weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
113 switch ($xml->head->ut) {
115 $tempF = $xml->cc->tmp;
116 $tempC = $temperature->convertFahrenheitToCelsius($tempF);
119 $tempC = $xml->cc->tmp;
120 $tempF = $temperature->convertCelsiusToFahrenheit($tempC);
124 $this->event->getNick(),
125 'ERROR: No scale information given.');
129 $hiF = $temperature->getHeatIndex($tempF, $r);
130 $hiC = $temperature->convertFahrenheitToCelsius($hiF);
131 $weather .= 'Temperature: ' . $tempF . 'F/' . $tempC . 'C';
132 $weather .= ', Humidity: ' . (string) $xml->cc->hmid . '%';
133 if ($hiF > $tempF || $hiC > $tempC) {
134 $weather .= ', Heat Index: ' . $hiF . 'F/' . $hiC . 'C';
137 ', Conditions: ' . (string) $xml->cc->t .
138 ', Updated: ' . (string) $xml->cc->lsup .
139 ' [ http://weather.com/weather/today/' .
141 array('(', ')', ',', ' '),
142 array('', '', '', '+'),
143 (string) $xml->loc->dnam
147 $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);