You are here

function _weather_es_aemet in Weather_es 6

Same name and namespace in other branches
  1. 5 weather_es_parser.inc \_weather_es_aemet()
  2. 6.2 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_submit in ./weather_es.module
Handle the submission of the custom weather_es block.

File

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

Code

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

  // Raw data of AEMET
  $array_text = _weather_es_retrieve_data($wuid, $city_cod, $lan, $city_nam);
  if ($array_text != -1) {

    // Get the days
    preg_match_all('/<th.+th>/', $array_text[0][0][0], $captured_th);
    for ($i = 1; $i < 8; $i++) {
      preg_match('/>.+</', $captured_th[0][$i], $data);
      $data_lon = strlen($data[0]);
      $ori_str = substr($data[0], 1, $data_lon - 2);
      $htm_str = htmlentities($ori_str);
      $days[] = $htm_str;
    }

    // Get ampm (we don't really need that...)
    preg_match_all('/<th.+th>/', $array_text[0][0][1], $captured_th);
    for ($i = 0; $i < sizeof($captured_th[0]); $i++) {
      preg_match('/>.+</', $captured_th[0][$i], $data);
      $data_lon = strlen($data[0]);
      $ampm[] = substr($data[0], 1, $data_lon - 2);
    }

    // Get the data types and the data
    for ($i = 2; $i < sizeof($array_text[0][0]); $i++) {

      // Data type
      preg_match_all('/<th.+th>/', $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);
      if (preg_match('/cielo|cel|ceo|egoera|Sky|ciel/', $array_text[0][0][$i])) {
        $ii = 0;
      }
      elseif (preg_match('#\\(%\\)#', $array_text[0][0][$i])) {
        $ii = 1;
      }
      elseif (preg_match('#\\(m\\)#', $array_text[0][0][$i])) {
        $ii = 2;
      }
      elseif (preg_match('#\\(.C\\)#', $array_text[0][0][$i])) {
        if (!isset($jj)) {
          $ii = 3;
          $jj = 0;
        }
        else {
          $ii = 4;
        }
      }
      elseif (preg_match('/Viento|Vent|Vento|Haizeak|Wind/', $array_text[0][0][$i])) {
        $ii = 5;
      }
      elseif (preg_match('#\\(km/h\\)#', $array_text[0][0][$i])) {
        $ii = 6;
      }
      elseif (preg_match('/UV/', $array_text[0][0][$i])) {
        $ii = 7;
      }
      elseif (preg_match('/Nivel|Nivell|Gehinezko|Level|Niveau/', $array_text[0][0][$i])) {
        $ii = 8;
      }
      else {
        $ii = 99;
      }
      switch ($ii) {

        // Sky
        case 0:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // Rain
        case 1:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // Snow
        case 2:
          preg_match_all('/<td.+td>/', $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;
            }
          }
          break;

        // Tmax
        case 3:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // Tmin
        case 4:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // Wind direction
        case 5:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // Wind speed
        case 6:
          preg_match_all('/<td.+td>/', $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);
          }
          break;

        // UI
        case 7:
          preg_match_all('/<td.+td>/', $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]);
            $ui[] = substr($data[0], 0, $data_lon - 1);
          }
          break;

        // Risk level
        case 8:
          preg_match_all('/<td.+td>/', $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]);
            $rsk[] = substr($data[0], 5, $data_lon - 6);
          }
          break;
      }
    }
    if (!isset($snow)) {
      for ($i = 0; $i < 7; $i++) {
        $snow[$i] = 9999;
      }
    }
    for ($i = 0; $i < sizeof($ori_data_type); $i++) {
      $htm_str = htmlentities($ori_data_type[$i]);
      $data_type[] = $htm_str;
    }
    $aemet = array(
      $days,
      $ampm,
      $data_type,
      $sky_img,
      $sky_txt,
      $rain,
      $snow,
      $tmax,
      $tmin,
      $win_dir,
      $win_spd,
      $ui,
      $rsk,
    );
  }
  else {
    $aemet = -1;
  }
  return $aemet;
}