~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to sample/rss/convert.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require "rss"
 
4
 
 
5
feeds = []
 
6
verbose = false
 
7
encoding = "UTF-8"
 
8
to_version = "1.0"
 
9
 
 
10
def error(exception)
 
11
  mark = "=" * 20
 
12
  mark = "#{mark} error #{mark}"
 
13
  STDERR.puts mark
 
14
  STDERR.puts exception.class
 
15
  STDERR.puts exception.message
 
16
  STDERR.puts exception.backtrace
 
17
  STDERR.puts mark
 
18
end
 
19
 
 
20
before_time = Time.now
 
21
ARGV.each do |fname|
 
22
  case fname
 
23
  when '-v'
 
24
    verbose = true
 
25
    next
 
26
  when /^-t(0\.91|1\.0|2\.0)$/
 
27
    to_version = $1
 
28
    next
 
29
  end
 
30
  rss = nil
 
31
  f = File.read(fname)
 
32
  begin
 
33
    ## do validate parse
 
34
    rss = RSS::Parser.parse(f)
 
35
  rescue RSS::InvalidRSSError
 
36
    error($!) if verbose
 
37
    ## do non validate parse for invalid RSS 1.0
 
38
    begin
 
39
      rss = RSS::Parser.parse(f, false)
 
40
    rescue RSS::Error
 
41
      ## invalid RSS.
 
42
      error($!) if verbose
 
43
    end
 
44
  rescue RSS::Error
 
45
    error($!) if verbose
 
46
  end
 
47
  if rss.nil?
 
48
    STDERR.puts "#{fname} does not include RSS 1.0 or 0.9x/2.0"
 
49
  else
 
50
    begin
 
51
      rss.output_encoding = encoding
 
52
    rescue RSS::UnknownConversionMethodError
 
53
      error($!) if verbose
 
54
    end
 
55
    feeds << [fname, rss]
 
56
  end
 
57
end
 
58
processing_time = Time.now - before_time
 
59
 
 
60
feeds.each do |fname, rss|
 
61
  converted_rss = rss.to_xml(to_version)
 
62
  output_name = fname.sub(/(\.[^\.]+)$/, "-#{to_version}\\1")
 
63
  File.open(output_name, "w") do |output|
 
64
    output.print(converted_rss)
 
65
  end
 
66
end
 
67
 
 
68
STDERR.puts "Used XML parser: #{RSS::Parser.default_parser}"
 
69
STDERR.puts "Processing time: #{processing_time}s"