summaryrefslogtreecommitdiff
path: root/sample/rss/re_read.rb
blob: ee54a18a88cf8ab4cf9cc7edc7ba50ab035b67fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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"