diff options
Diffstat (limited to 'jni/ruby/sample/rss')
-rwxr-xr-x | jni/ruby/sample/rss/blend.rb | 79 | ||||
-rwxr-xr-x | jni/ruby/sample/rss/convert.rb | 69 | ||||
-rwxr-xr-x | jni/ruby/sample/rss/list_description.rb | 91 | ||||
-rwxr-xr-x | jni/ruby/sample/rss/re_read.rb | 64 | ||||
-rwxr-xr-x | jni/ruby/sample/rss/rss_recent.rb | 85 |
5 files changed, 388 insertions, 0 deletions
diff --git a/jni/ruby/sample/rss/blend.rb b/jni/ruby/sample/rss/blend.rb new file mode 100755 index 0000000..351f6f3 --- /dev/null +++ b/jni/ruby/sample/rss/blend.rb @@ -0,0 +1,79 @@ +#!/usr/bin/env ruby + +require "rss" + +feeds = [] +verbose = false +encoding = "UTF-8" + +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| + if fname == '-v' + verbose = true + next + end + rss = nil + f = File.new(fname).read + 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 << rss + end +end +processing_time = Time.now - before_time + +rss = RSS::Maker.make("1.0") do |maker| + maker.encoding = encoding + maker.channel.about = "http://example.com/blend.rdf" + maker.channel.title = "blended feeds" + maker.channel.link = "http://example.com/" + maker.channel.description = "blended feeds generated by RSS Parser" + + feeds.each do |feed| + feed.items.each do |item| + item.setup_maker(maker.items) + end + end + + maker.items.each do |item| + item.title ||= "UNKNOWN" + item.link ||= "UNKNOWN" + end + + maker.items.do_sort = true + maker.items.max_size = 15 +end +puts rss + +STDERR.puts "Used XML parser: #{RSS::Parser.default_parser}" +STDERR.puts "Processing time: #{processing_time}s" diff --git a/jni/ruby/sample/rss/convert.rb b/jni/ruby/sample/rss/convert.rb new file mode 100755 index 0000000..e6bff4c --- /dev/null +++ b/jni/ruby/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/jni/ruby/sample/rss/list_description.rb b/jni/ruby/sample/rss/list_description.rb new file mode 100755 index 0000000..990974d --- /dev/null +++ b/jni/ruby/sample/rss/list_description.rb @@ -0,0 +1,91 @@ +#!/usr/bin/env ruby + +require "nkf" +class String + # From tdiary.rb + def shorten( len = 120 ) + lines = NKF::nkf( "-t -m0 -f#{len}", self.gsub( /\n/, ' ' ) ).split( /\n/ ) + lines[0].concat( '...' ) if lines[0] and lines[1] + lines[0] + end +end + +require "rss" + +channels = {} +verbose = false + +def error(exception) + mark = "=" * 20 + mark = "#{mark} error #{mark}" + puts mark + puts exception.class + puts exception.message + puts exception.backtrace + puts mark +end + +before_time = Time.now +ARGV.each do |fname| + if fname == '-v' + verbose = true + next + end + rss = nil + f = File.new(fname).read + 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? + puts "#{fname} does not include RSS 1.0 or 0.9x/2.0" + else + begin + rss.output_encoding = "utf-8" + rescue RSS::UnknownConversionMethodError + error($!) if verbose + end + + rss = rss.to_rss("1.0") do |maker| + maker.channel.about ||= maker.channel.link + maker.channel.description ||= "No description" + maker.items.each do |item| + item.title ||= "No title" + item.link ||= "UNKNOWN" + end + end + next if rss.nil? + + rss.items.each do |item| + channels[rss.channel.title] ||= [] + channels[rss.channel.title] << item if item.description + end + end +end +processing_time = Time.now - before_time + +channels.sort do |x, y| + x[0] <=> y[0] +end[0..20].each do |title, items| + puts "Channel: #{title}" unless items.empty? + items.sort do |x, y| + x.title <=> y.title + end[0..10].each do |item| + puts " Item: #{item.title.shorten(50)}" + puts " Description: #{item.description.shorten(50)}" + end +end + +puts "Used XML parser: #{RSS::Parser.default_parser}" +puts "Processing time: #{processing_time}s" diff --git a/jni/ruby/sample/rss/re_read.rb b/jni/ruby/sample/rss/re_read.rb new file mode 100755 index 0000000..c386ab2 --- /dev/null +++ b/jni/ruby/sample/rss/re_read.rb @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby + +require "rss" + +def error(exception) + mark = "=" * 20 + mark = "#{mark} error #{mark}" + puts mark + puts exception.class + puts exception.message + puts exception.backtrace + puts mark +end + +verbose = false +before_time = Time.now + +ARGV.each do |fname| + if fname == '-v' + verbose = true + next + end + source = nil + File.open(fname) do |f| + source = f.read + end + + rss = nil + read = false + begin + rss = RSS::Parser.parse(source) + 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 feed + begin + rss = RSS::Parser.parse(source, false) + rescue RSS::Error + ## invalid feed + error($!) if verbose + end + rescue RSS::Error + error($!) if verbose + end + + if rss.nil? + puts "Invalid feed: #{fname}" + elsif !read + puts "Re-read invalid feed: #{fname}" + begin + RSS::Parser.parse(rss.to_s) + rescue RSS::Error + puts " Error occurred: #{fname}" + error($!) if verbose + end + end +end + +processing_time = Time.now - before_time + +puts "Used XML parser: #{RSS::Parser.default_parser}" +puts "Processing time: #{processing_time}s" diff --git a/jni/ruby/sample/rss/rss_recent.rb b/jni/ruby/sample/rss/rss_recent.rb new file mode 100755 index 0000000..14c8612 --- /dev/null +++ b/jni/ruby/sample/rss/rss_recent.rb @@ -0,0 +1,85 @@ +#!/usr/bin/env ruby + +require "nkf" +class String + # From tdiary.rb + def shorten( len = 120 ) + lines = NKF::nkf( "-t -m0 -f#{len}", self.gsub( /\n/, ' ' ) ).split( /\n/ ) + lines[0].concat( '...' ) if lines[0] and lines[1] + lines[0] + end +end + +require "rss" + +items = [] +verbose = false + +def error(exception) + mark = "=" * 20 + mark = "#{mark} error #{mark}" + puts mark + puts exception.class + puts exception.message + puts exception.backtrace + puts mark +end +before_time = Time.now +ARGV.each do |fname| + if fname == '-v' + verbose = true + next + end + rss = nil + f = File.new(fname).read + 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? + puts "#{fname} does not include RSS 1.0 or 0.9x/2.0" + else + begin + rss.output_encoding = "utf-8" + rescue RSS::UnknownConversionMethodError + error($!) if verbose + end + + rss = rss.to_rss("1.0") do |maker| + maker.channel.about ||= maker.channel.link + maker.channel.description ||= "No description" + maker.items.each do |item| + item.title ||= "UNKNOWN" + 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 + +items.sort do |x, y| + y[1].dc_date <=> x[1].dc_date +end[0..20].each do |channel, item| + puts "#{item.dc_date.localtime.iso8601}: " << + "#{channel.title}: #{item.title}" + puts " Description: #{item.description.shorten(50)}" if item.description +end + +puts "Used XML parser: #{RSS::Parser.default_parser}" +puts "Processing time: #{processing_time}s" |