自分のサイトの情報をRSS配信したくていろいろやってみようと調べていたらRSSって結構面白そうなので例によってちょっと実験してみます。
第1段階は頻繁に更新されて有益な地震情報のRSS配信、livedoor地震速報のRSSを読み込んでWEBページで表示してみるというものです。
URIはhttp://weather.livedoor.com/forecast/rss/earthquake.xmlです。(クリックすると表示されます。
中身は↓。途中のデータは省略しています。
<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <title>地震速報 - livedoor 天気情報</title> <link>http://weather.livedoor.com/earthquake/?r=rss</link> <description>livedoor 天気情報が配信する全国の地震速報</description> <lastBuildDate>Thu, 21 Aug 2008 12:34:16 +0900</lastBuildDate> <author>livedoor Weather Team.</author> <language>ja</language> <category>天気情報</category> <generator>http://weather.livedoor.com/</generator> <copyright>Copyright (C) 1996-2008 livedoor Co.,Ltd. All rights reserved.</copyright> <image> <title>livedoor 天気情報</title> <link>http://weather.livedoor.com</link> <url>http://image.weather.livedoor.com/img/cmn/livedoor.gif</url> <width>118</width> <height>26</height> </image> <ldWeather:provider name="(株)ハレックス" link="http://www.halex.co.jp/halexbrain/weather/" /> <ldWeather:provider name="日本気象協会" link="http://tenki.jp/" /> <ldWeather:notes link="http://weather.livedoor.com/weather_hacks/forum.html">livedoor 天気情報 Weather Hacks での疑問点などは http://weather.livedoor.com/weather_hacks/forum.html で質問してみましょう!</ldWeather:notes> <item> <title>[ PR ] ブログでお天気を簡単ゲット!</title> <link>http://weather.livedoor.com/weather_hacks/plugin.html</link> <category>PR</category> <description> livedoor 天気情報「Weather Hacks」では一般のブロガーの皆さん向けにブログでお天気を表示できる、お天気プラグインを公開しました。使い方はとってもカンタン!手順に沿って作成したHTMLソースを自分のブログに貼り付けるだけです! </description> <image> <title>お天気プラグイン - livedoor 天気情報</title> <link>http://weather.livedoor.com/weather_hacks/plugin.html</link> <url>http://image.weather.livedoor.com/img/weather_hacks/news_title.gif</url> <width>151</width> <height>50</height> </image> <pubDate>Thu, 21 Aug 2008 00:00:00 +0900</pubDate> </item> <item> <title>21日12:29 [ 最大震度 ] 震度 1 [ 震源地 ] 釧路沖</title> <link>http://weather.livedoor.com/earthquake/2008-08-21-12-29.html?r=rss</link> <category>地震速報</category> <description>8月21日12時29分、釧路沖を震源とする最大震度1の地震が発生しました。</description> <ldWeather:earthquake epicenter="釧路沖" maximumintensity="1" date="Thu, 21 Aug 2008 12:29:00 +0900" /> <day>Thursday</day> <pubDate>Thu, 21 Aug 2008 12:34:16 +0900</pubDate> </item> <item> <title>21日11:29 [ 最大震度 ] 震度 1 [ 震源地 ] 八丈島東方沖</title> <link>http://weather.livedoor.com/earthquake/2008-08-21-11-29.html?r=rss</link> <category>地震速報</category> <description>8月21日11時29分、八丈島東方沖を震源とする最大震度1の地震が発生しました。</description> <ldWeather:earthquake epicenter="八丈島東方沖" maximumintensity="1" date="Thu, 21 Aug 2008 11:29:00 +0900" /> <day>Thursday</day> <pubDate>Thu, 21 Aug 2008 11:34:27 +0900</pubDate> </item> <item> <title>06日13:04 [ 最大震度 ] 震度 2 [ 震源地 ] 宮城県北部</title> <link>http://weather.livedoor.com/earthquake/2008-08-06-13-04.html?r=rss</link> <category>地震速報</category> <description>8月6日13時4分、宮城県北部を震源とする最大震度2の地震が発生しました。</description> <ldWeather:earthquake epicenter="宮城県北部" maximumintensity="2" date="Wed, 06 Aug 2008 13:04:00 +0900" /> <day>Wednesday</day> <pubDate>Wed, 06 Aug 2008 13:10:14 +0900</pubDate> </item> </channel> </rss>
PRとかも入っていて複雑そうですが調べてみると結構簡単です。
RSS 1.0は複雑らしいがRSS2.0はこんな感じです。
channel内のitem要素がひとつの地震を表している。PRもあるけどitem要素のcategoryが「地震速報」のものだけ処理すればOKっぽい。
地震の情報はldWeather:earthquakeからもらえば良さそう。
RSSを処理するのにいろいろと使えそうなライブラリがあるのだけれど今回はRubyに標準添付されているrexmlを使用することにしました。
まずは端末で動かそう。REXML Tutorial - Homeを見ながらソースコードを書いてみました。こんな感じです。FedoraはUTF-8なのであまり気にせずにコーディングするだけで動きました。
require 'open-uri' require 'rexml/document' if $0 == __FILE__ # LiveDoorさんのRSSのURI EARTHQUAKE_URI = "http://weather.livedoor.com/forecast/rss/earthquake.xml" # REXML doc = REXML::Document.new open(EARTHQUAKE_URI) c = 0 # ldWeather:earthquakeについて処理を行う doc.elements.each("/rss/channel/item/ldWeather:earthquake") do |quake| c += 1 print <<"EOT" #{c} #{quake.attributes["date"]} #{quake.attributes["maximumintensity"]} #{quake.attributes["epicenter"]} EOT end end
実行結果↓。途中省略しています。
1
Thu, 21 Aug 2008 17:48:00 +0900
2
京都府南部
2
Thu, 21 Aug 2008 12:29:00 +0900
1
釧路沖
途中省略
51
Wed, 06 Aug 2008 17:23:00 +0900
1
宮城県北部
上記をこのページで表示してみました。実行結果は↓
他のページに比べてこのページの表示に時間がかかるのはlivedoorのRSSを読みに行っているからと思われます。
震源が空白のデータは震源不明とのことです。
| No. | 日時 | 最大震度 | 震源 |
|---|---|---|---|
| 1 | Wed, 28 Jul 2010 08:06:00 +0900 | 3 | 十勝沖 |
| 2 | Wed, 28 Jul 2010 07:58:00 +0900 | 1 | 十勝沖 |
| 3 | Tue, 27 Jul 2010 21:48:00 +0900 | 2 | 新潟県上中越沖 |
| 4 | Tue, 27 Jul 2010 21:13:00 +0900 | 1 | 千葉県北東部 |
| 5 | Tue, 27 Jul 2010 09:06:00 +0900 | 1 | 神奈川県西部 |
| 6 | Tue, 27 Jul 2010 08:31:00 +0900 | 3 | 岩手県沖 |
| 7 | Tue, 27 Jul 2010 07:29:00 +0900 | 1 | 留萌地方南部 |
| 8 | Mon, 26 Jul 2010 12:47:00 +0900 | 1 | 根室半島南東沖 |
| 9 | Mon, 26 Jul 2010 10:40:00 +0900 | 2 | 徳島県南部 |
| 10 | Mon, 26 Jul 2010 10:00:00 +0900 | 1 | 大分県西部 |
| 11 | Mon, 26 Jul 2010 07:18:00 +0900 | 1 | 長野県南部 |
| 12 | Mon, 26 Jul 2010 02:04:00 +0900 | 2 | 長野県南部 |
| 13 | Sun, 25 Jul 2010 22:44:00 +0900 | 1 | 群馬県南部 |
| 14 | Sun, 25 Jul 2010 17:11:00 +0900 | 3 | 熊本県阿蘇地方 |
| 15 | Sun, 25 Jul 2010 17:06:00 +0900 | 2 | 熊本県阿蘇地方 |
| 16 | Sun, 25 Jul 2010 09:35:00 +0900 | 1 | 岩手県内陸北部 |
| 17 | Sun, 25 Jul 2010 09:05:00 +0900 | 1 | 栃木県南部 |
| 18 | Sat, 24 Jul 2010 13:21:00 +0900 | 1 | 岐阜県美濃東部 |
| 19 | Sat, 24 Jul 2010 08:15:00 +0900 | フィリピン付近 | |
| 20 | Sat, 24 Jul 2010 07:51:00 +0900 | フィリピン付近 | |
| 21 | Sat, 24 Jul 2010 07:08:00 +0900 | フィリピン付近 | |
| 22 | Fri, 23 Jul 2010 21:24:00 +0900 | 2 | 釧路地方中南部 |
| 23 | Fri, 23 Jul 2010 17:42:00 +0900 | 1 | 和歌山県北部 |
| 24 | Fri, 23 Jul 2010 14:25:00 +0900 | 3 | 高知県西部 |
| 25 | Fri, 23 Jul 2010 06:06:00 +0900 | 5弱 | 千葉県北東部 |
| 26 | Fri, 23 Jul 2010 01:37:00 +0900 | 1 | 和歌山県北部 |
| 27 | Thu, 22 Jul 2010 16:57:00 +0900 | 1 | 長崎県南西部 |
| 28 | Thu, 22 Jul 2010 06:16:00 +0900 | 1 | 沖縄本島近海 |
| 29 | Thu, 22 Jul 2010 00:18:00 +0900 | 1 | 静岡県中部 |
| 30 | Wed, 21 Jul 2010 06:19:00 +0900 | 4 | 奈良県 |
| 31 | Wed, 21 Jul 2010 02:39:00 +0900 | 1 | 紀伊水道 |
| 32 | Tue, 20 Jul 2010 19:13:00 +0900 | 1 | 宮古島近海 |
| 33 | Tue, 20 Jul 2010 13:22:00 +0900 | 1 | 和歌山県北部 |
| 34 | Tue, 20 Jul 2010 11:13:00 +0900 | 1 | 福島県浜通り |
| 35 | Mon, 19 Jul 2010 17:22:00 +0900 | 1 | 北海道北西沖 |
| 36 | Sun, 18 Jul 2010 22:04:00 +0900 | ニューギニア付近 | |
| 37 | Sun, 18 Jul 2010 22:03:00 +0900 | 2 | 与那国島近海 |
| 38 | Sun, 18 Jul 2010 19:12:00 +0900 | 1 | 茨城県沖 |
| 39 | Sun, 18 Jul 2010 10:52:00 +0900 | 1 | 山形県沖 |
| 40 | Sat, 17 Jul 2010 19:01:00 +0900 | 2 | 岩手県内陸南部 |
| 41 | Fri, 16 Jul 2010 20:24:00 +0900 | 2 | 伊豆大島近海 |
| 42 | Fri, 16 Jul 2010 16:57:00 +0900 | 1 | 山梨県東部・富士五湖 |
| 43 | Fri, 16 Jul 2010 06:57:00 +0900 | 2 | 千葉県東方沖 |
| 44 | Fri, 16 Jul 2010 06:18:00 +0900 | 2 | 釧路沖 |
| 45 | Thu, 15 Jul 2010 18:54:00 +0900 | 1 | トカラ列島近海 |
| 46 | Thu, 15 Jul 2010 04:57:00 +0900 | 2 | 熊本県阿蘇地方 |
| 47 | Thu, 15 Jul 2010 04:09:00 +0900 | 2 | 熊本県阿蘇地方 |
| 48 | Wed, 14 Jul 2010 20:55:00 +0900 | 2 | 伊予灘 |
| 49 | Wed, 14 Jul 2010 12:59:00 +0900 | 1 | 兵庫県南東部 |
| 50 | Tue, 13 Jul 2010 22:01:00 +0900 | 1 | 茨城県沖 |
| 51 | Tue, 13 Jul 2010 21:28:00 +0900 | 2 | 伊豆大島近海 |