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
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
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.
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
35 * @pluginDesc Provide access to some Google services
37 class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
43 * @var Phergie_Plugin_Http
48 * Checks for dependencies.
52 public function onLoad()
54 $plugins = $this->getPluginHandler();
55 $plugins->getPlugin('Command');
56 $this->http = $plugins->getPlugin('Http');
57 $plugins->getPlugin('Help')->register($this);
61 * Returns the first result of a Google search.
63 * @param string $query Search term
66 * @todo Implement use of URL shortening here
68 * @pluginCmd [query] do a search on google
70 public function onCommandG($query)
72 $url = 'http://ajax.googleapis.com/ajax/services/search/web';
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) {
86 . $json->results[0]->titleNoFormatting
88 . $json->results[0]->url
90 . $json->cursor->moreResultsUrl;
91 $this->doPrivmsg($source, $msg);
93 $msg = $nick . ': No results for this query.';
94 $this->doPrivmsg($source, $msg);
99 * Performs a Google Count search for the given term.
101 * @param string $query Search term
105 * @pluginCmd [query] Do a search on Google and count the results
107 public function onCommandGc($query)
109 $url = 'http://ajax.googleapis.com/ajax/services/search/web';
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();
123 number_format($count, 0) .
124 ' estimated results for ' . $query;
125 $this->doPrivmsg($source, $msg);
127 $this->doPrivmsg($source, $nick . ': No results for this query.');
132 * Performs a Google Translate search for the given term.
134 * @param string $from Language of the search term
135 * @param string $to Language to which the search term should be
137 * @param string $query Term to translate
141 * @pluginCmd [from language] [to language] [text to translate] Do a translation on Google
143 public function onCommandGt($from, $to, $query)
145 $url = 'http://ajax.googleapis.com/ajax/services/language/translate';
149 'langpair' => $from . '|' . $to
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);
161 $nick . ': ' . $json->responseData->translatedText
167 * Performs a Google Weather search for the given term.
169 * @param string $location Location to search for
173 * @pluginCmd [location] Show the weather for the specified location
175 public function onCommandGw($location)
177 $url = 'http://www.google.com/ig/api';
179 'weather' => $location,
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()
190 $condition = $xml->current_conditions->condition->attributes()->data[0];
191 $temp = $xml->current_conditions->temp_c->attributes()->data[0]
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);
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];
204 = 'Forecast: ' . $day .
205 ' - Min: ' . $min . '� C' .
206 ' - Max: ' . $max . '� C' .
208 $this->doPrivmsg($source, $msg);
211 $this->doPrivmsg($source, $xml->problem_cause->attributes()->data[0]);
216 * Performs a Google Maps search for the given term.
218 * @param string $location Location to search for
222 * @pluginCmd [location] Get the location from Google Maps to the location specified
224 public function onCommandGmap($location)
226 $location = utf8_encode($location);
227 $url = 'http://maps.google.com/maps/geo';
235 'key' => $this->_config['google.key']
237 $response = $this->http->get($url, $params);
238 $json = (array) $response->getContent();
239 $event = $this->getEvent();
240 $source = $event->getSource();
241 $nick = $event->getNick();
243 $qtd = count($json['Placemark']);
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] . ','
251 $msg = $nick . ' -> ' . $address . ' - ' . $url;
252 $this->doPrivmsg($source, $msg);
257 ', there are a lot of places with that query.' .
258 ' Try to be more specific!';
259 $this->doPrivmsg($source, $msg);
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]
266 $msg = $nick . ' -> ' . $address . ' - ' . $url;
267 $this->doPrivmsg($source, $msg);
269 $this->doPrivmsg($source, $nick . ', I found nothing.');
272 $this->doPrivmsg($source, $nick . ', we have a problem.');
277 * Perform a Google Convert query to convert a value from one metric to
280 * @param string $value Value to convert
281 * @param string $from Source metric
282 * @param string $to Destination metric
286 * @pluginCmd [value] [currency from] [currency to] Converts a monetary value from one currency to another
288 public function onCommandGconvert($value, $from, $to)
290 $url = 'http://www.google.com/finance/converter';
296 $response = $this->http->get($url, $params);
297 $contents = $response->getContent();
298 $event = $this->getEvent();
299 $source = $event->getSource();
300 $nick = $event->getNick();
303 '#<span class=bld>.*? ' . $to . '</span>#im',
308 $this->doPrivmsg($source, $nick . ', I can\'t do that.');
310 $str = str_replace('<span class=bld>', '', $matches[0]);
311 $str = str_replace($to . '</span>', '', $str);
313 = number_format($value, 2, ',', '.') . ' ' . $from .
314 ' => ' . number_format($str, 2, ',', '.') . ' ' . $to;
315 $this->doPrivmsg($source, $text);
318 $this->doPrivmsg($source, $nick . ', we had a problem.');
323 * Performs a Google search to convert a value from one unit to another.
325 * @param string $unit Source metric
326 * @param string $to Value to be converted
327 * @param string $unit2 Destination metric
331 * @pluginCmd [unit] [to] [unit2] Convert a value from one metric to another
333 public function onCommandConvert($unit, $to, $unit2)
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();
343 if (empty($contents)) {
346 $nick . ', sorry, I can\'t give you an answer right now.'
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;
357 array(chr(195), chr(151), chr(160)),
359 $children->item(0)->nodeValue
361 if ($children->length >= 3) {
363 .= '^' . $children->item(1)->nodeValue
364 . $children->item(2)->nodeValue;
370 $this->doPrivmsg($source, $nick . ': ' . $text);
372 $this->doPrivmsg($target, $nick . ', sorry I can\'t do that.');