* @uses Phergie_Plugin_Command pear.phergie.org
* @uses Phergie_Plugin_Http pear.phergie.org
* @uses Phergie_Plugin_Temperature 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;
+
+ /**
+ * Language for Google Services
+ */
+ protected $lang;
+
/**
* Checks for dependencies.
*
{
$plugins = $this->getPluginHandler();
$plugins->getPlugin('Command');
- $plugins->getPlugin('Http');
+ $this->http = $plugins->getPlugin('Http');
+ $plugins->getPlugin('Help')->register($this);
$plugins->getPlugin('Weather');
+
+ $this->lang = $this->getConfig('google.lang', 'en');
}
/**
*
* @return void
* @todo Implement use of URL shortening here
+ *
+ * @pluginCmd [query] do a search on google
*/
public function onCommandG($query)
{
'v' => '1.0',
'q' => $query
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$json = $response->getContent()->responseData;
$event = $this->getEvent();
$source = $event->getSource();
* @param string $query Search term
*
* @return void
+ *
+ * @pluginCmd [query] Do a search on Google and count the results
*/
public function onCommandGc($query)
{
'v' => '1.0',
'q' => $query
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$json = $response->getContent()->responseData->cursor;
$count = $json->estimatedResultCount;
$event = $this->getEvent();
* @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)
{
'q' => $query,
'langpair' => $from . '|' . $to
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$json = $response->getContent();
$event = $this->getEvent();
$source = $event->getSource();
* between 0 and 3 to get the forecast
*
* @return void
+ *
+ * @pluginCmd [location] Show the weather for the specified location
*/
public function onCommandGw($location, $offset = null)
{
$url = 'http://www.google.com/ig/api';
$params = array(
'weather' => $location,
- 'hl' => $this->getConfig('google.lang', 'en'),
+ 'hl' => $this->lang,
'oe' => 'UTF-8'
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$xml = $response->getContent()->weather;
$event = $this->getEvent();
* @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)
{
$params = array(
'q' => $location,
'output' => 'json',
- 'gl' => $this->getConfig('google.lang', 'en'),
+ 'gl' => $this->lang,
'sensor' => 'false',
'oe' => 'utf8',
'mrt' => 'all',
'key' => $this->getConfig('google.key')
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$json = $response->getContent();
if (!empty($json)) {
$qtd = count($json->Placemark);
* @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)
{
'from' => $from,
'to' => $to
);
- $response = $this->plugins->http->get($url, $params);
+ $response = $this->http->get($url, $params);
$contents = $response->getContent();
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
if ($contents) {
- libxml_use_internal_errors(true);
- $doc = new DOMDocument;
- $doc->loadHTML($contents);
- libxml_clear_errors();
- $xpath = new DOMXPath($doc);
- $result = $xpath->query('//div[@id="currency_converter_result"]');
- $div = $result->item(0);
- $text = rtrim($div->textContent);
- $this->doPrivmsg($source, $text);
+ 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.');
}
}
public function onCommandConvert($query)
{
$url = 'http://www.google.com/search?q=' . urlencode($query);
- $response = $this->plugins->http->get($url);
+ $response = $this->http->get($url);
$contents = $response->getContent();
$event = $this->getEvent();
$source = $event->getSource();
*
* @return void
* @todo Implement use of URL shortening here
+ *
+ * @pluginCmd [query] do a search of a definition on Google Dictionary
*/
public function onCommandDefine($query)
{
- $lang = $this->getConfig('google.lang', 'en');
- $url = 'http://www.google.com/dictionary/json';
- $params = array(
- 'callback' => 'result',
- 'q' => $query,
- 'sl' => $lang,
- 'tl' => $lang,
- 'restrict' => 'pr,de'
- );
- $response = $this->plugins->http->get($url, $params);
- $json = $response->getContent();
+ $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);
- // Remove some garbage from the JSON and decode it
- $json = str_replace(array('result(', ',200,null)'), '', $json);
+ //Awesome workaround to remove a lot of slashes from json
$json = str_replace('"', '¿?¿', $json);
$json = strip_tags(stripcslashes($json));
$json = str_replace('"', "'", $json);
$json = str_replace('¿?¿', '"', $json);
+
$json = json_decode($json);
$event = $this->getEvent();
$source = $event->getSource();
$nick = $event->getNick();
- if (!empty($json->webDefinitions)) {
- $results = 0;
- foreach ($json->primaries[0]->entries as $entry) {
- if ($entry->type == 'meaning') {
- $results++;
- if (empty($text)) {
- foreach ($entry->terms as $term) {
- if ($term->type == 'text') {
- $text = trim($term->text);
- }
- }
- }
- }
- }
- $more = $results > 1 ? ($results - 1) . ' ' : '';
- $lang_code = substr($lang, 0, 2);
- $msg = $nick . ': ' . $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;
+ 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;
$this->doPrivmsg($source, $msg);
- } else {
- if ($lang != 'en'){
- $lang = 'en';
+ }else{
+ if ($this->lang != 'en'){
+ $temp = $this->lang;
+ $this->lang = 'en';
$this->onCommandDefine($query);
- } else {
+ $this->lang = $temp;
+ }else{
$msg = $nick . ': No results for this query.';
$this->doPrivmsg($source, $msg);
}