2004-01-27 22:46:13 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require "nkf"
|
|
|
|
class String
|
2004-11-27 03:41:32 -05:00
|
|
|
# From tdiary.rb
|
|
|
|
def shorten( len = 120 )
|
2009-05-16 04:50:38 -04:00
|
|
|
lines = NKF::nkf( "-t -m0 -f#{len}", self.gsub( /\n/, ' ' ) ).split( /\n/ )
|
2004-11-27 03:41:32 -05:00
|
|
|
lines[0].concat( '...' ) if lines[0] and lines[1]
|
|
|
|
lines[0]
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2005-07-22 02:14:48 -04:00
|
|
|
require "rss"
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
channels = {}
|
|
|
|
verbose = false
|
|
|
|
|
|
|
|
def error(exception)
|
2004-11-27 03:41:32 -05:00
|
|
|
mark = "=" * 20
|
|
|
|
mark = "#{mark} error #{mark}"
|
|
|
|
puts mark
|
|
|
|
puts exception.class
|
|
|
|
puts exception.message
|
|
|
|
puts exception.backtrace
|
|
|
|
puts mark
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before_time = Time.now
|
|
|
|
ARGV.each do |fname|
|
2004-11-27 03:41:32 -05:00
|
|
|
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
|
2009-05-16 04:50:38 -04:00
|
|
|
rss.output_encoding = "utf-8"
|
2004-11-27 03:41:32 -05:00
|
|
|
rescue RSS::UnknownConversionMethodError
|
|
|
|
error($!) if verbose
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
rss = rss.to_rss("1.0") do |maker|
|
|
|
|
maker.channel.about ||= maker.channel.link
|
2007-08-04 23:03:05 -04:00
|
|
|
maker.channel.description ||= "No description"
|
2007-03-17 06:13:25 -04:00
|
|
|
maker.items.each do |item|
|
2007-08-04 23:03:05 -04:00
|
|
|
item.title ||= "No title"
|
2007-03-17 06:13:25 -04:00
|
|
|
item.link ||= "UNKNOWN"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next if rss.nil?
|
|
|
|
|
2004-11-27 03:41:32 -05:00
|
|
|
rss.items.each do |item|
|
|
|
|
channels[rss.channel.title] ||= []
|
|
|
|
channels[rss.channel.title] << item if item.description
|
|
|
|
end
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
processing_time = Time.now - before_time
|
|
|
|
|
|
|
|
channels.sort do |x, y|
|
2004-11-27 03:41:32 -05:00
|
|
|
x[0] <=> y[0]
|
2004-01-27 22:46:13 -05:00
|
|
|
end[0..20].each do |title, items|
|
2004-11-27 03:41:32 -05:00
|
|
|
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
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2004-04-06 23:36:38 -04:00
|
|
|
puts "Used XML parser: #{RSS::Parser.default_parser}"
|
|
|
|
puts "Processing time: #{processing_time}s"
|