hello-world
webエンジニアのメモ。とりあえずやってみる。

[ruby]opmlをnokogiriで解析する方法

公開日時

Feedlyからopml形式でエクスポートできるようになったので、nokogiriを使ってパースしてみました。

  • Gemfile
source 'https://rubygems.org'

gem 'nokogiri'
  • feed.rb
require "rubygems"
require "bundler/setup"
require 'nokogiri'

class Feed
  def self.parse(xml)
    feeds = []
    opml = Nokogiri::XML(xml)
    opml.xpath("//outline").each do |x|
      if x['type'] == "rss"
        feeds << {
          title: x['title'],
          xml_url: x['xmlUrl'] ? x['xmlUrl'] : nil
        }
      end
    end
    feeds
  end
end

Feed.parse(File::open(ARGV[0]).read).each do |feed|
  p feed
end
  • opmlファイルを引数にして解析してみる
ruby feed.rb feedly.opml

{:title=>"sample", :xml_url=>"http://localhost/rss"}
・・・・

こんな感じでopmlを解析すれば、自分の登録しているフィードの一覧を取得することができます。

参考