]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Irc/extlib/phergie/Phergie/Plugin/Google.php
Revert "Merged in Phergie changes"
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Google.php
index d2a9d4dce991854c248ab4067ac7e5b0a00fbf1b..faa98f926abe0f089040bba8265a12c4566b0026 100644 (file)
  * @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.
      *
@@ -44,8 +59,11 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
     {
         $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');
     }
 
     /**
@@ -55,6 +73,8 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      *
      * @return void
      * @todo Implement use of URL shortening here
+     *
+     * @pluginCmd [query] do a search on google
      */
     public function onCommandG($query)
     {
@@ -63,7 +83,7 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
             '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();
@@ -90,6 +110,8 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      * @param string $query Search term
      *
      * @return void
+     *
+     * @pluginCmd [query] Do a search on Google and count the results
      */
     public function onCommandGc($query)
     {
@@ -98,7 +120,7 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
             '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();
@@ -124,6 +146,8 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      * @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)
     {
@@ -133,7 +157,7 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
             '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();
@@ -156,16 +180,18 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      *        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();
@@ -254,6 +280,8 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      * @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)
     {
@@ -266,13 +294,13 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
         $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);
@@ -317,6 +345,8 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      * @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)
     {
@@ -326,21 +356,29 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
             '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.');
         }
     }
 
@@ -357,7 +395,7 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
     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();
@@ -396,61 +434,49 @@ class Phergie_Plugin_Google extends Phergie_Plugin_Abstract
      *
      * @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);
             }