Created
February 12, 2013 21:07
-
-
Save anonymous/4773397 to your computer and use it in GitHub Desktop.
Grabber time weather updates files (tvpogoda.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
#...//.../*...*/ | |
/* develop: Alexey Kovalev | |
e-mail: [email protected] */ | |
# --- GLOBAL VARIABLES --------------------------------------------------------- | |
# string error prefix & message | |
$str_errorPrx = "[ php.error ] "; | |
$str_errorMsg = ""; | |
# --- GET HTML CODE ------------------------------------------------------------ | |
# path parser webPage | |
$str_sourceNamePage = "http://tvpogoda.com/fobmail/Mos24/"; | |
# get html-code my web-site | |
$str_sourceHtmlPage = file_get_contents( $str_sourceNamePage ); | |
# WRITE *LOG | |
# check return value | |
if ( $str_sourceHtmlPage == FALSE ) { | |
# generate error | |
$str_errorMsg = "#01 | |
Return value is not string. | |
'file_get_contents( $str_sourceNamePage )' | |
"; | |
# exit script | |
exit( $str_errorPrx . $str_errorMsg ); | |
} | |
# --- FIND DATA ---------------------------------------------------------------- | |
# pattern ( regular expression ) for find "value" | |
$str_patternFileName = "/>[\w\s]*.(txt|xml|doc)</"; | |
$str_date = "[0-3][0-9]-[A-Z][a-z]{2,}-20[1-9][0-9]"; | |
$str_time = "[0-2][0-9]:[0-5][0-9]\s*"; | |
$str_patternDatetime = "/>" . $str_date . "\s" . $str_time . "</"; | |
# "/>[0-3][0-9]-[A-Z][a-z]{2}-20[1-9][0-9]\s[0-2][0-9]:[0-5][0-9]\s*</"; | |
# array replace pair string ">" & "<" - null | |
$arr_badSymbolReplacePair = array( ">" => "", "<" => "" ); | |
# output array | |
$arr_fileName = array(); | |
$arr_dateTime = array(); | |
# get all element $arr_fileName | |
$arr_fileName = getValueFromStringUsePattern( $str_sourceHtmlPage, | |
$str_patternFileName, | |
$arr_badSymbolReplacePair ); | |
# get all element $arr_dateTime | |
$arr_dateTime = getValueFromStringUsePattern( $str_sourceHtmlPage, | |
$str_patternDatetime, | |
$arr_badSymbolReplacePair ); | |
# --- DATETIME ----------------------------------------------------------------- | |
# format date | |
$str_formatDatetime = "Y-m-d H:i:s O"; | |
foreach( $arr_dateTime as &$str_dateTime ) | |
{ | |
# create object type DateTime | |
$dt_value = date_create( $str_dateTime .":00", timezone_open('UTC')); | |
# WRITE *LOG | |
# check return value | |
if( $dt_value == FALSE ) { | |
# generate error. [ log ] | |
$str_errorMsg = "#03 | |
Return value is not datetime ('$str_dateTime' => '$dt_value'). | |
date_create( $str_dateTime .':00', timezone_open('UTC'))' | |
"; | |
# add error to log | |
# log( $str_errorPrx . $str_errorMsg ); | |
continue; | |
} | |
# get datetime(string) | |
$str_dateTime = date_format( $dt_value, $str_formatDatetime ); | |
# WRITE *LOG | |
# check return value | |
if( $str_dateTime == FALSE ) { | |
# generate error. [ log ] | |
$str_errorMsg = "#04 | |
Return value is not srting ('$dt_value' => '$str_dateTime') | |
date_format( $dt_value, $str_formatDatetime )' | |
"; | |
# add error to log | |
# log( $str_errorPrx . $str_errorMsg ); | |
continue; | |
} | |
} | |
# current datetime | |
$str_datetimeNow = date( $str_formatDatetime ); | |
# --- STRUCTURING DATA --------------------------------------------------------- | |
#output array | |
$asArr_data = array(); | |
# create array ('fileName': 'dateTime') | |
for( $i = 0; $i < count( $arr_fileName ); $i++) { | |
$asArr_data[ $arr_fileName[$i] ] = | |
array( $arr_dateTime[$i], $str_datetimeNow ); } | |
# WRITE *LOG | |
# check count element (array) | |
if ( count( $arr_fileName ) != count( $arr_dateTime )) { | |
# generate error | |
$str_listValue = getStringFromAssociativeArray( $asArr_data, | |
"Name", | |
"Date" ); | |
$str_errorMsg = "#02 | |
Discrepancy count element in array.\n'\n$str_listValue' | |
"; | |
# exit script | |
exit( $str_errorPrx . $str_errorMsg ); | |
} | |
/*# print result | |
echo getStringFromAssociativeArray( $asArr_data, | |
"Name", | |
"Date", | |
30 );*/ | |
# print result | |
echo getStringFromAssociativeArrayWhereValueArray( $asArr_data, | |
"Name", | |
"Date", | |
30 ); | |
# print current datetime | |
echo "\n " .str_pad( "Datetime.now ", 30, ".", STR_PAD_RIGHT). | |
" " .$str_datetimeNow; | |
# --- EXIST DATA (JSON) -------------------------------------------------------- | |
# set current date +.json | |
$str_currentDate_json = substr( $str_datetimeNow, 0, 10 ) . ".json"; | |
# root folder 4 weather data | |
$str_pathRootWeatherFolder = dirName(__FILE__) . "/../weather/"; | |
# full path 2 file | |
$str_fullPath2FileCurrentDate_json = $str_pathRootWeatherFolder . | |
$str_currentDate_json; | |
# check exist folder 'weather'? | |
if( !is_dir( $str_pathRootWeatherFolder ) ){ | |
mkdir( $str_pathRootWeatherFolder ); | |
# WRITE *LOG | |
} | |
#find current file (date) | |
if( !file_exists( $str_fullPath2FileCurrentDate_json ) ){ | |
$fl = fopen( $str_fullPath2FileCurrentDate_json, "w" ); | |
fclose( $fl ); | |
# WRITE *LOG | |
} | |
# --- READ DATA (JSON) --------------------------------------------------------- | |
# open file 2 read | |
$f_currentDate_json = fopen( $str_fullPath2FileCurrentDate_json, "r" ); | |
# content file (currentDate.json) | |
$str_contentCurrentDate_json = null; | |
# check exist file | |
if ( $f_currentDate_json ) { | |
# get filesize | |
$i_fileSize = filesize ( $str_fullPath2FileCurrentDate_json ); | |
if( $i_fileSize != 0) { | |
# get string file | |
$str_contentCurrentDate_json = fread( $f_currentDate_json, $i_fileSize); | |
} | |
} | |
# close file | |
fclose( $f_currentDate_json); | |
# print result | |
#echo "\n\n" . $str_contentCurrentDate_json . "\n"; | |
# --- COMPARE & CHANGE DATA (JSON) --------------------------------------------- | |
# get assiciative array from string | |
$asArr_currentDate_json = json_decode( $str_contentCurrentDate_json, true ); | |
# exist element | |
$bl_existElementInArray = FALSE; | |
# add/create new data | |
foreach ( $asArr_data as $str_fileName => $arr_date ) { | |
# add new datetime | |
if( isset( $asArr_currentDate_json[ $str_fileName ] )) { | |
foreach ( $asArr_currentDate_json[ $str_fileName ] as $arr_value ) { | |
if( $arr_value[0] == $arr_date[0] ) { | |
$bl_existElementInArray = TRUE; | |
break; | |
} | |
} | |
if ( !$bl_existElementInArray ) { | |
# add element in start array | |
array_unshift( $asArr_currentDate_json[ $str_fileName ], | |
$arr_date ); | |
} | |
# return default flag | |
$bl_existElementInArray = FALSE; | |
} | |
# create new datetime | |
else { $asArr_currentDate_json[ $str_fileName ] = array( $arr_date ); } | |
} | |
# print result | |
#print_r( $asArr_currentDate_json ); | |
# --- SAVE DATA (JSON) --------------------------------------------------------- | |
# encode asArr2json | |
$json_currentDate_json = json_encode( $asArr_currentDate_json ); | |
# set pretty print (json) | |
$json_currentDate_json = prettyPrint( $json_currentDate_json ); | |
# print result | |
#echo "\n\n". $json_currentDate_json ."\n\n"; | |
# open file 2 write | |
$f_currentDate_json = fopen( $str_fullPath2FileCurrentDate_json, "w" ); | |
# write data | |
fwrite( $f_currentDate_json, $json_currentDate_json); | |
# close file | |
fclose( $f_currentDate_json); | |
# ------------------------------------------------------------------------------ | |
# ============================================================================== | |
# ------------------------------------------------------------------------------ | |
# --- FUNCTION ----------------------------------------------------------------- | |
function getValueFromStringUsePattern( $str_string, | |
$str_pattern, | |
$arr_replacePair ) | |
{ | |
# array from output values | |
$arr_values = array(); | |
$arr_result = array(); | |
$str_value = null; | |
# get all string equal pattern | |
preg_match_all( $str_pattern, $str_string, $arr_values, PREG_SET_ORDER ); | |
# replace all bad symbol | |
foreach( $arr_values as $arr_value ) | |
{ | |
# delete "space"... | |
$str_value = trim( $arr_value[0] ); | |
# delete bad symbol | |
$str_value = strtr( $str_value, $arr_replacePair ); | |
# delete "space"... | |
$str_value = trim( $str_value ); | |
# push in array - $str_value | |
$arr_result[] = $str_value; | |
} | |
# return result array | |
return $arr_result; | |
} | |
function getStringFromAssociativeArray( $asArr_associative, | |
$str_key, | |
$str_value, | |
$i_lenghtString ) | |
{ | |
# top | |
$str_joinValueArray = | |
str_pad( "[$str_key]", ($i_lenghtString +1), " ", STR_PAD_RIGHT) . | |
"[$str_value]" ."\n"; | |
#str_pad( "[$str_value]", (25 +1), " ", STR_PAD_LEFT) ."\n"; | |
# body (pair value: 'name' => 'date') | |
foreach( $asArr_associative as $key => $value ) { | |
$str_joinValueArray .= " ". | |
str_pad( $key . " ", $i_lenghtString, ".", STR_PAD_RIGHT) ." ". | |
$value . "\n"; | |
#str_pad((" " . $value), $i_lenghtString, ".", STR_PAD_LEFT) ." \n"; | |
} | |
return $str_joinValueArray; | |
} | |
function getStringFromAssociativeArrayWhereValueArray( $asArr_associative, | |
$str_key, | |
$str_value, | |
$i_lenghtString ) | |
{ | |
# top | |
$str_joinValueArray = | |
str_pad( "[$str_key]", ($i_lenghtString +1), " ", STR_PAD_RIGHT) . | |
"[$str_value]" ."\n"; | |
#str_pad( "[$str_value]", (25 +1), " ", STR_PAD_LEFT) ."\n"; | |
# body (pair value: 'name' => 'date') | |
foreach( $asArr_associative as $key => $value ) { | |
$str_joinValueArray .= " ". | |
str_pad( $key . " ", $i_lenghtString, ".", STR_PAD_RIGHT) ." ". | |
$value[0] . "\n " | |
.str_repeat( " ", $i_lenghtString ). | |
$value[1] . "\n"; | |
} | |
return $str_joinValueArray; | |
} | |
# http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php | |
# edit: alexey kovalev | |
function prettyPrint( $json ) | |
{ | |
$result = ''; | |
$level = 0; | |
$prev_char = ''; | |
$in_quotes = false; | |
$ends_line_level = NULL; | |
$json_length = strlen( $json ); | |
for( $i = 0; $i < $json_length; $i++ ) { | |
$char = $json[$i]; | |
$new_line_level = NULL; | |
$post = ""; | |
if( $ends_line_level !== NULL ) { | |
$new_line_level = $ends_line_level; | |
$ends_line_level = NULL; | |
} | |
if( $char === '"' && $prev_char != '\\' ) { | |
$in_quotes = !$in_quotes; | |
} else if( ! $in_quotes ) { | |
switch( $char ) { | |
case '}': case ']': | |
$level--; | |
$ends_line_level = NULL; | |
$new_line_level = $level; | |
break; | |
case '{': case '[': | |
$level++; | |
case ',': | |
$ends_line_level = $level; | |
break; | |
case ':': | |
$post = " "; | |
break; | |
case " ": case "\t": case "\n": case "\r": | |
$char = ""; | |
$ends_line_level = $new_line_level; | |
$new_line_level = NULL; | |
break; | |
} | |
} | |
if( $new_line_level !== NULL ) { | |
$result .= "\r\n".str_repeat( "\t", $new_line_level ); | |
} | |
$result .= $char.$post; | |
$prev_char = $char; | |
} | |
# breaty array: alexey kovalev | |
$str_startArray = "/\[\r\n\t{2,}\"/"; | |
$result = preg_replace( $str_startArray, '[ "', $result ); | |
$str_middleArray = "/,\r\n\t{2,}\"/"; | |
$result = preg_replace( $str_middleArray, ', "', $result ); | |
$str_endArray = "/\"\r\n\t{2,}\]/"; | |
$result = preg_replace( $str_endArray, '" ]', $result ); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment