You are here

function _weather_download_forecast in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather_parser.inc \_weather_download_forecast()

Downloads a new forecast from yr.no.

Parameters

string $geoid: The GeoID for which the forecasts should be downloaded.

Return value

bool TRUE on success, FALSE on failure.

1 call to _weather_download_forecast()
weather_get_weather in ./weather.common.inc
Returns a weather object for the specified GeoID.

File

./weather_parser.inc, line 133
Retrieves and parses raw METAR data and stores result in database.

Code

function _weather_download_forecast($geoid) {

  // Do not download anything if the variable 'weather_time_for_testing' is set.
  // In this case, we are in testing mode and only load defined
  // forecasts to get always the same results.
  if (variable_get('weather_time_for_testing', REQUEST_TIME) != REQUEST_TIME) {
    $path = drupal_get_path('module', 'weather') . '/tests/data/' . $geoid . '.xml';
    if (is_readable($path)) {
      $xml = file_get_contents($path);
    }
    else {
      $xml = '';
    }
    return _weather_parse_forecast($xml, $geoid);
  }

  // Specify timeout in seconds.
  $timeout = 10;
  module_load_include('inc', 'weather', 'weather.common');
  $url = _weather_get_link_for_geoid($geoid, 'yr');
  $response = drupal_http_request($url, array(
    'timeout' => $timeout,
  ));

  // Extract XML data from the received forecast.
  if (!isset($response->error)) {
    return _weather_parse_forecast($response->data, $geoid);
  }
  else {

    // Make an entry about this error into the watchdog table.
    watchdog('weather', 'Download of forecast failed: @error', array(
      '@error' => $response->error,
    ), WATCHDOG_ERROR);

    // Show a message to users with administration priviledges.
    if (user_access('administer custom weather block') or user_access('administer site configuration')) {
      drupal_set_message(t('Download of forecast failed: @error', array(
        '@error' => $response->error,
      )), 'error');
    }
  }
}