<?php
header('Content-Type: text/xml;charset=UTF-8');
$html = file_get_contents('https://earthquake.tenki.jp/bousai/earthquake/entries/level-3/');
$base_url = 'https://earthquake.tenki.jp';
$now = new DateTime();
$now_rfc2822 = $now->format(DateTimeInterface::RFC822);
$now_PermaLink = $now->format('A').' Asia/Tokyo+9 '.$now->format('D M y');
// RSS ヘッダ
$RSS =<<< EOD
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>過去の地震情報 震度3以上(日付の新しい順) - 日本気象協会 tenki.jp</title>
<link>https://earthquake.tenki.jp/bousai/earthquake/entries/level-3/</link>
<language>ja</language>
<lastBuildDate>${now_rfc2822}</lastBuildDate>
<pubDate>${now_rfc2822}</pubDate>
<guid isPermaLink="false">${now_PermaLink}</guid>
EOD;
# テーブルの抜き出し
if(preg_match('/<table class="earthquake\-entries\-table">[\s\S]*?<\/table>/m',$html, $matches)){
$earth_quake_table = $matches[0];
}else{
$earth_quake_table = '';
}
if(preg_match_all('/<tr>([\s\S]*?)<\/tr>/m',$earth_quake_table, $matches)){
$trs = $matches[1];
for($tr=1; $tr<count($trs); $tr++){
if(preg_match('/datetime"><a href="([^"]*)" class="text-link">/', $trs[$tr], $matches)){
$link = $base_url.$matches[1];
}
if(preg_match('/datetime">.*class="text-link">(.*)<\/a>/', $trs[$tr], $matches)){
$datetime = $matches[1];
// 欠則時は恐らく<span class="grey">が入る
if(preg_match('/<span class="grey">(.*)<\/span>/', $datetime, $matches)){
$datetime = $matches[1];
}
}
if(preg_match('/center">(.*)<\/td>/',$trs[$tr],$matches)){
$center = $matches[1];
// 欠則時は恐らく<span class="grey">が入る
if(preg_match('/<span class="grey">(.*)<\/span>/', $center, $matches)){
$center = $matches[1];
}
}
if(preg_match('/magnitude">(.*)<\/td>/',$trs[$tr],$matches)){
$magnitude = $matches[1];
// 欠則時は恐らく<span class="grey">が入る
if(preg_match('/<span class="grey">(.*)<\/span>/', $magnitude, $matches)){
$magnitude = $matches[1];
}
}
if(preg_match('/max\-level">.*alt="(.*?)"/',$trs[$tr],$matches)){
$max_level = $matches[1];
// 欠則時は恐らく<span class="grey">が入る
if(preg_match('/<span class="grey">(.*)<\/span>/', $max_level, $matches)){
$max_level = $matches[1];
}
}
sscanf($datetime,'%d年%0d月%0d日%0d時%0d分',$y,$m,$d,$h,$i);
$ocdate = new DateTime("$y-$m-$d $h:$i");
$occurrence_date = $ocdate->format(DateTimeInterface::RFC822);
$RSS .=<<< EOD
<item>
<title>[震源地] ${center} [最大震度] 震度${max_level} ${magnitude}(${datetime}発生) - tenki.jp</title>
<link>${link}</link>
<description/>
<pubDate>${occurrence_date}</pubDate>
</item>
EOD;
}
$RSS .=<<< EOD
</channel>
</rss>
EOD;
print $RSS;
}
?>