]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Weather.php
7d3cc1de0381d0c7326f9805d33ad3e7269ea6aa
[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      * Returns a weather report for a specified location.
60      *
61      * @param string $location Zip code or city/state/country specification
62      *
63      * @return void
64      */
65     public function onCommandWeather($location)
66     {
67         $response = $this->plugins->http->get(
68             'http://xoap.weather.com/search/search',
69             array('where' => $location)
70         );
71
72         if ($response->isError()) {
73             $this->doNotice(
74                 $this->event->getNick(),
75                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
76             );
77             return;
78         }
79
80         $nick = $this->event->getNick();
81
82         $xml = $response->getContent();
83         if (count($xml->loc) == 0) {
84             $this->doNotice($nick, 'No results for that location.');
85             return;
86         }
87
88         $where = (string) $xml->loc[0]['id'];
89         $response = $this->plugins->http->get(
90             'http://xoap.weather.com/weather/local/' . $where,
91             array(
92                 'cc' => '*',
93                 'link' => 'xoap',
94                 'prod' => 'xoap',
95                 'par' => $this->config['weather.partner_id'],
96                 'key' => $this->config['weather.license_key'],
97             )
98         );
99
100         if ($response->isError()) {
101             $this->doNotice(
102                 $this->event->getNick(),
103                 'ERROR: ' . $response->getCode() . ' ' . $response->getMessage()
104             );
105             return;
106         }
107
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';
115         } else {
116             $weather .= round(((((int) $xml->cc->tmp * 9) / 5) + 32)) . 'F';
117         }
118         $weather .=
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/' .
123             str_replace(
124                 array('(', ')', ',', ' '),
125                 array('', '', '', '+'),
126                 (string) $xml->loc->dnam
127             ) .
128             ' ]';
129
130         $this->doPrivmsg($this->event->getSource(), $nick . ': ' . $weather);
131     }
132 }