<?php
$url="http://gismeteo.ru/towns/28642.htm";
$MONTH_ARRAY=array("янв"=>"01","фев"=>"02","мар"=>"03","апр"=>"04","май"=>"05","июн"=>"06","июл"=>"07","авг"=>"08","сен"=>"09","окт"=>"10","ноя"=>"11","дек"=>"12");
$open=file($url);
if($open)
{
$CNT="";
foreach($open as $m)
{
$CNT.=$m;
}
$text=$CNT;
preg_match("/(\d{2}|\d)\/(\d{2}|\d)\/(\d{4})/",$text,$regs);
$day = $regs[1];
$month = $regs[2];
$year = $regs[3];
# print "!$day!$month!$year!";
#$start="".$day."/".$month."/".$year."";
if(eregi("Челябинск",$text) and eregi("(([0-9][0-9]|[1-9])\/([0-9][0-9]|[1-9])\/([20][0-9][0-9]))",$text))
{
$start="</td></tr><tr><td nowrap align=center bgcolor=F0F0F0>";
$end="</td></tr></table>';";
$text=explode("".$start."",$text);
$text=explode("".$end."",$text[1]);
$text=$text[0];
# разбиваем текст на <tr>
$TR=split("</tr><tr>",$text);
foreach($TR as $vtr=>$ktr)
{
$ktr=trim($ktr);
if(!empty($ktr))
{
# разбиваем текст на <td>
$TD=split("</td>",$ktr);
foreach($TD as $vtd=>$ktd)
{
preg_match("/\s+title\=(\'|\")*(.*?)(\'|\")*\s*>/si", $ktd, $matches);
if(!empty($matches[2]))
{
$ktd=$matches[2];
}
$ktd=ereg_replace('<[^>]*>',' ',$ktd);
$ARR[$vtr][$vtd]=$ktd;
}
}
}
#print"\n\n\n";
foreach($ARR[0] as $k=>$v)
{
if(!eregi("meteo",$v) and !empty($v))
{
# датa и время суток
$v=trim($v);
$v=ereg_replace(" "," ",$v);
$dayall=split(" ",$v);
$DAYTIME = $dayall[0];
$DAY = $dayall[1];
if(strlen($DAY)<2){$DAY="0".$DAY;}
$MONTH = $dayall[2];
$MONTH = $MONTH_ARRAY[$MONTH];
# print "день: $DAY\nмесяц: $MONTH\nвремя дня: $DAYTIME\n";
# Облачность
$OBLACHNOST=trim($ARR[1][$k]);
# print "облачность $OBLACHNOST\n";
# Осадки
$OSADKI = trim($ARR[2][$k]);
# print "Осадки $OSADKI\n";
# Атмосферное давление, мм
$DAVLENIE = trim($ARR[3][$k]);
# print "Атмосферное давление $DAVLENIE\n";
# Tемпература воздуха, °C
$TEMPERATURA = trim($ARR[4][$k]);
# print "Tемпература воздуха $TEMPERATURA\n";
# Влажность воздуха,%
$VLAGNOST = trim($ARR[5][$k]);
# print "Влажность воздуха $VLAGNOST\n";
# Ветер, метр/сек
$VETER = trim($ARR[6][$k]);
# print "Ветер $VETER\n\n\n\n";
$q="select * from weather where daytime='".$DAYTIME."' AND date='".$year."-".$MONTH."-".$DAY."'";
$m=mysql_query($q);
if(mysql_num_rows($m)>0)
{
$u=mysql_query("update weather SET `oblachnost`='".$OBLACHNOST."', `osadki`='".$OSADKI."', `davlenue`='".$DAVLENIE."', `temperatura`='".$TEMPERATURA."', `vlagnost`='".$VLAGNOST."', `veter`='".$VETER."', `updatedate`='".$year."-".$month."-".$day."' where daytime='".$DAYTIME."' AND date='".$year."-".$MONTH."-".$DAY."' and updatedate <> '".$year."-".$month."-".$day."'");
if($u)
{
print "updated\n";
}
}
if(mysql_num_rows($m)<1)
{
$q="INSERT INTO `weather` ( `id` , `daytime` , `oblachnost` , `osadki` , `davlenue` , `temperatura` , `vlagnost` , `veter` , `updatedate` , `date` )
VALUES (
'', '".$DAYTIME."', '".$OBLACHNOST."', '".$OSADKI."', '".$DAVLENIE."', '".$TEMPERATURA."', '".$VLAGNOST."', '".$VETER."', '".$year."-".$month."-".$day."', '".$year."-".$MONTH."-".$DAY."'
)";
mysql_query($q);
print "inserted\n";
}
}
}
#print"\n\n\n";
}
}
?>