Move Weatherbot example from Yahoo APIs to Openweathermap, fix bug in iterators used by ManaggeClassfiers not hahving a direction, a=chris

Chris Pollett [2020-08-23 15:Aug:rd]
Move Weatherbot example from Yahoo APIs to Openweathermap, fix bug in iterators used by ManaggeClassfiers not hahving a direction, a=chris
Filename
src/controllers/ClassifierController.php
src/controllers/components/AccountaccessComponent.php
src/examples/WeatherBot.php
src/models/VisitorModel.php
diff --git a/src/controllers/ClassifierController.php b/src/controllers/ClassifierController.php
index fa05a802f..3a64363c0 100644
--- a/src/controllers/ClassifierController.php
+++ b/src/controllers/ClassifierController.php
@@ -233,6 +233,7 @@ class ClassifierController extends Controller implements CrawlConstants
                  'COMPONENTS' => [[
                     'CRAWL_TIMESTAMP' => $crawl_time,
                     'WEIGHT' => 1.0,
+                    'DIRECTION' => 1,
                     'KEYWORDS' => $keywords]]]]]);
         return new MixArchiveBundleIterator($mix_time, $mix_time);
     }
diff --git a/src/controllers/components/AccountaccessComponent.php b/src/controllers/components/AccountaccessComponent.php
index d244554d4..25314e7df 100644
--- a/src/controllers/components/AccountaccessComponent.php
+++ b/src/controllers/components/AccountaccessComponent.php
@@ -288,7 +288,7 @@ class AccountaccessComponent extends Component
                     $is_bot_updated = true;
                 }
                 if (isset($_FILES['user_icon']['name']) &&
-                    $_FILES['user_icon']['name'] !="") {
+                    $_FILES['user_icon']['name'] != "") {
                     if (!in_array($_FILES['user_icon']['type'],
                         ['image/png', 'image/gif', 'image/jpeg'])) {
                         return $parent->redirectWithMessage(
@@ -327,7 +327,7 @@ class AccountaccessComponent extends Component
                 return $parent->redirectWithMessage(
                     tl('accountaccess_component_user_updated'),
                     ["edit", "edit_pass"]);
-          }
+        }
         return $data;
     }
     /**
diff --git a/src/examples/WeatherBot.php b/src/examples/WeatherBot.php
index c58ad966b..90c4cafcb 100644
--- a/src/examples/WeatherBot.php
+++ b/src/examples/WeatherBot.php
@@ -34,10 +34,11 @@ namespace seekquarry\yioop\examples\weatherbot;
  * This class demonstrates a simple Weather Chat Bot using the Yioop
  * ChatBot APIs for Yioop Discussion Groups.
  * To use this bot:
- * (1) Move this file to some folder of a web server you have access to.
+ * (0) Move this file to some folder of a web server you have access to.
  *     Denote by some_url the url of this folder. If you point your
  *     browser at this folder you should see a message that begins with:
  *     There was a configuration issue with your query.
+ * (1) Get an api key from openweathermap.org and fill it in below.
  * (2) Create a new Yioop User.
  * (3) Under Manage Accounts, click on the lock symbol next to Account Details
  * (4) Check the Bot User check box, click save.
@@ -102,7 +103,15 @@ class WeatherBot
     /**
      * Url of site that this bot gets weather information from
      */
-    const WEATHER_URL = "http://query.yahooapis.com/v1/public/yql";
+    const WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
+    /**
+     * API KEY of site that this bot gets weather information from
+     */
+    const API_KEY = "";
+    /**
+     * metric, default, or imperial units for temperature
+     */
+    const UNITS = "imperial";
     /**
      * Token given when setting up the bot in Yioop  for callback requests
      * This bots checks that a request from a Yioop Intance  sends
@@ -168,15 +177,13 @@ class WeatherBot
     function getWeather($args)
     {
         $result = $this->getRawWeatherData($args[0]);
-        $temp = empty($result->query->results->channel->item->condition->temp) ?
-            "" : $result->query->results->channel->item->condition->temp;
-        $text = empty($result->query->results->channel->item->condition->text) ?
-            "" : mb_strtolower(
-            $result->query->results->channel->item->condition->text);
+        $temp = $result->main->temp ?? "";
+        $text = empty($result->weather[0]->description) ?
+            "" : mb_strtolower($result->weather[0]->description);
         if (empty($temp) || empty($text)) {
             return "";
         }
-        return "The weather is $temp and $text in {$args[0]}.";
+        return "The weather is $temp with $text in {$args[0]}.";
     }
     /**
      * Return which location is warmer, the one stored in $args[0] or the
@@ -192,14 +199,12 @@ class WeatherBot
             return "";
         }
         $result = $this->getRawWeatherData($args[0]);
-        $tmp0 = empty($result->query->results->channel->item->condition->temp) ?
-            "" : $result->query->results->channel->item->condition->temp;
+        $tmp0 = $result->main->temp ?? "";
         if (empty($tmp0)) {
             return "";
         }
         $result = $this->getRawWeatherData($args[1]);
-        $tmp1 = empty($result->query->results->channel->item->condition->temp) ?
-            "" : $result->query->results->channel->item->condition->temp;
+        $tmp1 = $result->main->temp ?? "";
         if (empty($tmp1)) {
             return "";
         }
@@ -221,11 +226,8 @@ class WeatherBot
      */
     function getRawWeatherData($location)
     {
-        $yql_query = "select * from weather.forecast where woeid in
-            (select woeid from geo.places(1) where text='" . $location
-            ."')";
-        $url = self::WEATHER_URL . "?q=" .
-            urlencode($yql_query) . "&format=json";
+        $url = self::WEATHER_URL . "?APPID=". self::API_KEY . "&units=".
+            self::UNITS . "&q=" . urlencode($location);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
diff --git a/src/models/VisitorModel.php b/src/models/VisitorModel.php
index 3c7390310..66a7bf1d2 100644
--- a/src/models/VisitorModel.php
+++ b/src/models/VisitorModel.php
@@ -60,9 +60,9 @@ class VisitorModel extends Model
     public function getVisitor($ip_address, $page_name = 'captcha_time_out')
     {
         $db = $this->db;
-        $sql = "SELECT * FROM VISITOR WHERE ADDRESS=:address
-            AND PAGE_NAME=:page_name " . $db->limitOffset(1);
-        $result = $this->db->execute($sql, [":address" => $ip_address,
+        $sql = "SELECT * FROM VISITOR WHERE ADDRESS = :address
+            AND PAGE_NAME = :page_name " . $db->limitOffset(1);
+        $result = $db->execute($sql, [":address" => $ip_address,
             ":page_name" => $page_name]);
         if (!$result || !$row = $db->fetchArray($result)) {
             return false;
ViewGit