summaryrefslogtreecommitdiff
path: root/jni/ruby/sample/rss/convert.rb
blob: e6bff4c623dbcf6d8be2fe16c0ae614d8f8dcc56 (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
65
66
67
68
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"