diff options
author | Martin Schirrmacher <vdr.skinflatplus@schirrmacher.eu> | 2014-11-15 15:49:51 +0100 |
---|---|---|
committer | Martin Schirrmacher <vdr.skinflatplus@schirrmacher.eu> | 2014-11-15 15:49:51 +0100 |
commit | ff62c44e6cfdf5b015d04256341e950de76c6b92 (patch) | |
tree | 47b7bc2b3648a02878574f69b874cbf47b60beb1 /widgets | |
parent | 21e71933917740417e2c51286cb65a586f981a1e (diff) | |
download | skin-flatplus-ff62c44e6cfdf5b015d04256341e950de76c6b92.tar.gz skin-flatplus-ff62c44e6cfdf5b015d04256341e950de76c6b92.tar.bz2 |
add weather widget
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/weather/lib/forecast.io.php | 431 | ||||
-rw-r--r-- | widgets/weather/update_weather.php | 117 |
2 files changed, 548 insertions, 0 deletions
diff --git a/widgets/weather/lib/forecast.io.php b/widgets/weather/lib/forecast.io.php new file mode 100644 index 00000000..a3fdd0b2 --- /dev/null +++ b/widgets/weather/lib/forecast.io.php @@ -0,0 +1,431 @@ +<?php +/** + * Helper Class for forecast.io webservice + */ + +class ForecastIO{ + + private $api_key; + const API_ENDPOINT = 'https://api.forecast.io/forecast/'; + + /** + * Create a new instance + * + * @param String $api_key + */ + function __construct($api_key) { + + $this->api_key = $api_key; + + } + + + private function requestData($latitude, $longitude, $units, $language = 'en', $timestamp = false, $exclusions = false) { + + $validUnits = array('auto', 'us', 'si', 'ca', 'uk'); + + if (in_array($units, $validUnits)) { + + $request_url = self::API_ENDPOINT . + $this->api_key . '/' . + $latitude . ',' . $longitude . + '?units=' . $units . '&lang=' . $language . + ( $timestamp ? ',' . $timestamp : '' ) . + ( $exclusions ? '&exclude=' . $exclusions : '' ); + + /** + * Use Buffer to cache API-requests if initialized + * (if not, just get the latest data) + * + * More info: http://git.io/FoO2Qw + */ + + if(class_exists('Buffer')) { + $cache = new Buffer(); + $content = $cache->data($request_url); + } else { + $content = file_get_contents($request_url); + } + + } else { + + return false; + + } + + if (!empty($content)) { + + return json_decode($content); + + } else { + + return false; + + } + + + } + + /** + * Will return the current conditions + * + * @param float $latitude + * @param float $longitude + * @return \ForecastIOConditions|boolean + */ + function getCurrentConditions($latitude, $longitude, $units = 'auto', $language) { + + $data = $this->requestData($latitude, $longitude, $units, $language); + + if ($data !== false) { + + return new ForecastIOConditions($data->currently); + + } else { + + return false; + + } + + } + + /** + * Will return historical conditions for day of given timestamp + * + * @param float $latitude + * @param float $longitude + * @param int $timestamp + * @return \ForecastIOConditions|boolean + */ + function getHistoricalConditions($latitude, $longitude, $units = 'auto', $language, $timestamp) { + + $exclusions = 'currently,minutely,hourly,alerts,flags'; + + $data = $this->requestData($latitude, $longitude, $units, $language, $timestamp, $exclusions); + + if ($data !== false) { + + return new ForecastIOConditions($data->daily->data[0]); + + } else { + + return false; + + } + + } + + /** + * Will return conditions on hourly basis for today + * + * @param type $latitude + * @param type $longitude + * @return \ForecastIOConditions|boolean + */ + function getForecastToday($latitude, $longitude, $units = 'auto', $language) { + + $data = $this->requestData($latitude, $longitude, $units, $language); + + if ($data !== false) { + + $conditions = array(); + + $today = date('Y-m-d'); + + foreach ($data->hourly->data as $raw_data) { + + if (date('Y-m-d', $raw_data->time) == $today) { + + $conditions[] = new ForecastIOConditions($raw_data); + + } + + } + + return $conditions; + + } else { + + return false; + + } + + } + + + /** + * Will return daily conditions for next seven days + * + * @param float $latitude + * @param float $longitude + * @return \ForecastIOConditions|boolean + */ + function getForecastWeek($latitude, $longitude, $units = 'auto', $language) { + + $data = $this->requestData($latitude, $longitude, $units, $language); + + if ($data !== false) { + + $conditions = array(); + + foreach ($data->daily->data as $raw_data) { + + $conditions[] = new ForecastIOConditions($raw_data); + + } + + return $conditions; + + } else { + + return false; + + } + + } + + +} + + +/** + * Wrapper for get data by getters + */ +class ForecastIOConditions{ + + private $raw_data; + + function __construct($raw_data) { + + $this->raw_data = $raw_data; + + } + + /** + * Will return the temperature + * + * @return String + */ + function getTemperature() { + + return $this->raw_data->temperature; + + } + + /** + * get the min temperature + * + * only available for week forecast + * + * @return type + */ + function getMinTemperature() { + + return $this->raw_data->temperatureMin; + + } + + /** + * get max temperature + * + * only available for week forecast + * + * @return type + */ + function getMaxTemperature() { + + return $this->raw_data->temperatureMax; + + } + + /** + * get apparent temperature (heat index/wind chill) + * + * only available for current conditions + * + * @return type + */ + function getApparentTemperature() { + + return $this->raw_data->apparentTemperature; + + } + + /** + * Get the summary of the conditions + * + * @return String + */ + function getSummary() { + + return $this->raw_data->summary; + + } + + /** + * Get the icon of the conditions + * + * @return String + */ + function getIcon() { + + return $this->raw_data->icon; + + } + + /** + * Get the time, when $format not set timestamp else formatted time + * + * @param String $format + * @return String + */ + function getTime($format = null) { + + if (!isset($format)) { + + return $this->raw_data->time; + + } else { + + return date($format, $this->raw_data->time); + + } + + } + + /** + * Get the pressure + * + * @return String + */ + function getPressure() { + + return $this->raw_data->pressure; + + } + + /** + * Get the dew point + * + * Available in the current conditions + * + * @return String + */ + function getDewPoint() { + + return $this->raw_data->dewPoint; + + } + + /** + * get humidity + * + * @return String + */ + function getHumidity() { + + return $this->raw_data->humidity; + + } + + /** + * Get the wind speed + * + * @return String + */ + function getWindSpeed() { + + return $this->raw_data->windSpeed; + + } + + /** + * Get wind direction + * + * @return type + */ + function getWindBearing() { + + return $this->raw_data->windBearing; + + } + + /** + * get precipitation type + * + * @return type + */ + function getPrecipitationType() { + + return $this->raw_data->precipType; + + } + + /** + * get the probability 0..1 of precipitation type + * + * @return type + */ + function getPrecipitationProbability() { + + return $this->raw_data->precipProbability; + + } + + /** + * Get the cloud cover + * + * @return type + */ + function getCloudCover() { + + return $this->raw_data->cloudCover; + + } + + + + /** + * get sunrise time + * + * only available for week forecast + * + * @return type + */ + function getSunrise($format=null) { + + if (!isset($format)) { + + return $this->raw_data->sunriseTime; + + } else { + + return date($format, $this->raw_data->sunriseTime); + + } + + } + + /** + * get sunset time + * + * only available for week forecast + * + * @return type + */ + function getSunset($format=null) { + + if (!isset($format)) { + + return $this->raw_data->sunsetTime; + + } else { + + return date($format, $this->raw_data->sunsetTime); + + } + + } + +} +?> diff --git a/widgets/weather/update_weather.php b/widgets/weather/update_weather.php new file mode 100644 index 00000000..8d2d8be9 --- /dev/null +++ b/widgets/weather/update_weather.php @@ -0,0 +1,117 @@ +<?php + +/* + * USER CONFIG + */ +$city = "Berlin"; +$country = "DE"; +$locationSkin = "Berlin"; // location shown in skin +$units = 'si'; // Can be set to 'us', 'si', 'ca', 'uk' or 'auto' (see forecast.io API); default is auto +$degree_sign = "°C"; +$lang = 'de'; // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' +// We have only 1000 api calls per day, so please only do one update per day! +// Or request an own key for free at forecast.io +$api_key = '137f2d85a1f1db5762e5e073103541d2'; + +/* + * DO NOT CHANGE ANYTHING FROM HERE + */ +include('lib/forecast.io.php'); + +// delete old files +array_map('unlink', glob("weather.*")); + +// get lat & long from google maps +$MAPSURL = "http://maps.googleapis.com/maps/api/geocode/json?address=".$city.",".$country."&sensor=false"; +$json = file_get_contents($MAPSURL); +$data = json_decode($json, true); + +if( !isset($data['results'][0]) ) { + echo "no latitude and longitude find for: ".$city.",".$country." !\n"; + exit; +} +$latitude = $data['results'][0]['geometry']['location']['lat']; +$longitude = $data['results'][0]['geometry']['location']['lng']; + +// forecast query +$forecast = new ForecastIO($api_key); + +// get daily conditions for next 7 days +$conditions_week = $forecast->getForecastWeek($latitude, $longitude, $units, $lang); + +if( !$handle = fopen("weather.location", "w") ) { + print "can't create file!\n"; +} else { + fwrite($handle, $locationSkin); + fclose($handle); +} + + +$index = -1; +foreach($conditions_week as $conditions) { + $index++; + + if( !$handle = fopen("weather.".$index.".summary", "w") ) { + print "can't create file!\n"; + continue; + } + fwrite($handle, $conditions->getSummary()); + fclose($handle); + +/* + if( !$handle = fopen("weather.".$index.".temp", "w") ) { + print "can't create file!\n"; + continue; + } + // we only have min & max so we must calc + $temp = round(($conditions->getMinTemperature() + $conditions->getMaxTemperature()) / 2.0, 1); + fwrite($handle, $temp); + fwrite($handle, $degree_sign); + fclose($handle); + */ + if( !$handle = fopen("weather.".$index.".tempMin", "w") ) { + print "can't create file!\n"; + continue; + } + // we only have min & max so we must calc + $temp = round($conditions->getMinTemperature(), 1); + fwrite($handle, $temp); + fwrite($handle, $degree_sign); + fclose($handle); + + if( !$handle = fopen("weather.".$index.".tempMax", "w") ) { + print "can't create file!\n"; + continue; + } + // we only have min & max so we must calc + $temp = round($conditions->getMaxTemperature(), 1); + fwrite($handle, $temp); + fwrite($handle, $degree_sign); + fclose($handle); + + + if( !$handle = fopen("weather.".$index.".precipitation", "w") ) { + print "can't create file!\n"; + continue; + } + fwrite($handle, $conditions->getPrecipitationProbability()); + fclose($handle); + + if( !$handle = fopen("weather.".$index.".precipitationType", "w") ) { + print "can't create file!\n"; + continue; + } + if( $conditions->getPrecipitationProbability() > 0 ) + fwrite($handle, $conditions->getPrecipitationType()); + else + fwrite($handle, "none"); + fclose($handle); + + if( !$handle = fopen("weather.".$index.".icon", "w") ) { + print "can't create file!\n"; + continue; + } + fwrite($handle, $conditions->getIcon()); + fclose($handle); +} +?> |