Class: XMLTV::XmlWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/xmltv/xmltv.rb

Constant Summary collapse

With_lang =
[  'title','sub-title','desc','category','language','orig-language','country','premiere','last-chance']
Programme_dtd =
[
  ['title' ],
  [ 'sub-title' ],
  [ 'desc' ],
  [ 'credits', nil, [
    'director', 
    'actor',
    'writer',
    'adapter',
    'producer',
    'presenter', 
    'commentator',
    'guest'
    ]
  ],
  ['date'],
  ['category' ] ,
  ['language' ] ,
  ['orig-language' ] ,
  [ 'length', [ 'units' ] ],
  [ 'icon', [ 'src', 'width', 'height' ] ], 
  [ 'url'],
  [ 'country' ] , 
  [ 'episode-num', [ 'system' ] ], 
  [ 'video', [], [
    'present',
    'colour', 
    'aspect',
    'quality'
    ]
  ],  
  [ 'audio' , [],  [
    'present',
    'stereo'
    ]
  ],
  ['previously-shown', ['start', 'channel' ]],
  ['premiere' ],
  [ 'last-chance' ],
  [ 'new' ],
  [ 'subtitles', [ 'type' ], [ 'language' ] ], 
  [ 'rating', [ 'system' ] ,  [ 'value', 'icon' ] ],
  [ 'star-rating', [],  [ 'value', 'icon' ] ]
]

Instance Method Summary collapse

Constructor Details

#initialize(grabber) ⇒ XmlWriter

Returns a new instance of XmlWriter.



231
232
233
234
235
236
237
238
239
# File 'lib/xmltv/xmltv.rb', line 231

def initialize(grabber)
  @grabber = grabber
  @doc = doc = REXML::Document.new
  doc << REXML::XMLDecl.new("1.0", "UTF-8")
  dtd = grabber.config['dtd'] || 'file:///usr/share/xmltv/xmltv.dtd'
  doc << REXML::DocType.new('tv', %Q{SYSTEM "#{dtd}"})
  @el_tv = doc.add_element( 'tv' )
  @el_tv.attributes['generator-info-name'] = 'xmltv.rb'
end

Instance Method Details

#add_dtd(progdata) ⇒ Object

Raises:

  • (ArgumentError)


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/xmltv/xmltv.rb', line 240

def add_dtd(progdata)
  raise ArgumentError.new("add_dtd must have a Progdata") unless progdata.is_a?(Progdata)
  p = @el_tv.add_element('programme')
  %w{ start stop }.each do |word|
    p.attributes[word] = progdata[word].strftime( '%Y%m%d%H%M%S %Z')
  end
  p.attributes['channel'] = progdata['channel']
  Programme_dtd.each do |el|
    name , attrs , elements = el
#        STDERR.puts name
    next if (cur = progdata[name]).empty?
    wl = With_lang.include?(name)
    curS = cur.is_a?(String)
    if wl && curS
      next if cur.strip.empty?
    end
      
    el = p.add_element(name)
    el.text = cur if curS
    if wl
      el.attributes['lang'] = @grabber.lang
    end
    if attrs
      attrs.each do |a|
        el.attributes[a] = cur[a] if cur[a]
      end
    end
    if elements
      elements.each do |e|
        case cur[e]
          when String
            el.add_element(e).text = cur[e]
          when Array
            cur[e].each do |r|
              el.add_element(e).text = r
            end
        end
      end
    end
  end
end

#write_file(pda, chan_id) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/xmltv/xmltv.rb', line 281

def write_file(pda, chan_id)
  pda.each do |progdata|
    begin
      add_dtd(progdata)
    rescue StandardError => exc
      STDERR.puts exc, exc.message, exc.backtrace, '===='
      PP.pp(progdata, STDERR)
      raise
    end
  end
  file = ">#{@grabber.outputfile(chan_id)}"
  errorfile = "/var/tmp/xmllint-errors-#{Time.now.to_i}"
  if XmltvOptions.validate
    IO.popen(" ( xmllint --valid - | tv_sort #{file} ) 2>#{errorfile}", 'w') do |h|  
      write_xml h
    end
    if test(?s, errorfile)
      raise ValidateError.new("zie #{errorfile}")
    end
  else
    File.open("#{@grabber.outputfile(chan_id)}", 'w') do |h|
      write_xml h
    end
  end
end

#write_xml(h, indent = -1)) ⇒ Object



306
307
308
309
# File 'lib/xmltv/xmltv.rb', line 306

def write_xml(h, indent = -1)
  @doc.write(h, indent)
  h.puts
end