gnu-social/plugins/Irc/extlib/phergie/Phergie/Plugin/Google.php

486 lines
16 KiB
PHP
Raw Normal View History

2010-06-16 09:55:39 +09:00
<?php
/**
2010-07-26 08:04:12 +09:00
* Phergie
2010-06-16 09:55:39 +09:00
*
* 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
*
2010-07-26 08:04:12 +09:00
* @category Phergie
2010-06-16 09:55:39 +09:00
* @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
*/
/**
2010-07-26 08:04:12 +09:00
* Provides commands used to access several services offered by Google
* including search, translation, weather, maps, and currency and general
2010-06-16 09:55:39 +09:00
* value unit conversion.
*
2010-07-26 08:04:12 +09:00
* @category Phergie
2010-06-16 09:55:39 +09:00
* @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
2010-07-26 08:04:12 +09:00
* @uses Phergie_Plugin_Temperature pear.phergie.org
*
* @pluginDesc Provide access to some Google services
2010-06-16 09:55:39 +09:00
*/
class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
{
/**
* HTTP plugin
*
* @var Phergie_Plugin_Http
*/
protected $http;
/**
* Language for Google Services
*/
protected $lang;
2010-06-16 09:55:39 +09:00
/**
* Checks for dependencies.
*
* @return void
*/
public function onLoad()
{
$plugins = $this->getPluginHandler();
$plugins->getPlugin('Command');
$this->http = $plugins->getPlugin('Http');
$plugins->getPlugin('Help')->register($this);
2010-07-26 08:04:12 +09:00
$plugins->getPlugin('Weather');
$this->lang = $this->getConfig('google.lang', 'en');
2010-06-16 09:55:39 +09:00
}
/**
* 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
2010-06-16 09:55:39 +09:00
*/
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);
2010-06-16 09:55:39 +09:00
$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
2010-06-16 09:55:39 +09:00
*/
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);
2010-06-16 09:55:39 +09:00
$json = $response->getContent()->responseData->cursor;
$count = $json->estimatedResultCount;
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
if ($count) {
$msg
2010-07-26 08:04:12 +09:00
= $nick . ': ' .
number_format($count, 0) .
2010-06-16 09:55:39 +09:00
' 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
2010-07-26 08:04:12 +09:00
* @param string $to Language to which the search term should be
2010-06-16 09:55:39 +09:00
* translated
* @param string $query Term to translate
*
* @return void
*
* @pluginCmd [from language] [to language] [text to translate] Do a translation on Google
2010-06-16 09:55:39 +09:00
*/
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);
2010-06-16 09:55:39 +09:00
$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(
2010-07-26 08:04:12 +09:00
$source,
2010-06-16 09:55:39 +09:00
$nick . ': ' . $json->responseData->translatedText
);
}
}
/**
* Performs a Google Weather search for the given term.
2010-07-26 08:04:12 +09:00
*
2010-06-16 09:55:39 +09:00
* @param string $location Location to search for
2010-07-26 08:04:12 +09:00
* @param int $offset Optional day offset from the current date
* between 0 and 3 to get the forecast
2010-06-16 09:55:39 +09:00
*
* @return void
*
* @pluginCmd [location] Show the weather for the specified location
2010-06-16 09:55:39 +09:00
*/
2010-07-26 08:04:12 +09:00
public function onCommandGw($location, $offset = null)
2010-06-16 09:55:39 +09:00
{
$url = 'http://www.google.com/ig/api';
$params = array(
'weather' => $location,
'hl' => $this->lang,
2010-06-16 09:55:39 +09:00
'oe' => 'UTF-8'
);
$response = $this->http->get($url, $params);
2010-06-16 09:55:39 +09:00
$xml = $response->getContent()->weather;
2010-07-26 08:04:12 +09:00
$event = $this->getEvent();
$source = $event->getSource();
$msg = '';
if ($event->isInChannel()) {
$msg .= $event->getNick() . ': ';
}
if (isset($xml->problem_cause)) {
$msg .= $xml->problem_cause->attributes()->data[0];
2010-06-16 09:55:39 +09:00
$this->doPrivmsg($source, $msg);
2010-07-26 08:04:12 +09:00
return;
}
2010-06-16 09:55:39 +09:00
2010-07-26 08:04:12 +09:00
$temperature = $this->plugins->getPlugin('Temperature');
$forecast = $xml->forecast_information;
$city = $forecast->city->attributes()->data[0];
$zip = $forecast->postal_code->attributes()->data[0];
if ($offset !== null) {
$offset = (int) $offset;
if ($offset < 0) {
$this->doNotice($source, 'Past weather data is not available');
return;
} elseif ($offset > 3) {
$this->doNotice($source, 'Future weather data is limited to 3 days from today');
return;
}
$linha = $xml->forecast_conditions[$offset];
$low = $linha->low->attributes()->data[0];
$high = $linha->high->attributes()->data[0];
$units = $forecast->unit_system->attributes()->data[0];
$condition = $linha->condition->attributes()->data[0];
$day = $linha->day_of_week->attributes()->data[0];
$date = ($offset == 0) ? time() : strtotime('next ' . $day);
$day = ucfirst($day) . ' ' . date('n/j/y', $date);
if ($units == 'US') {
$lowF = $low;
$lowC = $temperature->convertFahrenheitToCelsius($low);
$highF = $high;
$highC = $temperature->convertFahrenheitToCelsius($high);
} else {
$lowC = $low;
$lowF = $temperature->convertCelsiusToFahrenheit($lowC);
$highC = $high;
$highF = $temperature->convertCelsiusToFahrenheit($high);
2010-06-16 09:55:39 +09:00
}
2010-07-26 08:04:12 +09:00
$msg .= 'Forecast for ' . $city . ' (' . $zip . ')'
. ' on ' . $day . ' ::'
. ' Low: ' . $lowF . 'F/' . $lowC . 'C,'
. ' High: ' . $highF . 'F/' . $highC . 'C,'
. ' Conditions: ' . $condition;
2010-06-16 09:55:39 +09:00
} else {
2010-07-26 08:04:12 +09:00
$conditions = $xml->current_conditions;
$condition = $conditions->condition->attributes()->data[0];
$tempF = $conditions->temp_f->attributes()->data[0];
$tempC = $conditions->temp_c->attributes()->data[0];
$humidity = $conditions->humidity->attributes()->data[0];
$wind = $conditions->wind_condition->attributes()->data[0];
$time = $forecast->current_date_time->attributes()->data[0];
$time = date('n/j/y g:i A', strtotime($time)) . ' +0000';
$hiF = $temperature->getHeatIndex($tempF, $humidity);
$hiC = $temperature->convertFahrenheitToCelsius($hiF);
$msg .= 'Weather for ' . $city . ' (' . $zip . ') -'
. ' Temperature: ' . $tempF . 'F/' . $tempC . 'C,'
. ' ' . $humidity . ','
. ' Heat Index: ' . $hiF . 'F/' . $hiC . 'C,'
. ' Conditions: ' . $condition . ','
. ' Updated: ' . $time;
2010-06-16 09:55:39 +09:00
}
2010-07-26 08:04:12 +09:00
$this->doPrivmsg($source, $msg);
2010-06-16 09:55:39 +09:00
}
/**
* 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
2010-06-16 09:55:39 +09:00
*/
public function onCommandGmap($location)
2010-07-26 08:04:12 +09:00
{
2010-07-01 01:33:29 +09:00
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
2010-07-26 08:04:12 +09:00
2010-06-16 09:55:39 +09:00
$location = utf8_encode($location);
$url = 'http://maps.google.com/maps/geo';
$params = array(
'q' => $location,
'output' => 'json',
'gl' => $this->lang,
2010-06-16 09:55:39 +09:00
'sensor' => 'false',
'oe' => 'utf8',
'mrt' => 'all',
2010-07-01 01:33:29 +09:00
'key' => $this->getConfig('google.key')
2010-06-16 09:55:39 +09:00
);
$response = $this->http->get($url, $params);
2010-07-01 01:33:29 +09:00
$json = $response->getContent();
2010-06-16 09:55:39 +09:00
if (!empty($json)) {
2010-07-01 01:33:29 +09:00
$qtd = count($json->Placemark);
2010-06-16 09:55:39 +09:00
if ($qtd > 1) {
if ($qtd <= 3) {
2010-07-01 01:33:29 +09:00
foreach ($json->Placemark as $places) {
$xy = $places->Point->coordinates;
$address = utf8_decode($places->address);
2010-07-26 08:04:12 +09:00
$url = 'http://maps.google.com/maps?sll=' . $xy[1] . ','
2010-06-16 09:55:39 +09:00
. $xy[0] . '&z=15';
$msg = $nick . ' -> ' . $address . ' - ' . $url;
$this->doPrivmsg($source, $msg);
}
} else {
$msg
2010-07-26 08:04:12 +09:00
= $nick .
', there are a lot of places with that query.' .
2010-06-16 09:55:39 +09:00
' Try to be more specific!';
$this->doPrivmsg($source, $msg);
}
} elseif ($qtd == 1) {
2010-07-01 01:33:29 +09:00
$xy = $json->Placemark[0]->Point->coordinates;
$address = utf8_decode($json->Placemark[0]->address);
2010-07-26 08:04:12 +09:00
$url = 'http://maps.google.com/maps?sll=' . $xy[1] . ',' . $xy[0]
2010-06-16 09:55:39 +09:00
. '&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.');
}
}
/**
2010-07-26 08:04:12 +09:00
* Perform a Google Convert query to convert a value from one metric to
2010-06-16 09:55:39 +09:00
* 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
2010-06-16 09:55:39 +09:00
*/
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);
2010-06-16 09:55:39 +09:00
$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.');
2010-06-16 09:55:39 +09:00
}
}
/**
* Performs a Google search to convert a value from one unit to another.
*
2010-07-01 01:33:29 +09:00
* @param string $query Query of the form "[quantity] [unit] to [unit2]"
2010-06-16 09:55:39 +09:00
*
* @return void
*
2010-07-26 08:04:12 +09:00
* @pluginCmd [quantity] [unit] to [unit2] Convert a value from one
2010-07-01 01:33:29 +09:00
* metric to another
2010-06-16 09:55:39 +09:00
*/
2010-07-01 01:33:29 +09:00
public function onCommandConvert($query)
2010-06-16 09:55:39 +09:00
{
2010-07-01 01:33:29 +09:00
$url = 'http://www.google.com/search?q=' . urlencode($query);
$response = $this->http->get($url);
2010-06-16 09:55:39 +09:00
$contents = $response->getContent();
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
2010-07-01 01:33:29 +09:00
if ($response->isError()) {
$code = $response->getCode();
$message = $response->getMessage();
$this->doNotice($nick, 'ERROR: ' . $code . ' ' . $message);
2010-06-16 09:55:39 +09:00
return;
}
2010-07-01 01:33:29 +09:00
$start = strpos($contents, '<h3 class=r>');
if ($start !== false) {
$end = strpos($contents, '</b>', $start);
$text = strip_tags(substr($contents, $start, $end - $start));
$text = str_replace(
array(chr(195), chr(151), chr(160)),
array('x', '', ' '),
$text
);
2010-06-16 09:55:39 +09:00
}
if (isset($text)) {
$this->doPrivmsg($source, $nick . ': ' . $text);
} else {
2010-07-01 01:33:29 +09:00
$this->doNotice($nick, 'Sorry I couldn\'t find an answer.');
2010-06-16 09:55:39 +09:00
}
}
2010-07-26 08:04:12 +09:00
/**
* Returns the first definition of a Google Dictionary search.
*
* @param string $query Word to get the definition
*
* @return void
* @todo Implement use of URL shortening here
*
* @pluginCmd [query] do a search of a definition on Google Dictionary
2010-07-26 08:04:12 +09:00
*/
public function onCommandDefine($query)
{
$query = urlencode($query);
$url = 'http://www.google.com/dictionary/json?callback=result'.
'&q='.$query.'&sl='.$this->lang.'&tl='.$this->lang.
'&restrict=pr,de';
$json = file_get_contents($url);
//Remove some garbage from the json
$json = str_replace(array("result(", ",200,null)"), "", $json);
2010-07-26 08:04:12 +09:00
//Awesome workaround to remove a lot of slashes from json
2010-07-26 08:04:12 +09:00
$json = str_replace('"', '¿?¿', $json);
$json = strip_tags(stripcslashes($json));
$json = str_replace('"', "'", $json);
$json = str_replace('¿?¿', '"', $json);
2010-07-26 08:04:12 +09:00
$json = json_decode($json);
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
if (!empty($json->webDefinitions)){
$results = count($json->webDefinitions[0]->entries);
$more = $results > 1 ? ($results-1).' ' : NULL;
$lang_code = substr($this->lang, 0, 2);
$msg =
$nick . ': ' .
$json->webDefinitions[0]->entries[0]->terms[0]->text .
' - You can find more '.$more.'results at '.
'http://www.google.com/dictionary?aq=f&langpair='.
$lang_code.'%7C'.$lang_code.'&q='.$query.'&hl='.$lang_code;
2010-07-26 08:04:12 +09:00
$this->doPrivmsg($source, $msg);
}else{
if ($this->lang != 'en'){
$temp = $this->lang;
$this->lang = 'en';
2010-07-26 08:04:12 +09:00
$this->onCommandDefine($query);
$this->lang = $temp;
}else{
2010-07-26 08:04:12 +09:00
$msg = $nick . ': No results for this query.';
$this->doPrivmsg($source, $msg);
}
}
}
2010-06-16 09:55:39 +09:00
}