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 * Converts a temperature in Celsius to Fahrenheit.
61 * @param int $temp Temperature in Celsius
63 * @return int Temperature converted to Fahrenheit
65 public function convertCelsiusToFahrenheit($temp)
67 return round(((((int) $temp * 9) / 5) + 32));
71 * Converts a temperature in Fahrenheit to Celsius.
73 * @param int $temp Temperature in Fahrenheit
75 * @return int Temperature converted to Celsius
77 public function convertFahrenheitToCelsius($temp)
79 return round(((((int) $temp - 32) * 5) / 9));
83 * Returns a weather report for a specified location.
85 * @param string $location Zip code or city/state/country specification
89 public function onCommandWeather($location)
91 $response = $this->plugins->http->get(
92 'http://xoap.weather.com/search/search',
93 array('where' => $location)
96 if ($response->isError()) {
98 $this->event->getNick(),
99 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
104 $nick = $this->event->getNick();
106 $xml = $response->getContent();
107 if (count($xml->loc) == 0) {
108 $this->doNotice($nick, 'No results for that location.');
112 $where = (string) $xml->loc[0]['id'];
113 $response = $this->plugins->http->get(
114 'http://xoap.weather.com/weather/local/' . $where,
119 'par' => $this->config['weather.partner_id'],
120 'key' => $this->config['weather.license_key'],
124 if ($response->isError()) {
126 $this->event->getNick(),
127 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
132 $xml = $response->getContent();
133 $weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
134 switch ($xml->head->ut) {
136 $tempF = $xml->cc->tmp;
137 $tempC = $this->convertFahrenheitToCelsius($tempF);
140 $tempC = $xml->cc->tmp;
141 $tempF = $this->convertCelsiusToFahrenheit($tempC);
145 $this->event->getNick(),
146 'ERROR: No scale information given.');
150 $tempF2 = $tempF * $tempF;
154 (2.04901523 * $tempF) +
156 (.22475541 * $tempF * $r) -
157 (6.83783 * pow(10,-3) * $tempF2) -
158 (5.481717 * pow(10,-2) * $r2) +
159 (1.22874 * pow(10,-3) * $tempF2 * $r) +
160 (8.5282 * pow(10,-4) * $tempF * $r2) -
161 (1.99 * pow(10,-6) * $tempF2 * $r2)
163 $hiC = $this->convertFahrenheitToCelsius($hiF);
164 $weather .= 'Temperature: ' . $tempF . 'F/' . $tempC . 'C';
165 $weather .= ', Humidity: ' . (string) $xml->cc->hmid . '%';
166 if ($hiF > $tempF || $hiC > $tempC) {
167 $weather .= ', Heat Index: ' . $hiF . 'F/' . $hiC . 'C';
170 ', Conditions: ' . (string) $xml->cc->t .
171 ', Updated: ' . (string) $xml->cc->lsup .
172 ' [ http://weather.com/weather/today/' .
174 array('(', ')', ',', ' '),
175 array('', '', '', '+'),
176 (string) $xml->loc->dnam
180 $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);