初版(2011/03/27 0:00くらい公開)
/* warningが出るので宣言する */
$time = '';
$tstart = '';
$tend = '';
$todayPeak = '';
$peak = '';
$peakUpdate = '';
$data = getFile('http://www.tepco.co.jp/forecast/index-j.html');
foreach($data as $line) {
if ($line) {
if (preg_match('/class\=\"heading-04\"/', $line)){
$time = strip_tags($line);
$time = mb_convert_kana($time, 'ns');
preg_match_all('/([\:0-9]+)/', $time, $match);
$tstart = $match[1][0];
$tend = $match[1][1];
continue;
}
if (preg_match('/class\=\"fore_tmc01\"/', $line)){
$todaypeak = strip_tags($line);
$todaypeak = mb_convert_kana($todaypeak, 'a');
$todaypeak = preg_replace('/(\ | )/', '', $todaypeak);
preg_match('/^本日のピーク時供給力\:([\,0-9]+)万kW(.+)/', $todaypeak, $match);
$peak = preg_replace('/,/', '', $match[1]);
$peak = intval($peak);
$peakUpdate = $match[2];
break;
}
}
}
$time = strftime('%H', strtotime("-1 hour"));
$time = intval($time);
$csv = getFile('http://www.tepco.co.jp/forecast/html/images/juyo-j.csv');
foreach($csv as $line) {
if (preg_match('/'.$time.'\:00/', $line)) {
$param = explode(',', $line);
break;
}
}
$parcent = round($param[2] / $peak * 100, 2);
if ($param[2] == '0') {
print $time.'時台の使用量はまだCSVに記録されていないようです';
exit;
}
print <<<EOM
{$time}時台の使用量: {$param[2]}万kW (昨日の同時間帯: {$param[3]}万kW)
本日のピーク時供給力: {$peak}万kW {$peakUpdate}
使用率: {$parcent}%\n\n
本日の計画停電実施予定時間: {$tstart}?{$tend}
EOM;
function getFile($url) {
/*
URLのファイルを取得→UTF-8に変換→行ごとに配列にして返す
取得失敗は考慮していない
*/
$data = file_get_contents($url);
$data = mb_convert_encoding($data, 'utf-8', 'shift_jis');
$data = preg_replace('/(\r\n|\r|\n)/', "\n", $data);
$data = explode("\n", $data);
return $data;
}