使用Ruby on Rails解析及创建RSS

最近想用Rails写一个类似lilina的东东,搜集了关于用Ruby解析及创建RSS的一些资源,原来Ruby自带了一个RSS解析器和生成器,这给我们带来了极大的方便。

Ruby RSS 解析

ruby 代码
  1. require 'rss/1.0'         
  2. require 'rss/2.0'         
  3. require 'open-uri'         
  4.         
  5. feed= "http://www.iteye.com/blog/rss_blog/41837" # url或本地XML文件      
  6. content = ""         
  7.   open(feed) do |s|       
  8.     content = s.read       
  9.   end       
  10. rss = RSS::Parser.parse(content, false# false表示不验证feed的合法性  

上面的代码会将feed中的XML下载并解析,解析后的内容保存在对象rss中,下面我们来操作该对象。

ruby 代码
  1. channel = rss.channel   
  2. items = rss.items # rss.channel.items亦可   
  3.   
  4. puts "频道信息"  
  5. puts "标题: #{channel.title}"  
  6. puts "链接: #{channel.link}"  
  7. puts "描述: #{channel.description}"  
  8. puts "更新时间: #{channel.date}"  
  9. puts "文章数量: #{items.size}"  
  10.   
  11. for i in 0 ... items.size   
  12.   puts "----------- 文章#{i} -----------"  
  13.   puts "\t标题: #{items[i].title}"  
  14.   puts "\t链接: #{items[i].link}"  
  15.   puts "\t发表时间: #{items[i].date}"  
  16.   puts "\t内容: #{items[i].description}"  
  17. end  

 对于RSS中channel和item的其它api操作,可以参照RSS的中文规范

 如果觉得上面的内容包含了太多的HTML字符,不方便查看的话,可以调用下面的to_html方法,将返回结果显示在view中。 

ruby 代码
  1. class RSS::Rss   
  2.   def to_html   
  3.     max_description_length = 100   
  4.   
  5.     html = "<h4><a href='#{channel.link}'>#{channel.title}</a></h4>"  
  6.     html << "<small>Updated on #{channel.date.strftime('%m/%d/%Y')}</small>" \   
  7.             if channel.date   
  8.     html << "<p>#{channel.description}</p>"  
  9.     html << "<ol>"  
  10.   
  11.     channel.items.each do |i|   
  12.       html << "<li><strong><a href='#{i.link}'>#{i.title}</a></strong><br/>"  
  13.       html << "<small>Added on #{i.date.strftime("%m/%d/%Y")} at \  
  14. #{i.date.strftime("%I:%M%p")}</small><br/>" if i.date   
  15.       desc_text = i.description.gsub(/<[^>]+>/,"").squeeze(" ").strip   
  16.       if desc_text.length > max_description_length   
  17.         desc_text = desc_text[0,max_description_length] + "…"  
  18.       else  
  19.         desc_text = i.description   
  20.       end  
  21.       html << "#{desc_text}"  
  22.       html << "</li>"  
  23.     end  
  24.   
  25.     html << "</ol>"  
  26.     html   
  27.   end  
  28. end  

Ruby RSS 创建

创建RSS也非常容易,代码如下:

ruby 代码
require 'rss/maker'               version = "2.0" # ["0.9", "1.0", "2.0"]        destination = "rss_cpc.xml"              content = RSS::Maker.make(version) do |m|          m.channel.title = "使用Ruby解析及创建RSS"         m.channel.link = "http://cpccai.iteye.com"         m.channel.description = "使用Ruby解析及创建RSS"         m.items.do_sort = true # 文章按日期排序                 i = m.items.new_item          i.title = "使用Ruby解析RSS"         i.link = "http://cpccai.iteye.com/blog/137941"         i.date = Time.parse("2007/11/01 11:01")                 i = m.items.new_item          i.title = "使用Ruby创建RSS"         i.link = "http://cpccai.iteye.com/blog/137941"         i.date = Time.now        end              File.open(destination,"w"do |f|          f.write(content)        end   

 

运行后可以在文件所在目录找到生成的rss_cpc.xml文件。 
 
 

参考文献:

1.  Ruby RSS - Eating feeds tastefully

2.  RSS教程 RSS基础知识及相关规范介绍

3.  Ruby RSS相关网络资源 http://directory.fsf.org/project/rubyrss/ http://rubyforge.org/frs/?group_id=1624

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值