Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/Weather.php

Revision 273, 3.5 kB (checked in by Seldaek, 2 months ago)

* Plugins that can't load now return error messages to say why
fixes #35

Line 
1 <?php
2
3 /**
4  * Detects and responds to requests for current weather conditions in a
5  * particular location using data from a web service. Requires registering
6  * with weather.com to obtain authentication credentials, which must be
7  * stored in the configuration settings partner_id and license_key for the
8  * plugin to function.
9  *
10  * @see http://www.weather.com/services/xmloap.html
11  */
12 class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract_Command
13 {
14     /**
15      * Partner ID for web service authentication
16      *
17      * @var string
18      */
19     protected $partnerId;
20
21     /**
22      * License Key for web service authentication
23      *
24      * @var string
25      */
26     protected $licenseKey;
27
28     /**
29      * Obtains configuration settings used for web service authentication.
30      *
31      * @return void
32      */
33     public function onInit()
34     {
35         $this->partnerId = $this->getPluginIni('partner_id');
36         $this->licenseKey = $this->getPluginIni('license_key');
37     }
38
39     /**
40      * Returns whether or not the plugin's dependencies are met.
41      *
42      * @param Phergie_Driver_Abstract $client Client instance
43      * @param array $plugins List of short names for plugins that the
44      *                       bootstrap file intends to instantiate
45      * @see Phergie_Plugin_Abstract_Base::checkDependencies()
46      * @return bool TRUE if dependencies are met, FALSE otherwise
47      */
48     public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins)
49     {
50         $partnerId = $client->getIni('weather.partner_id');
51         $licenseKey = $client->getIni('weather.license_key');
52
53         $errors = array();
54
55         if (!$licenseKey) {
56             $errors[] = 'Ini setting weather.license_key must be filled-in';
57         }
58         if (!$partnerId) {
59             $errors[] = 'Ini setting weather.partner_id must be filled-in';
60         }
61
62         return empty($errors) ? true : $errors;
63     }
64
65     public function onDoWeather($where)
66     {
67         $target = $this->event->getNick();
68
69         $context = stream_context_create(array(
70             'http' => array(
71                 'timeout' => 3
72             )
73         ));
74
75         $feed = 'http://xoap.weather.com/search/search?where=' . urlencode($where);
76         $contents = @file_get_contents($feed, null, $context);
77         if (!$contents) {
78             return;
79         }
80
81         $xml = new SimpleXMLElement($contents);
82         if (count($xml->loc) != 0) {
83             $where = $xml->loc[0]['id'];
84             $feed = 'http://xoap.weather.com/weather/local/' . $where . '?cc=*&link=xoap&par=' . $this->partnerId . '&key=' . $this->licenseKey;
85             $contents = @file_get_contents($feed, null, $context);
86             if (!$contents) {
87                 return;
88             }
89
90             $xml = new SimpleXMLElement($contents);
91             $weather = 'Weather for ' . $xml->loc->dnam . ' :: ';
92             $weather .= 'Current temperature: ' . $xml->cc->tmp . $xml->head->ut . '/';
93             if ($xml->head->ut == 'F') {
94                 $celsius = round(((($xml->cc->tmp - 32) * 5) / 9));
95                 $weather .= $celsius . 'C/' . ($celsius + 273) . 'K, ';
96             } else {
97                 $weather .= round(((($xml->cc->tmp * 9) / 5) + 32)) . 'F/';
98                 $weather .= ($xml->cc->tmp + 273) . 'K, ';
99             }
100             $weather .= 'Current conditions: ' . $xml->cc->t . ', ';
101             $weather .= 'Last update: ' . $xml->cc->lsup;
102         } else {
103             $weather = 'No results for that location.';
104         }
105
106         $this->doPrivmsg($this->event->getSource(), $target . ': ' . $weather);
107     }
108 }
109
Note: See TracBrowser for help on using the browser.