You are here

function _weather_es_aemet in Weather_es 6.2

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

Clean the data and save it

2 calls to _weather_es_aemet()
block_weather_es_contents in ./weather_es.module
Show the weather_es contents
weather_es_configuration_form_submit in ./weather_es.module
Handle the submission of the custom weather_es block.

File

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

Code

function _weather_es_aemet($wuid, $city_cod, $lan, $city_nam) {

  // Raw data of AEMET
  $aemet = -1;
  $array_text = _weather_es_retrieve_data($wuid, $city_cod, $lan, $city_nam);
  if ($array_text != -1) {
    for ($i = 0; $i < sizeof($array_text[0][0]); $i++) {

      // Data type
      preg_match_all('/<th.+th>/U', $array_text[0][0][$i], $captured_th);
      preg_match('/>.+</', $captured_th[0][0], $data);
      $data_lon = strlen($data[0]);
      $ori_data_type[] = substr($data[0], 1, $data_lon - 2);

      // Dates
      if (preg_match('/abbr="Fec\\."|abbr="Dat."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<th.+th>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 1; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>.+</', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $ori_str = substr($data[0], 1, $data_lon - 2);
          $htm_str = htmlentities($ori_str);
          $days[] = $htm_str;
        }
      }
      elseif (preg_match('/abbr="[ap]m\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<th.+th>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>.+</', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $ampm[] = substr($data[0], 1, $data_lon - 2);
        }
      }
      elseif (preg_match('/abbr="Cielo"/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);

        // Images
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/[0-9]{2}.gif/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $sky_img[] = substr($data[0], 0, $data_lon - 4);
        }

        // Text
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/alt=".+"/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $sky_txt[] = substr($data[0], 5, $data_lon - 6);
        }
      }
      elseif (preg_match('/abbr="Pro\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>[0-9]{1,3}/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $rain[] = substr($data[0], 1, $data_lon);
        }
      }
      elseif (preg_match('/abbr="Cot\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>[0-9]{1,4}/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          if (sizeof($data[0]) != 0) {
            $snow[] = substr($data[0], 1, $data_lon);
          }
          else {
            $snow[] = 9999;
          }
        }
      }
      elseif (preg_match('/abbr="Max\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>[0-9]{1,4}/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $tmax[] = substr($data[0], 1, $data_lon);
        }
      }
      elseif (preg_match('/abbr="Min\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>-?[0-9]{1,4}/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $tmin[] = substr($data[0], 1, $data_lon);
        }
      }
      elseif (preg_match('/abbr="Vie\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/alt=".+"/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $win_dir[] = substr($data[0], 5, $data_lon - 6);
        }
      }
      elseif (preg_match('/abbr="km\\/h\\."/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/>[0-9]{1,4}/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $win_spd[] = substr($data[0], 1, $data_lon);
        }
      }
      elseif (preg_match('/abbr="UV"/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/[0-9]{1,2}</', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          if ($data_lon != 0) {
            $ui[] = substr($data[0], 0, $data_lon - 1);
          }
          else {
            $ui[] = 99;
          }
        }
      }
      elseif (preg_match('/abbr="Aviso"/', $array_text[0][0][$i], $abbr)) {
        preg_match_all('/<td.+td>/U', $array_text[0][0][$i], $captured_th);
        for ($j = 0; $j < sizeof($captured_th[0]); $j++) {
          preg_match('/alt=".+?"/', $captured_th[0][$j], $data);
          $data_lon = strlen($data[0]);
          $ori_rsk[] = substr($data[0], 5, $data_lon - 6);
        }
      }
    }
    if (!isset($snow)) {
      for ($i = 0; $i < 7; $i++) {
        $snow[$i] = 9999;
      }
    }
    for ($i = 2; $i < sizeof($ori_data_type); $i++) {
      $htm_str = htmlentities($ori_data_type[$i]);
      $htm_str = strtr($htm_str, array(
        '&amp;' => '&',
      ));
      $data_type[] = $htm_str;
    }
    for ($i = 0; $i < sizeof($ori_rsk); $i++) {
      $htm_str = htmlentities($ori_rsk[$i]);
      $rsk[] = $htm_str;
    }
    $aemet = array(
      $days,
      $ampm,
      $data_type,
      $sky_img,
      $sky_txt,
      $rain,
      $snow,
      $tmax,
      $tmin,
      $win_dir,
      $win_spd,
      $ui,
      $rsk,
    );
  }
  return $aemet;
}