File source

<?php
/** parses www.shmu.sk for weather */
class Shmu extends WeatherSource{
  protected 
$city;
  protected 
$data;
  protected 
$timestamp false;
  protected 
$validity 600;
  protected 
$icons = Array(
    
=> "",
    
=> "01",
    
=> "02",
    
=> "03",
    
=> "10",
    
=> "10"//prehanky
    
=> "11",
    
=> "13",
    
=> "13"// sneh s dazdom,
    
=> "50"//hmla
    
10 => "02",
    
11 => "10",
    
12 => "02",
    
13 => ""// snehove prehanky,
    
14 => ""// nie je
    
15 =>"03",
    
16 => "03",
    
17 => "13"
  
);
  protected 
$icons_current = Array(
    
=> "01",
    
=> "02",
    
=> "03",
    
=> "04",
    
=> "50",
    
=> "10"// slaby dazd
    
=> "10",   // silny dazd
    
=> "13",   //dazd so snehom
    
=> "13"//sneh
    
10 => "13"//slaby sneh
    
11 => "10"//prehanky
    
12 => "13"//snehove prehanky
    
13 => "11"//burka
    
14 => "11"//blesky
  
);
  protected 
$substitiute_current = Array(
    
"TN" => "PN"
  
);

  
  function 
__construct($city){
    
$this->city $city;
  }

  function 
getIcon($status$time false){
    
$suffix "d";
    if(
$time && Date("H",$time) >= 19 ){
      
$suffix "n";
    }
    return 
$this->icons[$status].$suffix;
  }

  function 
getIconCurrent($status$time false){
    
$suffix "d";
    if(
$time && Date("H",$time) >= 19 ){
      
$suffix "n";
    }
    return 
$this->icons_current[$status].$suffix;
  }

  function 
getCurrent($city){
    if(
$this->substitiute_current[$city]){
      
$city $this->substitiute_current[$city];
    }
    
$page file_get_contents("http://www.shmu.sk/sk/?page=1&id=meteo_gapocasie_sk");
    
$data $this->parseSHMU($page,$city);
    
$matches $data["details"];
    
$text "";
    if(
trim($matches[2][3])!="-"){
      
$text $matches[2][3].", ";  
    }
    
$text.=$matches[2][2];
    
$r = Array(
      
"status" => $data["icon"],
      
"icon" => $this->getIconCurrent($data["icon"], Time()),
      
"city" => $matches[2][0],
      
"temp" => intval($matches[2][1]),
      
"text" => $text,
      
"wind" => intval($matches[2][4]),
      
"wind_dir" => $matches[2][5]
    );
    return 
$r;
  }

  function 
getForecast($city$date false){
    if(!
$date$date Time();
    
$page file_get_contents("http://www.shmu.sk/sk/?page=1&id=meteo_gpredpoved_sk&day=".Date("Ymd",$date));
    
$data $this->parseSHMU($page$city);
    
$matches $data["details"];
    return Array(
      
"status" => $data["icon"],
      
"icon" => $this->getIcon($data["icon"]),
      
"city" => $matches[2][0],
      
"date" => $matches[2][1],
      
"temp_night"=> intval($matches[2][2]),
      
"temp" => intval($matches[2][3]),
      
"text" => $matches[2][4],
      
"wind" => intval($matches[2][5]),
      
"wind_dir" => $matches[2][6]
    );
  }

  function 
parseSHMU($page$city){
    
$matches = Array();
    
preg_match('/HINTS_ITEMS = (\{[^\}]*\})/ms'$page$matches);
    
//return $matches[1];
    
$data str_replace("'"'"'$matches[1]);
    
$data json_decode($data,true);
    
$w $data[$city];
    
preg_match_all("/<p>(.*?)<strong>(.*?)<\/strong>[[:space:]]*<\/p>/ms",$w,$matches);

    
preg_match("@<img src=\"([^\"]+)\"[^>]+".$city."[^>]+>@ms"$page$icons);
    
preg_match("@([0-9]+)\.[a-zA-Z]+$@ms",$icons[1],$icon);
    return Array(
      
"icon" => $icon[1],
      
"details" => $matches
    
);
  }

  function 
load(){
    
$today Time();
    return Array(
      
"current" => $this->getCurrent($this->city),
      
"0" => $this->getForecast($this->city,$today),
      
"1" => $this->getForecast($this->city,$today+24),
      
"2" => $this->getForecast($this->city,$today+48),
      
"3" => $this->getForecast($this->city,$today+72)
    );
  }

  function 
getData($type="current"){
    if(
$this->timestamp && $this->timestamp $this->validity Time()){
      
    }else{
      
$this->data $this->load();
      
$this->timestamp Time();
    }
    return 
$this->data[$type];
  }
}