function forecast_render_element_content() { global $template, $picture, $page, $conf; load_language('plugin.lang', FORECAST_PATH); if (empty($page['image_id']) and !is_numeric($page['image_id'])) { return; } // Load coordinates and date_creation from picture $query = "SELECT latitude,longitude,date FROM forecast WHERE id='" . $page['image_id'] . "';"; //FIXME LIMIT 1 ? $result = pwg_query($query); $row = pwg_db_fetch_assoc($result); if (!isset($row) or !isset($row['latitude']) or empty($row['latitude']) or !isset($row['longitude']) or empty($row['longitude']) or !isset($row['date']) or empty($row['date'])) { return; } $lat = $row['latitude']; $lon = $row['longitude']; $date = $row['date']; // Load parameter, fallback to default if unset $fc_height = isset($conf['forecast_conf']['height']) ? $conf['forecast_conf']['height'] : '200'; $fc_header = isset($conf['forecast_conf']['link']) ? $conf['forecast_conf']['link'] : 'Overcast'; $fc_header_css = isset($conf['forecast_conf']['linkcss']) ? $conf['forecast_conf']['linkcss'] : ''; $fc_show_link = isset($conf['forecast_conf']['show']) ? $conf['forecast_conf']['show'] : 'true'; $fc_api_key = isset($conf['forecast_conf']['api_key']) ? $conf['forecast_conf']['api_key'] : ''; if (strlen($fc_header_css) != 0) { $fc_css = "style='" . $fc_header_css . "'"; } $fc_link = "http://forecast.io/#/f/" . $lat . "," . $lon; // Init Forecast.io lib include 'lib/forecast.io.php'; // Can be set to 'us', 'si', 'ca', 'uk' or 'auto' (see forecast.io API); default is auto // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' $fc_unit = isset($conf['forecast_conf']['unit']) ? $conf['forecast_conf']['unit'] : 'auto'; $fc_lang = isset($conf['forecast_conf']['lang']) ? $conf['forecast_conf']['lang'] : 'en'; /* Do we have a Forecast.io API key */ if (strlen($fc_api_key) != 0) { // Make a request to Forecast.io using the user supply API, proxy set to false $forecast = new ForecastIO($fc_api_key, $fc_unit, $fc_lang, false); } else { /** * Make a request to https://forecast-xbgmsharp.rhcloud.com * to non disclose the Forecast.io API key, proxy set to true * Source code at https://github.com/xbgmsharp/nodejs-forecast **/ $forecast = new ForecastIO($fc_api_key, $fc_unit, $fc_lang, true); } $condition = $forecast->getHistoricalConditions($lat, $lon, $date); if (!isset($condition) or $condition === 'false') { return; } //print_r($condition); // Parse weather condition to human readable $condition = parseCondition($condition); // Select the template $template->set_filenames(array('forecast_content' => dirname(__FILE__) . "/template/picture.tpl")); // Assign the template variables $template->assign(array('FORECAST_HEIGHT' => $fc_height, 'FORECAST_PATH' => embellish_url(get_gallery_home_url() . FORECAST_PATH), 'FORECAST_NAME' => $fc_header, 'FORECAST_NAME_CSS' => $fc_header_css, 'FORECAST_SHOW_LINK' => $fc_show_link, 'FORECAST_LINK' => $fc_link, 'FORECAST_DATA' => $condition)); // Return the rendered html $forecast_content = $template->parse('forecast_content', true); return $forecast_content; }
function getweather($api_key, $latitude, $longitude, $units = "auto", $lang = "en") { $forecast = new ForecastIO($api_key, $units, $lang); //This is array holding CURRENT weather data i.e. weather right now $now = array(); $yesterday = array(); //This holds today's, tomorrow's and the day after's weather data $twoday = array(); //create a timestamp of yestday, so we can retrieve its weather $today = new DateTime("now"); $yesterdaytime = $today->sub(new DateInterval('P1D'))->getTimeStamp(); //echo $yesterday; //Get current conditions and give to array too $condition = $forecast->getCurrentConditions($latitude, $longitude); echo "current: " . $condition->getTemperature() . "\n"; echo "percent rain: " . $condition->getPrecipitationProbability() . "\n"; echo "humidity: " . $condition->getHumidity() . "\n"; echo "rain fall: " . $condition->getPrecipitationIntensity(); $now['temp'] = $condition->getTemperature(); $now['chancerain'] = $condition->getPrecipitationProbability(); $now['humidity'] = $condition->getHumidity(); $now['summary'] = $condition->getSummary(); $now['rainfall'] = $condition->getPrecipitationIntensity(); //If precip probability > 0 then get precip type too if ($now['chancerain'] > 0) { echo "Precip type: " . $current->PrecipitationType() . "\n"; $now['preciptype'] = $current->PrecipitationType(); } //Get weekly forecast (next 2 days is fine) $conditions_week = $forecast->getForecastWeek($latitude, $longitude); //print_r($conditions_week); echo "\nConditions this week:\n"; $i = 0; foreach ($conditions_week as $conditions) { $cheese = new DateTime("now"); $step = new DateInterval('P2D'); //If day is less then or equal to 2 days from now then we want that info if ($cheese->add($step)->format('Y-m-d') >= $conditions->getTime('Y-m-d')) { echo $conditions->getTime('Y-m-d') . ": " . $conditions->getMaxTemperature() . "\n"; $twoday[$i]['maxtemp'] = $conditions->getMaxTemperature(); $twoday[$i]['chancerain'] = $conditions->getPrecipitationProbability(); $twoday[$i]['rainfall'] = $conditions->getPrecipitationIntensity(); $twoday[$i]['rainfallmax'] = $conditions->getPrecipitationIntensityMax(); if ($twoday[$i]['rainfallmax'] > 0) { $twoday[$i]['rainfallmaxtime'] = $conditions->getPrecipitationIntensityMaxTime(); } if ($twoday[$i]['chancerain'] > 0) { echo "Precip type: " . $conditions->getPrecipitationType() . "\n"; $twoday[$i]['preciptype'] = $conditions->getPrecipitationType(); } $twoday[$i]['humidity'] = $conditions->getHumidity(); $twoday[$i]['summary'] = $conditions->getSummary(); } $i++; } //Get Yesterdays forecast echo "\n Yesterdays conditions: \n"; $yesterdays = $forecast->getHistoricalConditions($latitude, $longitude, $yesterdaytime); echo "Yesterday: " . $yesterdays->getMaxTemperature() . "\n"; echo "percent rain: " . $yesterdays->getPrecipitationProbability() . "\n"; echo "humidity: " . $yesterdays->getHumidity() . "\n"; echo "rain fall: " . $yesterdays->getPrecipitationIntensity(); $yesterday['temp'] = $yesterdays->getMaxTemperature(); $yesterday['chancerain'] = $yesterdays->getPrecipitationProbability(); $yesterday['humidity'] = $yesterdays->getHumidity(); $yesterday['summary'] = $yesterdays->getSummary(); $yesterday['rainfall'] = $yesterdays->getPrecipitationIntensity(); //If precip probability > 0 then get precip type too if ($yesterday['chancerain'] > 0) { echo "Precip type: " . $yesterdays->PrecipitationType() . "\n"; $yesterdays['preciptype'] = $yesterdays->PrecipitationType(); } //print_r($yesterdays); //return array with current, today, tomorrow, and day after temp/conditions return [$now, $twoday]; }
$lang = 'en'; // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' $forecast = new ForecastIO($api_key, $units, $lang); // all default will be // $forecast = new ForecastIO($api_key); /* * GET CURRENT CONDITIONS */ $condition = $forecast->getCurrentConditions($latitude, $longitude); echo 'Current temperature: ' . $condition->getTemperature() . "\n"; /* * GET HOURLY CONDITIONS FOR TODAY */ $conditions_today = $forecast->getForecastToday($latitude, $longitude); echo "\n\nTodays temperature:\n"; foreach ($conditions_today as $cond) { echo $cond->getTime('H:i:s') . ': ' . $cond->getTemperature() . "\n"; } /* * GET DAILY CONDITIONS FOR NEXT 7 DAYS */ $conditions_week = $forecast->getForecastWeek($latitude, $longitude); echo "\n\nConditions this week:\n"; foreach ($conditions_week as $conditions) { echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; } /* * GET HISTORICAL CONDITIONS */ $condition = $forecast->getHistoricalConditions($latitude, $longitude, '2010-10-10T14:00:00-0700'); echo "\n\nTemperatur 2010-10-10: " . $condition->getMaxTemperature() . "\n";
} else { /** * Make a request to https://forecast-xbgmsharp.rhcloud.com * to non disclose the Forecast.io API key, proxy set to true * Source code at https://github.com/xbgmsharp/nodejs-forecast **/ $forecast = new ForecastIO($fc_api_key, $fc_unit, $fc_lang, true); } $images = hash_from_query($query, 'id'); $datas = array(); $errors = array(); $warnings = array(); $infos = array(); foreach ($images as $image) { // Fech reverse location from API $condition = $forecast->getHistoricalConditions($image['latitude'], $image['longitude'], $image['date']); if (!isset($condition) or $condition === 'false') { $errors[] = "Error fetching weather condition data for " . $image['name']; } //print_r($condition); // If reponse include icon if (isset($condition) and isset($condition->icon) and !empty($condition->icon)) { if (!$sync_options['simulate']) { $id = tag_id_from_tag_name($sync_options['fc_tag_group'] . ":" . $condition->icon); add_tags([$id], [$image['id']]); } $infos[] = "Set tag '" . $condition->icon . "' for " . $image['name']; $datas[] = $image['id']; } else { $warnings[] = "No valid tags for " . $image['name']; }