]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Weather.php
Merge branch '0.9.x' into 1.0.x
[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     Phergie_Plugin_Temperature pear.phergie.org
38  * @uses     extension SimpleXML
39  */
40 class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
41 {
42     /**
43      * Checks for dependencies.
44      *
45      * @return void
46      */
47     public function onLoad()
48     {
49         $plugins = $this->getPluginHandler();
50         $plugins->getPlugin('Command');
51         $plugins->getPlugin('Http');
52         $plugins->getPlugin('Temperature');
53
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');
57         }
58     }
59
60     /**
61      * Returns a weather report for a specified location.
62      *
63      * @param string $location Zip code or city/state/country specification
64      *
65      * @return void
66      */
67     public function onCommandWeather($location)
68     {
69         $response = $this->plugins->http->get(
70             'http://xoap.weather.com/search/search',
71             array('where' => $location)
72         );
73
74         if ($response->isError()) {
75             $this->doNotice(
76                 $this->event->getNick(),
77                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
78             );
79             return;
80         }
81
82         $nick = $this->event->getNick();
83
84         $xml = $response->getContent();
85         if (count($xml->loc) == 0) {
86             $this->doNotice($nick, 'No results for that location.');
87             return;
88         }
89
90         $where = (string) $xml->loc[0]['id'];
91         $response = $this->plugins->http->get(
92             'http://xoap.weather.com/weather/local/' . $where,
93             array(
94                 'cc' => '*',
95                 'link' => 'xoap',
96                 'prod' => 'xoap',
97                 'par' => $this->config['weather.partner_id'],
98                 'key' => $this->config['weather.license_key'],
99             )
100         );
101
102         if ($response->isError()) {
103             $this->doNotice(
104                 $this->event->getNick(),
105                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
106             );
107             return;
108         }
109
110         $temperature = $this->plugins->getPlugin('Temperature');
111         $xml = $response->getContent();
112         $weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
113         switch ($xml->head->ut) {
114             case 'F':
115                 $tempF = $xml->cc->tmp;
116                 $tempC = $temperature->convertFahrenheitToCelsius($tempF);
117                 break;
118             case 'C':
119                 $tempC = $xml->cc->tmp;
120                 $tempF = $temperature->convertCelsiusToFahrenheit($tempC);
121                 break;
122             default:
123                 $this->doNotice(
124                     $this->event->getNick(),
125                     'ERROR: No scale information given.');
126                 break;
127         }
128         $r = $xml->cc->hmid;
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';
135         }
136         $weather .=
137             ', Conditions: ' . (string) $xml->cc->t .
138             ', Updated: ' . (string) $xml->cc->lsup .
139             ' [ http://weather.com/weather/today/' .
140             str_replace(
141                 array('(', ')', ',', ' '),
142                 array('', '', '', '+'),
143                 (string) $xml->loc->dnam
144             ) .
145             ' ]';
146
147         $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);
148     }
149 }