ライブブックマーク用にlivedoor天気情報RSSを変換
livedoorで天気予報のRSSが公開されたので、早速ライブブックマークに取り込んで使用してみたのだが、title要素が長くわかり難いので、クイックハックしてみました。
以下のPHPスクリプトを保存し、http://www.example.com/path/index.php?c=cityにアクセスしてみてください。
- example.com : 自分のサーバー
- path : 保存先
- city : 「1次細分区(cityタグ)」のid(詳しくはここを参照してください。)
- /*
- * livedoor 天気情報のRSSをFirefoxのライブブックマークに最適化
- * 2006/02/14 s_nobu
- * http://d.hatena.ne.jp/s_nobu/20060214/1139875247
- */
- $host = "http://weather.livedoor.com";
- require_once("XML/RSS.php");
- if(isset($_GET["c"])){
- $body = "";
- $city = $_GET["c"]; //60;
- $rss_file = $host."/forecast/rss/11/".$city.".xml"; //11は埼玉
- $rss =& new XML_RSS($rss_file);
- $rss->parse();
- $headers = $rss->getChannelInfo();
- $items = $rss->getItems();
- foreach($items as $item){
- if(!isset($item["description"])){
- $date_pattern = '/^.*rss(\d{4})(\d{2})(\d{2})$/';
- preg_match($date_pattern,$item["link"],$match);
- list($date,$y,$m,$d) = $match;
- $title = explode("-",$item["title"]);
- $forecast = trim($title[1]);
- $temperature = str_replace(mb_convert_encoding("最高気温","UTF-8"),'',trim($title[2]));
- $body .= <<<EOF
- <item>
- <title>$d : $forecast ($temperature)</title>
- <link>${item["link"]}</link>
- </item>
- EOF;
- }
- }
- }else{
- exit("都市を指定してください");
- }
- $header = <<<EOF
- <rss version="2.0">
- <channel>
- <title>${headers["title"]}</title>
- <link>${headers["link"]}</link>
- <description>${headers["description"]}</description>
- <author>livedoor Weather Team.</author>
- <language>ja</language>
- <category>weather</category>
- <generator>http://weather.livedoor.com/
- <modifier>s_nobu</modifier>
- EOF;
- $footer = <<<EOF
- </channel>
- </rss>
- EOF;
- header("Content-Type: application/xml\n\n");
- echo $header;
- echo $body;
- echo $footer;