Class: NfoController

Inherits:
Object
  • Object
show all
Defined in:
lib/dvdprofiler2xbmc/controllers/nfo_controller.rb

Overview

Synopsis

This is the heart and soul of the application. This is where the different media meta data are merged into the info (.nfo) format needed by XBMC.

TODO: This class is rather large and should be refactored.

the @info hash has keys that map directly to the .nfo file

Usage:

controller = NfoController.new(media)
controller.update
puts controller.isbn
puts controller.imdb_id

or

NfoController.update(media)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media) ⇒ NfoController

Synopsis



26
27
28
29
30
31
32
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 26

def initialize(media)
  @media = media
  @info = Hash.new
  @xbmc_info = XbmcInfo.new(@media.path_to(:nfo))
  self.isbn = @xbmc_info.movie['isbn']
  self.imdb_id = @xbmc_info.movie['id']
end

Class Method Details

.update(media) ⇒ Object

Synopsis



21
22
23
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 21

def self.update(media)
  NfoController.new(media).update
end

Instance Method Details

#imdb_idObject

Synopsis

return the IMDB ID or nil



97
98
99
100
101
102
103
104
105
106
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 97

def imdb_id
  unless @info['id'].blank?
    # make sure is not an array
    @media.imdb_id ||= [@info['id'].to_s].flatten.uniq.compact.first
  end
  unless @media.imdb_id.blank? || (@media.imdb_id.to_s =~ /^tt\d+$/)|| (@media.imdb_id.to_s =~ /^\d+$/)
    AppConfig[:logger].warn { "Attempting to return invalid IMDB ID: \"#{@media.imdb_id}\"" }
  end
  @media.imdb_id
end

#imdb_id=(ident) ⇒ Object

Synopsis

set the IMDB ID



110
111
112
113
114
115
116
117
118
119
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 110

def imdb_id=(ident)
  if ident.blank?
    @media.imdb_id = nil
  elsif (ident.to_s =~ /^tt\d+$/) || (ident.to_s =~ /^\d+$/)
    @media.imdb_id = ident.to_s
  else
    AppConfig[:logger].warn { "Attempting to set invalid IMDB ID: \"#{ident}\"" }
  end
  @media.imdb_id
end

#isbnObject

Synopsis

return the ISBN or nil



81
82
83
84
85
86
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 81

def isbn
  unless @info['isbn'].blank?
    @media.isbn ||= [@info['isbn']].flatten.uniq.compact.first.to_s
  end
  @media.isbn
end

#isbn=(n) ⇒ Object

Synopsis

set the ISBN



90
91
92
93
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 90

def isbn=(n)
  @media.isbn = n.to_s unless n.blank?
  @media.isbn
end

#updateObject

Synopsis

merge meta-data from the DVD Profiler collection.xml and from IMDB into the @movie hash



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
70
71
72
73
74
75
76
77
# File 'lib/dvdprofiler2xbmc/controllers/nfo_controller.rb', line 37

def update
  result = false
  begin
    AppConfig[:logger].info { "\n#{@media.title}" }
    @info.merge!(@xbmc_info.movie)

    # load any manually cached IDs
    self.isbn ||= load_isbn_id
    self.imdb_id ||= load_imdb_id

    box_set_parent_titles = []
    production_years = []
    released_years = []

    dvdprofiler_info = load_dvdprofiler_info
    unless dvdprofiler_info.nil?
      box_set_parent_titles = dvdprofiler_info.box_set_parent_titles
      production_years = dvdprofiler_info.production_years
      released_years = dvdprofiler_info.released_years
    end

    imdb_info = load_imdb_info(box_set_parent_titles, production_years, released_years)
    unless imdb_info.nil?
      self.imdb_id ||= imdb_info.imdb_id
    end

    tmdb_info = load_tmdb_info

    @info = merge(@info, dvdprofiler_info, imdb_info, tmdb_info)

    # map any genres
    @info['genre'] = remap_genres(@info['genre'])

    save
    result = true
  rescue Exception => e
    AppConfig[:logger].error { "Error updating \"#{@media.path_to(:nfo)}\" - " + e.to_s + "\n" + e.backtrace.join("\n") }
    raise e
  end
  result
end