1/ Exploiter les flux AIRAISSESESSE

Ici, nous allons exploiter les syndications des advisories FreeBSD, et nous en afficherons qu'un nombre prédéfinis :

#!/usr/bin/env ruby 
#
# Copyright (c) 2008 twisla <twisla@gcu.info>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
require 'open-uri'
require 'rexml/document'
 
# "config" variables
url = ARGV[0] || "http://www.freebsd.org/security/advisories.rdf"
entries = 10
 
# create feed REXML document
feed = REXML::Document.new(open(url).read)
 
# parse rss xml structure
# <channel>
#   <title>blabla</title>
#   <link>http://blabla</link>
#   <description>blabla</description>
# </channel>
#
# <item>
#   <title>blabla</title>
#   <link>http://blabla<link>
# </item>
 
# output header
[ "title", "link" , "description" ].each do |e|
  print feed.root.elements["channel"].elements[e].text + ' '
end
puts
 
# output desired number of entries
feed.root.each_element("//item") do |e|
  entries -= 1
  break unless entries
  puts "- #{e.elements["title"].text} #{e.elements["link"].text}"
end

Je vous laisse le soin d'aller plus loin en vous référant aux documentations suivantes :

A noter qu'il existe aussi une lib 'rss' dans la stdlib ruby, mais qu'elle ne voulait pas parser correctement ce advisories.rdf lors de mes tests.

Enjoy the AIRESSESSE life lutin

codaz/ruby/ruby.txt · Last modified: 2012/11/24 20:11 by sbz