]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Weather.php
Merged in changes to Phergie
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Weather.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
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.
28  *
29  * @category Phergie
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
38  */
39 class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
40 {
41     /**
42      * Checks for dependencies.
43      *
44      * @return void
45      */
46     public function onLoad()
47     {
48         $plugins = $this->getPluginHandler();
49         $plugins->getPlugin('Command');
50         $plugins->getPlugin('Http');
51
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');
55         }
56     }
57
58     /**
59      * Converts a temperature in Celsius to Fahrenheit.
60      *
61      * @param int $temp Temperature in Celsius
62      *
63      * @return int Temperature converted to Fahrenheit
64      */
65     public function convertCelsiusToFahrenheit($temp)
66     {
67         return round(((((int) $temp * 9) / 5) + 32));
68     }
69
70     /**
71      * Converts a temperature in Fahrenheit to Celsius.
72      *
73      * @param int $temp Temperature in Fahrenheit
74      *
75      * @return int Temperature converted to Celsius
76      */
77     public function convertFahrenheitToCelsius($temp)
78     {
79         return round(((((int) $temp - 32) * 5) / 9));
80     }
81
82     /**
83      * Returns a weather report for a specified location.
84      *
85      * @param string $location Zip code or city/state/country specification
86      *
87      * @return void
88      */
89     public function onCommandWeather($location)
90     {
91         $response = $this->plugins->http->get(
92             'http://xoap.weather.com/search/search',
93             array('where' => $location)
94         );
95
96         if ($response->isError()) {
97             $this->doNotice(
98                 $this->event->getNick(),
99                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
100             );
101             return;
102         }
103
104         $nick = $this->event->getNick();
105
106         $xml = $response->getContent();
107         if (count($xml->loc) == 0) {
108             $this->doNotice($nick, 'No results for that location.');
109             return;
110         }
111
112         $where = (string) $xml->loc[0]['id'];
113         $response = $this->plugins->http->get(
114             'http://xoap.weather.com/weather/local/' . $where,
115             array(
116                 'cc' => '*',
117                 'link' => 'xoap',
118                 'prod' => 'xoap',
119                 'par' => $this->config['weather.partner_id'],
120                 'key' => $this->config['weather.license_key'],
121             )
122         );
123
124         if ($response->isError()) {
125             $this->doNotice(
126                 $this->event->getNick(),
127                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
128             );
129             return;
130         }
131
132         $xml = $response->getContent();
133         $weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
134         switch ($xml->head->ut) {
135             case 'F':
136                 $tempF = $xml->cc->tmp;
137                 $tempC = $this->convertFahrenheitToCelsius($tempF);
138                 break;
139             case 'C':
140                 $tempC = $xml->cc->tmp;
141                 $tempF = $this->convertCelsiusToFahrenheit($tempC);
142                 break;
143             default:
144                 $this->doNotice(
145                     $this->event->getNick(),
146                     'ERROR: No scale information given.');
147                 break;
148         }
149         $r = $xml->cc->hmid;
150         $tempF2 = $tempF * $tempF;
151         $r2 = $r * $r;
152         $hiF = round(
153             -42.379 +
154             (2.04901523 * $tempF) +
155             (10.14333127 * $r) -
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)
162         );
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';
168         }
169         $weather .=
170             ', Conditions: ' . (string) $xml->cc->t .
171             ', Updated: ' . (string) $xml->cc->lsup .
172             ' [ http://weather.com/weather/today/' .
173             str_replace(
174                 array('(', ')', ',', ' '),
175                 array('', '', '', '+'),
176                 (string) $xml->loc->dnam
177             ) .
178             ' ]';
179
180         $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);
181     }
182 }