*/
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) {
$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'],
'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() {
$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
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'],
'$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')
));
$curweather_units = get_pconfig(local_user(), 'curweather', 'curweather_units');
$appid = get_config('curweather','appid');
if ($appid=="") {
- $noappidtext = t('No APPID found, please contact your admin to optain one.');
+ $noappidtext = t('No APPID found, please contact your admin to obtain one.');
} else {
$noappidtext = '';
}
'$noappidtext' => $noappidtext,
'$info' => t('Enter either the name of your location or the zip code.'),
'$curweather_loc' => array( 'curweather_loc', t('Your Location'), $curweather_loc, t('Identifier of your location (name or zip code), e.g. <em>Berlin,DE</em> or <em>14476,DE</em>.') ),
- '$curweather_units' => array( 'curweather_units', t('Units'), $curweather_units, t('select if the temperatur should be displayed in °C or °F'), array('metric'=>'°C', 'imperial'=>'°F')),
+ '$curweather_units' => array( 'curweather_units', t('Units'), $curweather_units, t('select if the temperature should be displayed in °C or °F'), array('metric'=>'°C', 'imperial'=>'°F')),
'$enabled' => array( 'curweather_enable', t('Show weather data'), $enable, '')
));
return;