You are here

function _weather_es_retrieve_data in Weather_es 5

Same name and namespace in other branches
  1. 6 weather_es_parser.inc \_weather_es_retrieve_data()
  2. 6.2 weather_es_parser.inc \_weather_es_retrieve_data()

Get the data from AEMET

1 call to _weather_es_retrieve_data()
_weather_es_aemet in ./weather_es_parser.inc
Clean the data and save it

File

./weather_es_parser.inc, line 299
Gets the data from the AEMET web

Code

function _weather_es_retrieve_data($wuid, $city_cod, $lan, $city_nam) {
  global $user;
  $response = -1;

  // Web direction example (until 2009/12/10): http://www.aemet.es/es/eltiempo/prediccion/localidades?l=03410&p=03
  // New web direction example: http://www.aemet.es/es/eltiempo/prediccion/localidades/jijona-xixona-03410
  // www.aemet.es: 193.144.152.138
  // Languages are: es, ca/va, gl, eu, en and fr.
  // This is what we get from the bad boys of aemet (30-01-2010)

  /*
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.aemet.es/es/eltiempo/prediccion/localidades/Aramaio-01030">here</a>.</p>
  </body></html>
  */
  $page = '/' . $lan . '/eltiempo/prediccion/localidades/' . $city_nam . '-' . $city_cod;
  $url = 'http://www.aemet.es' . $page;
  if (function_exists('fsockopen')) {
    $result = drupal_http_request($url);
    switch ($result->code) {
      case '200':
      case '304':

        // Get the table
        preg_match('/<table.+table>/s', $result->data, $captured);

        // Pass it to a string
        $data_string = implode(" ", $captured);
        $length = strlen($data_string);
        $position = strpos($data_string, "table>");
        $data_string = substr($data_string, 0, $position - $length);
        preg_match_all('/<tr.+tr>/sU', $data_string, $captured_tr);
        $response = array(
          $captured_tr,
        );
        break;
      case '301':
      case '302':
      case '307':
        $result = drupal_http_request('http://www.aemet.es' . $result->redirect_url);

        // Get the table
        preg_match('/<table.+table>/s', $result->data, $captured);

        // Pass it to a string
        $data_string = implode(" ", $captured);
        $length = strlen($data_string);
        $position = strpos($data_string, "table>");
        $data_string = substr($data_string, 0, $position - $length);
        preg_match_all('/<tr.+tr>/sU', $data_string, $captured_tr);
        $response = array(
          $captured_tr,
        );
        break;
      default:
        if ($user->uid) {
          drupal_set_message(t('fsockopen fail to open AEMET.'), 'error');
        }
    }
  }
  else {
    if ($user->uid) {
      drupal_set_message(t('Function fsockopen does not exist, you can\'t use this module...'), 'error');
    }
  }
  return $response;
}