summaryrefslogtreecommitdiff
path: root/sample/rss
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-03-17 10:13:25 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-03-17 10:13:25 +0000
commit966a25465aab5c2972e6c453f631a15fc2223256 (patch)
tree847090e856c9901ab2cc19251c179b9b0985e65b /sample/rss
parent53cbab048452742b537ac8bccf494630d1c184c8 (diff)
* lib/rss, test/rss:
- supported Atom. - bumped version 0.1.6 to 0.1.7. * sample/rss/convert.rb: added new sample. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12087 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample/rss')
-rwxr-xr-xsample/rss/blend.rb6
-rwxr-xr-xsample/rss/convert.rb69
-rwxr-xr-x[-rw-r--r--]sample/rss/list_description.rb13
-rwxr-xr-xsample/rss/re_read.rb10
-rwxr-xr-x[-rw-r--r--]sample/rss/rss_recent.rb19
5 files changed, 102 insertions, 15 deletions
diff --git a/sample/rss/blend.rb b/sample/rss/blend.rb
index 2fbd6ef..7d6b239 100755
--- a/sample/rss/blend.rb
+++ b/sample/rss/blend.rb
@@ -64,6 +64,12 @@ rss = RSS::Maker.make("1.0") do |maker|
item.setup_maker(maker.items)
end
end
+
+ maker.items.each do |item|
+ item.title.content ||= "UNKNOWN"
+ item.link ||= "UNKNOWN"
+ end
+
maker.items.do_sort = true
maker.items.max_size = 15
end
diff --git a/sample/rss/convert.rb b/sample/rss/convert.rb
new file mode 100755
index 0000000..e6bff4c
--- /dev/null
+++ b/sample/rss/convert.rb
@@ -0,0 +1,69 @@
+#!/usr/bin/env ruby
+
+require "rss"
+
+feeds = []
+verbose = false
+encoding = "UTF-8"
+to_version = "1.0"
+
+def error(exception)
+ mark = "=" * 20
+ mark = "#{mark} error #{mark}"
+ STDERR.puts mark
+ STDERR.puts exception.class
+ STDERR.puts exception.message
+ STDERR.puts exception.backtrace
+ STDERR.puts mark
+end
+
+before_time = Time.now
+ARGV.each do |fname|
+ case fname
+ when '-v'
+ verbose = true
+ next
+ when /^-t(0\.91|1\.0|2\.0|atom)$/
+ to_version = $1
+ next
+ end
+ rss = nil
+ f = File.read(fname)
+ begin
+ ## do validate parse
+ rss = RSS::Parser.parse(f)
+ rescue RSS::InvalidRSSError
+ error($!) if verbose
+ ## do non validate parse for invalid RSS 1.0
+ begin
+ rss = RSS::Parser.parse(f, false)
+ rescue RSS::Error
+ ## invalid RSS.
+ error($!) if verbose
+ end
+ rescue RSS::Error
+ error($!) if verbose
+ end
+ if rss.nil?
+ STDERR.puts "#{fname} does not include RSS 1.0 or 0.9x/2.0"
+ else
+ begin
+ rss.output_encoding = encoding
+ rescue RSS::UnknownConversionMethodError
+ error($!) if verbose
+ end
+ feeds << [fname, rss]
+ end
+end
+processing_time = Time.now - before_time
+
+feeds.each do |fname, rss|
+ converted_rss = rss.to_xml(to_version)
+ output_name = fname.sub(/(\.[^\.]+)$/, "-#{to_version}\\1")
+ File.open(output_name, "w") do |output|
+ output.print(converted_rss)
+ end
+end
+
+STDERR.puts "Used XML parser: #{RSS::Parser.default_parser}"
+STDERR.puts "Processing time: #{processing_time}s"
diff --git a/sample/rss/list_description.rb b/sample/rss/list_description.rb
index bb1f963..5f7069f 100644..100755
--- a/sample/rss/list_description.rb
+++ b/sample/rss/list_description.rb
@@ -56,9 +56,18 @@ ARGV.each do |fname|
rescue RSS::UnknownConversionMethodError
error($!) if verbose
end
- rss.channel.title ||= "Unknown"
+
+ rss = rss.to_rss("1.0") do |maker|
+ maker.channel.about ||= maker.channel.link
+ maker.channel.description.content ||= "No description"
+ maker.items.each do |item|
+ item.title.content ||= "No title"
+ item.link ||= "UNKNOWN"
+ end
+ end
+ next if rss.nil?
+
rss.items.each do |item|
- item.title ||= "Unknown"
channels[rss.channel.title] ||= []
channels[rss.channel.title] << item if item.description
end
diff --git a/sample/rss/re_read.rb b/sample/rss/re_read.rb
index c19a509..ee54a18 100755
--- a/sample/rss/re_read.rb
+++ b/sample/rss/re_read.rb
@@ -29,16 +29,16 @@ ARGV.each do |fname|
read = false
begin
rss = RSS::Parser.parse(source)
- puts "Re-read valid RSS: #{fname}"
+ puts "Re-read valid feed: #{fname}"
RSS::Parser.parse(rss.to_s)
read = true
rescue RSS::InvalidRSSError
error($!) if verbose
- ## do non validate parse for invalid RSS 1.0
+ ## do non validate parse for invalid feed
begin
rss = RSS::Parser.parse(source, false)
rescue RSS::Error
- ## invalid RSS.
+ ## invalid feed
error($!) if verbose
end
rescue RSS::Error
@@ -46,9 +46,9 @@ ARGV.each do |fname|
end
if rss.nil?
- puts "Invalid RSS: #{fname}"
+ puts "Invalid feed: #{fname}"
elsif !read
- puts "Re-read invalid RSS: #{fname}"
+ puts "Re-read invalid feed: #{fname}"
begin
RSS::Parser.parse(rss.to_s)
rescue RSS::Error
diff --git a/sample/rss/rss_recent.rb b/sample/rss/rss_recent.rb
index 7821df5..1b02835 100644..100755
--- a/sample/rss/rss_recent.rb
+++ b/sample/rss/rss_recent.rb
@@ -55,16 +55,19 @@ ARGV.each do |fname|
rescue RSS::UnknownConversionMethodError
error($!) if verbose
end
- rss.items.each do |item|
- if item.respond_to?(:pubDate) and item.pubDate
- class << item
- alias_method(:dc_date, :pubDate)
- end
- end
- if item.respond_to?(:dc_date) and item.dc_date
- items << [rss.channel, item]
+
+ rss = rss.to_rss("1.0") do |maker|
+ maker.channel.about ||= maker.channel.link
+ maker.channel.description.content ||= "No description"
+ maker.items.each do |item|
+ item.link ||= "UNKNOWN"
end
end
+ next if rss.nil?
+
+ rss.items.each do |item|
+ items << [rss.channel, item] if item.dc_date
+ end
end
end
processing_time = Time.now - before_time