ライブブックマーク用にlivedoor天気情報RSSを変換

livedoorで天気予報のRSSが公開されたので、早速ライブブックマークに取り込んで使用してみたのだが、title要素が長くわかり難いので、クイックハックしてみました。


実行するにはPEARXML_RSSが必要です。

以下のPHPスクリプトを保存し、http://www.example.com/path/index.php?c=cityにアクセスしてみてください。

  • example.com : 自分のサーバー
  • path : 保存先
  • city : 「1次細分区(cityタグ)」のid(詳しくはここを参照してください。)


  1.  <?php
  2.  /*
  3.   * livedoor 天気情報のRSSFirefoxライブブックマークに最適化
  4.   *  2006/02/14 s_nobu
  5.  http://d.hatena.ne.jp/s_nobu/20060214/1139875247
  6.  */
  7.   
  8.    $host = "http://weather.livedoor.com";
  9.   
  10.    require_once("XML/RSS.php");
  11.   
  12.    if(isset($_GET["c"])){
  13.      $body = "";
  14.      $city = $_GET["c"]; //60;
  15.      $rss_file = $host."/forecast/rss/11/".$city.".xml"; //11は埼玉
  16.      $rss =& new XML_RSS($rss_file);
  17.      $rss->parse();
  18.      $headers = $rss->getChannelInfo();
  19.      $items = $rss->getItems();
  20.      foreach($items as $item){
  21.        if(!isset($item["description"])){
  22.          $date_pattern = '/^.*rss(\d{4})(\d{2})(\d{2})$/';
  23.          preg_match($date_pattern,$item["link"],$match);
  24.          list($date,$y,$m,$d) = $match;
  25.          $title = explode("-",$item["title"]);
  26.          $forecast = trim($title[1]);
  27.          $temperature = str_replace(mb_convert_encoding("最高気温","UTF-8"),'',trim($title[2]));
  28.          $body .= <<<EOF
  29.      <item>
  30.        <title>$d : $forecast ($temperature)</title>
  31.        <link>${item["link"]}</link>
  32.      </item>
  33.  
  34.  EOF;
  35.        }
  36.      }
  37.    }else{
  38.      exit("都市を指定してください");
  39.    }
  40.   
  41.    $header = <<<EOF
  42.  <rss version="2.0">
  43.    <channel>
  44.      <title>${headers["title"]}</title>
  45.      <link>${headers["link"]}</link>
  46.      <description>${headers["description"]}</description>
  47.      <author>livedoor Weather Team.</author>
  48.      <language>ja</language>
  49.      <category>weather</category>
  50.      <generator>http://weather.livedoor.com/
  51.      <modifier>s_nobu</modifier>
  52.  
  53.  EOF;
  54.  
  55.    $footer = <<<EOF
  56.    </channel>
  57.  </rss>
  58.   
  59.  EOF;
  60.   
  61.    header("Content-Type: application/xml\n\n")
  62.    echo $header;
  63.    echo $body;
  64.    echo $footer;
  65.   
  66.  ?>