]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Google.php
Added Phergie PHP IRC library
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Google.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_Google
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_Google
20  */
21
22 /**
23  * Provides commands used to access several services offered by Google 
24  * including search, translation, weather, maps, and currency and general 
25  * value unit conversion.
26  *
27  * @category Phergie 
28  * @package  Phergie_Plugin_Google
29  * @author   Phergie Development Team <team@phergie.org>
30  * @license  http://phergie.org/license New BSD License
31  * @link     http://pear.phergie.org/package/Phergie_Plugin_Google
32  * @uses     Phergie_Plugin_Command pear.phergie.org
33  * @uses     Phergie_Plugin_Http pear.phergie.org
34  *
35  * @pluginDesc Provide access to some Google services
36  */
37 class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
38 {
39
40     /**
41      * HTTP plugin
42      *
43      * @var Phergie_Plugin_Http
44      */
45     protected $http;
46
47     /**
48      * Checks for dependencies.
49      *
50      * @return void
51      */
52     public function onLoad()
53     {
54         $plugins = $this->getPluginHandler();
55         $plugins->getPlugin('Command');
56         $this->http = $plugins->getPlugin('Http');
57         $plugins->getPlugin('Help')->register($this);
58     }
59
60     /**
61      * Returns the first result of a Google search.
62      *
63      * @param string $query Search term
64      *
65      * @return void
66      * @todo Implement use of URL shortening here
67      *
68      * @pluginCmd [query] do a search on google
69      */
70     public function onCommandG($query)
71     {
72         $url = 'http://ajax.googleapis.com/ajax/services/search/web';
73         $params = array(
74             'v' => '1.0',
75             'q' => $query
76         );
77         $response = $this->http->get($url, $params);
78         $json = $response->getContent()->responseData;
79         $event = $this->getEvent();
80         $source = $event->getSource();
81         $nick = $event->getNick();
82         if ($json->cursor->estimatedResultCount > 0) {
83             $msg
84                 = $nick
85                 . ': [ '
86                 . $json->results[0]->titleNoFormatting
87                 . ' ] - '
88                 . $json->results[0]->url
89                 . ' - More results: '
90                 . $json->cursor->moreResultsUrl;
91             $this->doPrivmsg($source, $msg);
92         } else {
93             $msg = $nick . ': No results for this query.';
94             $this->doPrivmsg($source, $msg);
95         }
96     }
97
98     /**
99      * Performs a Google Count search for the given term.
100      *
101      * @param string $query Search term
102      *
103      * @return void
104      *
105      * @pluginCmd [query] Do a search on Google and count the results
106      */
107     public function onCommandGc($query)
108     {
109         $url = 'http://ajax.googleapis.com/ajax/services/search/web';
110         $params = array(
111             'v' => '1.0',
112             'q' => $query
113         );
114         $response = $this->http->get($url, $params);
115         $json = $response->getContent()->responseData->cursor;
116         $count = $json->estimatedResultCount;
117         $event = $this->getEvent();
118         $source = $event->getSource();
119         $nick = $event->getNick();
120         if ($count) {
121             $msg
122                 = $nick . ': ' . 
123                 number_format($count, 0) . 
124                 ' estimated results for ' . $query;
125             $this->doPrivmsg($source, $msg);
126         } else {
127             $this->doPrivmsg($source, $nick . ': No results for this query.');
128         }
129     }
130
131     /**
132      * Performs a Google Translate search for the given term.
133      *
134      * @param string $from  Language of the search term
135      * @param string $to    Language to which the search term should be 
136      *        translated
137      * @param string $query Term to translate
138      *
139      * @return void
140      *
141      * @pluginCmd [from language] [to language] [text to translate] Do a translation on Google
142      */
143     public function onCommandGt($from, $to, $query)
144     {
145         $url = 'http://ajax.googleapis.com/ajax/services/language/translate';
146         $params = array(
147             'v' => '1.0',
148             'q' => $query,
149             'langpair' => $from . '|' . $to
150         );
151         $response = $this->http->get($url, $params);
152         $json = $response->getContent();
153         $event = $this->getEvent();
154         $source = $event->getSource();
155         $nick = $event->getNick();
156         if (empty($json->responseData->translatedText)) {
157             $this->doPrivmsg($source, $nick . ': ' . $json->responseDetails);
158         } else {
159             $this->doPrivmsg(
160                 $source, 
161                 $nick . ': ' . $json->responseData->translatedText
162             );
163         }
164     }
165
166     /**
167      * Performs a Google Weather search for the given term.
168      * 
169      * @param string $location Location to search for
170      *
171      * @return void
172      *
173      * @pluginCmd [location] Show the weather for the specified location
174      */
175     public function onCommandGw($location)
176     {
177         $url = 'http://www.google.com/ig/api';
178         $params = array(
179             'weather' => $location,
180             'hl' => 'pt-br',
181             'oe' => 'UTF-8'
182         );
183         $response = $this->http->get($url, $params);
184         $xml = $response->getContent()->weather;
185         $source = $this->getEvent()->getSource();
186         if (!isset($xml->problem_cause)) {
187             $city = $xml->forecast_information->city->attributes()->data[0];
188             $time = $xml->forecast_information->current_date_time->attributes()
189                 ->data[0];
190             $condition = $xml->current_conditions->condition->attributes()->data[0];
191             $temp = $xml->current_conditions->temp_c->attributes()->data[0] 
192                 . '� C';
193             $humidity = $xml->current_conditions->humidity->attributes()->data[0];
194             $wind = $xml->current_conditions->wind_condition->attributes()->data[0];
195             $msg = implode(' - ', array($city, $temp, $condition, $humidity, $wind));
196             $this->doPrivmsg($source, $msg);
197
198             foreach ($xml->forecast_conditions as $key => $linha) {
199                 $day = ucfirst($linha->day_of_week->attributes()->data[0]);
200                 $min = $linha->low->attributes()->data[0];
201                 $max = $linha->high->attributes()->data[0];
202                 $condition = $linha->condition->attributes()->data[0];
203                 $msg 
204                     = 'Forecast: ' . $day . 
205                     ' - Min: ' . $min . '� C' . 
206                     ' - Max: ' . $max . '� C' . 
207                     ' - ' . $condition;
208                 $this->doPrivmsg($source, $msg);
209             }
210         } else {
211             $this->doPrivmsg($source, $xml->problem_cause->attributes()->data[0]);
212         }
213     }
214
215     /**
216      * Performs a Google Maps search for the given term.
217      *
218      * @param string $location Location to search for
219      *
220      * @return void
221      *
222      * @pluginCmd [location] Get the location from Google Maps to the location specified
223      */
224     public function onCommandGmap($location)
225     {
226         $location = utf8_encode($location);
227         $url = 'http://maps.google.com/maps/geo';
228         $params = array(
229             'q' => $location,
230             'output' => 'json',
231             'gl' => 'br',
232             'sensor' => 'false',
233             'oe' => 'utf8',
234             'mrt' => 'all',
235             'key' => $this->_config['google.key']
236         );
237         $response = $this->http->get($url, $params); 
238         $json = (array) $response->getContent();
239         $event = $this->getEvent();
240         $source = $event->getSource();
241         $nick = $event->getNick();
242         if (!empty($json)) {
243             $qtd = count($json['Placemark']);
244             if ($qtd > 1) {
245                 if ($qtd <= 3) {
246                     foreach ($json['Placemark'] as $places) {
247                         $xy = $places['Point']['coordinates'];
248                         $address = utf8_decode($places['address']);
249                         $url = 'http://maps.google.com/maps?sll=' . $xy[1] . ',' 
250                             . $xy[0] . '&z=15';
251                         $msg = $nick . ' -> ' . $address . ' - ' . $url;
252                         $this->doPrivmsg($source, $msg);
253                     }
254                 } else {
255                     $msg
256                         = $nick . 
257                         ', there are a lot of places with that query.' . 
258                         ' Try to be more specific!';
259                     $this->doPrivmsg($source, $msg);
260                 }
261             } elseif ($qtd == 1) {
262                 $xy = $json['Placemark'][0]['Point']['coordinates'];
263                 $address = utf8_decode($json['Placemark'][0]['address']);
264                 $url = 'http://maps.google.com/maps?sll=' . $xy[1] . ',' . $xy[0] 
265                     . '&z=15';
266                 $msg = $nick . ' -> ' . $address . ' - ' . $url;
267                 $this->doPrivmsg($source, $msg);
268             } else {
269                 $this->doPrivmsg($source, $nick . ', I found nothing.');
270             }
271         } else {
272             $this->doPrivmsg($source, $nick . ', we have a problem.');
273         }
274     }
275
276     /**
277      * Perform a Google Convert query to convert a value from one metric to 
278      * another.
279      *
280      * @param string $value Value to convert
281      * @param string $from  Source metric
282      * @param string $to    Destination metric
283      *
284      * @return void
285      *
286      * @pluginCmd [value] [currency from] [currency to] Converts a monetary value from one currency to another
287      */
288     public function onCommandGconvert($value, $from, $to)
289     {
290         $url = 'http://www.google.com/finance/converter';
291         $params = array(
292             'a' => $value,
293             'from' => $from,
294             'to' => $to
295         );
296         $response = $this->http->get($url, $params);
297         $contents = $response->getContent();
298         $event = $this->getEvent();
299         $source = $event->getSource();
300         $nick = $event->getNick();
301         if ($contents) {
302             preg_match(
303                 '#<span class=bld>.*? ' . $to . '</span>#im',
304                 $contents,
305                 $matches
306             );
307             if (!$matches[0]) {
308                 $this->doPrivmsg($source, $nick . ', I can\'t do that.');
309             } else {
310                 $str = str_replace('<span class=bld>', '', $matches[0]);
311                 $str = str_replace($to . '</span>', '', $str);
312                 $text 
313                     = number_format($value, 2, ',', '.') . ' ' . $from . 
314                     ' => ' . number_format($str, 2, ',', '.') . ' ' . $to;
315                 $this->doPrivmsg($source, $text);
316             }
317         } else {
318             $this->doPrivmsg($source, $nick . ', we had a problem.');
319         }
320     }
321
322     /**
323      * Performs a Google search to convert a value from one unit to another.
324      *
325      * @param string $unit  Source metric 
326      * @param string $to    Value to be converted
327      * @param string $unit2 Destination metric 
328      *
329      * @return void
330      *
331      * @pluginCmd [unit] [to] [unit2] Convert a value from one metric to another
332      */
333     public function onCommandConvert($unit, $to, $unit2)
334     {
335         $url = 'http://www.google.com/search?q=' 
336             . urlencode($unit . ' ' . $to . ' ' . $unit2);
337         $response = $this->http->get($url);
338         $contents = $response->getContent();
339         $event = $this->getEvent();
340         $source = $event->getSource();
341         $nick = $event->getNick();
342
343         if (empty($contents)) {
344             $this->doPrivmsg(
345                 $target,
346                 $nick . ', sorry, I can\'t give you an answer right now.'
347             );
348             return;
349         }
350
351         $doc = new DomDocument;
352         $doc->loadHTML($contents);
353         foreach ($doc->getElementsByTagName('h2') as $element) {
354             if ($element->getAttribute('class') == 'r') {
355                 $children = $element->childNodes;
356                 $text = str_replace(
357                     array(chr(195), chr(151), chr(160)),
358                     array('x', '', ' '),
359                     $children->item(0)->nodeValue
360                 );
361                 if ($children->length >= 3) {
362                     $text
363                         .= '^' . $children->item(1)->nodeValue 
364                         . $children->item(2)->nodeValue;
365                 }
366             }
367         }
368
369         if (isset($text)) {
370             $this->doPrivmsg($source, $nick . ': ' . $text);
371         } else {
372             $this->doPrivmsg($target, $nick . ', sorry I can\'t do that.');
373         }
374     }
375 }