summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Plugin/Google.php
blob: 413a93607551913944405abb768a727876a75e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
 * Phergie 
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie 
 * @package   Phergie_Plugin_Google
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie_Plugin_Google
 */

/**
 * Provides commands used to access several services offered by Google 
 * including search, translation, weather, maps, and currency and general 
 * value unit conversion.
 *
 * @category Phergie 
 * @package  Phergie_Plugin_Google
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie_Plugin_Google
 * @uses     Phergie_Plugin_Command pear.phergie.org
 * @uses     Phergie_Plugin_Http pear.phergie.org
 *
 * @pluginDesc Provide access to some Google services
 */
class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
{

    /**
     * HTTP plugin
     *
     * @var Phergie_Plugin_Http
     */
    protected $http;

    /**
     * Checks for dependencies.
     *
     * @return void
     */
    public function onLoad()
    {
        $plugins = $this->getPluginHandler();
        $plugins->getPlugin('Command');
        $this->http = $plugins->getPlugin('Http');
        $plugins->getPlugin('Help')->register($this);
    }

    /**
     * Returns the first result of a Google search.
     *
     * @param string $query Search term
     *
     * @return void
     * @todo Implement use of URL shortening here
     *
     * @pluginCmd [query] do a search on google
     */
    public function onCommandG($query)
    {
        $url = 'http://ajax.googleapis.com/ajax/services/search/web';
        $params = array(
            'v' => '1.0',
            'q' => $query
        );
        $response = $this->http->get($url, $params);
        $json = $response->getContent()->responseData;
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();
        if ($json->cursor->estimatedResultCount > 0) {
            $msg
                = $nick
                . ': [ '
                . $json->results[0]->titleNoFormatting
                . ' ] - '
                . $json->results[0]->url
                . ' - More results: '
                . $json->cursor->moreResultsUrl;
            $this->doPrivmsg($source, $msg);
        } else {
            $msg = $nick . ': No results for this query.';
            $this->doPrivmsg($source, $msg);
        }
    }

    /**
     * Performs a Google Count search for the given term.
     *
     * @param string $query Search term
     *
     * @return void
     *
     * @pluginCmd [query] Do a search on Google and count the results
     */
    public function onCommandGc($query)
    {
        $url = 'http://ajax.googleapis.com/ajax/services/search/web';
        $params = array(
            'v' => '1.0',
            'q' => $query
        );
        $response = $this->http->get($url, $params);
        $json = $response->getContent()->responseData->cursor;
        $count = $json->estimatedResultCount;
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();
        if ($count) {
            $msg
                = $nick . ': ' . 
                number_format($count, 0) . 
                ' estimated results for ' . $query;
            $this->doPrivmsg($source, $msg);
        } else {
            $this->doPrivmsg($source, $nick . ': No results for this query.');
        }
    }

    /**
     * Performs a Google Translate search for the given term.
     *
     * @param string $from  Language of the search term
     * @param string $to    Language to which the search term should be 
     *        translated
     * @param string $query Term to translate
     *
     * @return void
     *
     * @pluginCmd [from language] [to language] [text to translate] Do a translation on Google
     */
    public function onCommandGt($from, $to, $query)
    {
        $url = 'http://ajax.googleapis.com/ajax/services/language/translate';
        $params = array(
            'v' => '1.0',
            'q' => $query,
            'langpair' => $from . '|' . $to
        );
        $response = $this->http->get($url, $params);
        $json = $response->getContent();
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();
        if (empty($json->responseData->translatedText)) {
            $this->doPrivmsg($source, $nick . ': ' . $json->responseDetails);
        } else {
            $this->doPrivmsg(
                $source, 
                $nick . ': ' . $json->responseData->translatedText
            );
        }
    }

    /**
     * Performs a Google Weather search for the given term.
     * 
     * @param string $location Location to search for
     *
     * @return void
     *
     * @pluginCmd [location] Show the weather for the specified location
     */
    public function onCommandGw($location)
    {
        $url = 'http://www.google.com/ig/api';
        $params = array(
            'weather' => $location,
            'hl' => 'pt-br',
            'oe' => 'UTF-8'
        );
        $response = $this->http->get($url, $params);
        $xml = $response->getContent()->weather;
        $source = $this->getEvent()->getSource();
        if (!isset($xml->problem_cause)) {
            $city = $xml->forecast_information->city->attributes()->data[0];
            $time = $xml->forecast_information->current_date_time->attributes()
                ->data[0];
            $condition = $xml->current_conditions->condition->attributes()->data[0];
            $temp = $xml->current_conditions->temp_c->attributes()->data[0] 
                . '� C';
            $humidity = $xml->current_conditions->humidity->attributes()->data[0];
            $wind = $xml->current_conditions->wind_condition->attributes()->data[0];
            $msg = implode(' - ', array($city, $temp, $condition, $humidity, $wind));
            $this->doPrivmsg($source, $msg);

            foreach ($xml->forecast_conditions as $key => $linha) {
                $day = ucfirst($linha->day_of_week->attributes()->data[0]);
                $min = $linha->low->attributes()->data[0];
                $max = $linha->high->attributes()->data[0];
                $condition = $linha->condition->attributes()->data[0];
                $msg 
                    = 'Forecast: ' . $day . 
                    ' - Min: ' . $min . '� C' . 
                    ' - Max: ' . $max . '� C' . 
                    ' - ' . $condition;
                $this->doPrivmsg($source, $msg);
            }
        } else {
            $this->doPrivmsg($source, $xml->problem_cause->attributes()->data[0]);
        }
    }

