]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
start getting locations from remote services
authorEvan Prodromou <evan@status.net>
Wed, 16 Sep 2009 15:46:10 +0000 (11:46 -0400)
committerEvan Prodromou <evan@status.net>
Thu, 22 Oct 2009 02:47:19 +0000 (22:47 -0400)
lib/default.php
lib/location.php

index 30e43eefbd65394762bd0a5d4d447603b75997a3..c896d5408470a2ee0b56d9ed4c3f1867c6bf37ce 100644 (file)
@@ -229,4 +229,6 @@ $default =
         array('contentlimit' => null),
         'http' =>
         array('client' => 'curl'), // XXX: should this be the default?
+        'location' =>
+        array('namespace' => 1), // 1 = geonames, 2 = Yahoo Where on Earth
         );
index cbd03f33a439164b473685d42a0c215f976db766..f4ce7f67ac74138bb112502b2a77f5d11836839c 100644 (file)
@@ -50,11 +50,63 @@ class Location
 
     var $names;
 
-    static function fromName($name, $language=null)
+    const geonames = 1;
+    const whereOnEarth = 2;
+
+    static function fromName($name, $language=null, $location_ns=null)
     {
         if (is_null($language)) {
             $language = common_language();
         }
+        if (is_null($location_ns)) {
+            $location_ns = common_config('location', 'namespace');
+        }
+
+        $location = null;
+
+        if (Event::handle('LocationFromName', array($name, $language, $location_ns, &$location))) {
+
+            switch ($location_ns) {
+             case Location::geonames:
+                return Location::fromGeonamesName($name, $language);
+                break;
+             case Location::whereOnEarth:
+                return Location::fromWhereOnEarthName($name, $language);
+                break;
+            }
+        }
+
+        return $location;
+    }
+
+    static function fromGeonamesName($name, $language)
+    {
+        $location = null;
+        $client = HTTPClient::start();
+
+        // XXX: break down a name by commas, narrow by each
+
+        $str = http_build_query(array('maxRows' => 1,
+                                      'q' => $name,
+                                      'lang' => $language,
+                                      'type' => 'json'));
+
+        $result = $client->get('http://ws.geonames.org/search?'.$str);
+
+        if ($result->code == "200") {
+            $rj = json_decode($result->body);
+            if (count($rj['geonames']) > 0) {
+                $n = $rj['geonames'][0];
+                $location = new Location();
+                $location->lat = $n->lat;
+                $location->lon = $n->lon;
+                $location->name = $n->name;
+                $location->location_id = $n->geonameId;
+                $location->location_ns = Location:geonames;
+            }
+        }
+
+        return $location;
     }
 
     static function fromId($location_id, $location_ns = null)