]> git.mxchange.org Git - friendica-addons.git/blobdiff - curweather/curweather.php
Merge pull request #332 from tobiasd/20151214-translations
[friendica-addons.git] / curweather / curweather.php
index cfe66b7e03c3f7f0690c5273b1f9ac77612e22d4..e28cc2b3044adc6978128aedf631b055199b64fd 100644 (file)
  */
 
 require_once('include/network.php');
-include_once('include/text.php');
+require_once("mod/proxy.php");
+require_once('include/text.php');
 
 //  get the weather data from OpenWeatherMap
-function getWeather( $loc, $units='metric', $lang='en', $appid='') {
+function getWeather( $loc, $units='metric', $lang='en', $appid='', $cachetime=0) {
     $url = "http://api.openweathermap.org/data/2.5/weather?q=".$loc."&appid=".$appid."&lang=".$lang."&units=".$units."&mode=xml";
+    $cached = Cache::get('curweather'.md5($url));
+    $now = new DateTime();
+    if (!is_null($cached)) {
+       $cdate = get_pconfig(local_user(), 'curweather', 'last');
+       $cached = unserialize($cached);
+       if ($cdate + $cachetime > $now->getTimestamp()) {
+           return $cached;
+       }
+    }
     try {
        $res = new SimpleXMLElement(fetch_url($url));
     } catch (Exception $e) {
@@ -28,7 +38,12 @@ function getWeather( $loc, $units='metric', $lang='en', $appid='') {
        $tunit = '°F';
        $wunit = 'mph';
     }
-    return array(
+    if ( trim((string)$res->weather['value']) == trim((string)$res->clouds['name']) ) {
+       $desc = (string)$res->clouds['name'];
+    } else {
+       $desc = (string)$res->weather['value'].', '.(string)$res->clouds['name'];
+    }
+    $r = array(
        'city'=> (string) $res->city['name'][0],
        'country' => (string) $res->city->country[0],
        'lat' => (string) $res->city->coord['lat'],
@@ -36,10 +51,14 @@ function getWeather( $loc, $units='metric', $lang='en', $appid='') {
        'temperature' => (string) $res->temperature['value'][0].$tunit,
        'pressure' => (string) $res->pressure['value'].(string)$res->pressure['unit'],
        'humidity' => (string) $res->humidity['value'].(string)$res->humidity['unit'],
-       'descripion' => (string)$res->weather['value'].', '.(string)$res->clouds['name'],
+       'descripion' => $desc,
        'wind' => (string)$res->wind->speed['name'].' ('.(string)$res->wind->speed['value'].$wunit.')',
-       'update' => (string)$res->lastupdate['value']
+       'update' => (string)$res->lastupdate['value'],
+       'icon' => (string)$res->weather['icon']
     );
+    set_pconfig(local_user(), 'curweather', 'last', $now->getTimestamp());
+    Cache::set('curweather'.md5($url), serialize($r), CACHE_HOUR);
+    return $r;
 }
 
 function curweather_install() {
@@ -61,9 +80,10 @@ function curweather_network_mod_init(&$fk_app,&$b) {
 
     $fk_app->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
 
-    // the OpenWeatherMap-PHP-APIlib does all the work here
-    // the $rpt value is needed for location
+    // $rpt value is needed for location
     // $lang will be taken from the browser session to honour user settings
+    // TODO $lang does not work if the default settings are used
+    //      and not all response strings are translated
     // $units can be set in the settings by the user
     // $appid is configured by the admin in the admin panel
     // those parameters will be used to get: cloud status, temperature, preassure
@@ -80,16 +100,16 @@ function curweather_network_mod_init(&$fk_app,&$b) {
     if ($units==="")
        $units = 'metric';
     $ok = true;
-    
-    $res = getWeather($rpt, $units, $lang, $appid);
+
+    $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
     if ($res===false)
        $ok = false;
-    // TODO Caching
 
     if ($ok) {
        $t = get_markup_template("widget.tpl", "addon/curweather/" );
        $curweather = replace_macros ($t, array(
            '$title' => t("Current Weather"),
+           '$icon' => proxy_url('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
            '$city' => $res['city'],
            '$lon' => $res['lon'],
            '$lat' => $res['lat'],
@@ -98,7 +118,7 @@ function curweather_network_mod_init(&$fk_app,&$b) {
            '$relhumidity' => array('caption'=>t('Relative Humidity'), 'val'=>$res['humidity']),
            '$pressure' => array('caption'=>t('Pressure'), 'val'=>$res['pressure']),
            '$wind' => array('caption'=>t('Wind'), 'val'=> $res['wind']),
-           '$lastupdate' => t('Last Updated').': '.$res['update'],
+           '$lastupdate' => t('Last Updated').': '.$res['update'].'UTC',
            '$databy' =>  t('Data by'),
            '$showonmap' => t('Show on map')
        ));