    /**
     * Performs a Google Maps search for the given term.
     *
     * @param string $location Location to search for
     *
     * @return void
     *
     * @pluginCmd [location] Get the location from Google Maps to the location specified
     */
    public function onCommandGmap($location)
    {
        $location = utf8_encode($location);
        $url = 'http://maps.google.com/maps/geo';
        $params = array(
            'q' => $location,
            'output' => 'json',
            'gl' => 'br',
            'sensor' => 'false',
            'oe' => 'utf8',
            'mrt' => 'all',
            'key' => $this->_config['google.key']
        );
        $response = $this->http->get($url, $params); 
        $json = (array) $response->getContent();
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();
        if (!empty($json)) {
            $qtd = count($json['Placemark']);
            if ($qtd > 1) {
                if ($qtd <= 3) {
                    foreach ($json['Placemark'] as $places) {
                        $xy = $places['Point']['coordinates'];
                        $address = utf8_decode($places['address']);
                        $url = 'http://maps.google.com/maps?sll=' . $xy[1] . ',' 
                            . $xy[0] . '&z=15';
                        $msg = $nick . ' -> ' . $address . ' - ' . $url;
                        $this->doPrivmsg($source, $msg);
                    }
                } else {
                    $msg
                        = $nick . 
                        ', there are a lot of places with that query.' . 
                        ' Try to be more specific!';
                    $this->doPrivmsg($source, $msg);
                }
            } elseif ($qtd == 1) {
                $xy = $json['Placemark'][0]['Point']['coordinates'];
                $address = utf8_decode($json['Placemark'][0]['address']);
                $url = 'http://maps.google.com/maps?sll=' . $xy[1] . ',' . $xy[0] 
                    . '&z=15';
                $msg = $nick . ' -> ' . $address . ' - ' . $url;
                $this->doPrivmsg($source, $msg);
            } else {
                $this->doPrivmsg($source, $nick . ', I found nothing.');
            }
        } else {
            $this->doPrivmsg($source, $nick . ', we have a problem.');
        }
    }

    /**
     * Perform a Google Convert query to convert a value from one metric to 
     * another.
     *
     * @param string $value Value to convert
     * @param string $from  Source metric
     * @param string $to    Destination metric
     *
     * @return void
     *
     * @pluginCmd [value] [currency from] [currency to] Converts a monetary value from one currency to another
     */
    public function onCommandGconvert($value, $from, $to)
    {
        $url = 'http://www.google.com/finance/converter';
        $params = array(
            'a' => $value,
            'from' => $from,
            'to' => $to
        );
        $response = $this->http->get($url, $params);
        $contents = $response->getContent();
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();
        if ($contents) {
            preg_match(
                '#<span class=bld>.*? ' . $to . '</span>#im',
                $contents,
                $matches
            );
            if (!$matches[0]) {
                $this->doPrivmsg($source, $nick . ', I can\'t do that.');
            } else {
                $str = str_replace('<span class=bld>', '', $matches[0]);
                $str = str_replace($to . '</span>', '', $str);
                $text 
                    = number_format($value, 2, ',', '.') . ' ' . $from . 
                    ' => ' . number_format($str, 2, ',', '.') . ' ' . $to;
                $this->doPrivmsg($source, $text);
            }
        } else {
            $this->doPrivmsg($source, $nick . ', we had a problem.');
        }
    }

    /**
     * Performs a Google search to convert a value from one unit to another.
     *
     * @param string $unit  Source metric 
     * @param string $to    Value to be converted
     * @param string $unit2 Destination metric 
     *
     * @return void
     *
     * @pluginCmd [unit] [to] [unit2] Convert a value from one metric to another
     */
    public function onCommandConvert($unit, $to, $unit2)
    {
        $url = 'http://www.google.com/search?q=' 
            . urlencode($unit . ' ' . $to . ' ' . $unit2);
        $response = $this->http->get($url);
        $contents = $response->getContent();
        $event = $this->getEvent();
        $source = $event->getSource();
        $nick = $event->getNick();

        if (empty($contents)) {
            $this->doPrivmsg(
                $target,
                $nick . ', sorry, I can\'t give you an answer right now.'
            );
            return;
        }

        $doc = new DomDocument;
        $doc->loadHTML($contents);
        foreach ($doc->getElementsByTagName('h2') as $element) {
            if ($element->getAttribute('class') == 'r') {
                $children = $element->childNodes;
                $text = str_replace(
                    array(chr(195), chr(151), chr(160)),
                    array('x', '', ' '),
                    $children->item(0)->nodeValue
                );
                if ($children->length >= 3) {
                    $text
                        .= '^' . $children->item(1)->nodeValue 
                        . $children->item(2)->nodeValue;
                }
            }
        }

        if (isset($text)) {
            $this->doPrivmsg($source, $nick . ': ' . $text);
        } else {
            $this->doPrivmsg($target, $nick . ', sorry I can\'t do that.');
        }
    }
}