Class: TivoHMO::Adapters::Plex::Category

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport, MonitorMixin, TivoHMO::API::Container, Config::Mixin
Defined in:
lib/tivohmo/adapters/plex/category.rb

Instance Attribute Summary collapse

Attributes included from TivoHMO::API::Container

#presorted, #uuid

Attributes included from TivoHMO::API::Node

#app, #content_type, #created_at, #identifier, #modified_at, #parent, #root, #source_format, #title

Instance Method Summary collapse

Methods included from TivoHMO::API::Container

#refresh

Methods included from TivoHMO::API::Node

#add_child, #app?, #find, #root?, #title_path, #to_s, #tree_string

Constructor Details

#initialize(delegate, category_type, category_value = nil, presorted = false) ⇒ Category

Returns a new instance of Category.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tivohmo/adapters/plex/category.rb', line 20

def initialize(delegate, category_type, category_value=nil, presorted=false)
  # delegate is a Plex::Section
  @delegate = delegate

  super(delegate.key)
  self.presorted = presorted

  self.category_type = category_type
  self.category_value = category_value

  if category_value
    self.title = category_value[:title]
  else
    self.title = category_type.to_s.titleize
  end

  self.modified_at = Time.at(delegate.updated_at.to_i)
  self.created_at = Time.now
  @subtitles = config_get(:enable_subtitles)
end

Instance Attribute Details

#category_typeObject

Returns the value of attribute category_type.



15
16
17
# File 'lib/tivohmo/adapters/plex/category.rb', line 15

def category_type
  @category_type
end

#category_valueObject

Returns the value of attribute category_value.



15
16
17
# File 'lib/tivohmo/adapters/plex/category.rb', line 15

def category_value
  @category_value
end

#delegateObject (readonly)

Returns the value of attribute delegate.



14
15
16
# File 'lib/tivohmo/adapters/plex/category.rb', line 14

def delegate
  @delegate
end

Instance Method Details

#add_grouped(item_class, item_delegate) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tivohmo/adapters/plex/category.rb', line 89

def add_grouped(item_class, item_delegate)
  primary = item_class.new(item_delegate)

  if config_get(:enable_subtitles)
    subs = find_subtitles(item_delegate)

    if subs.size > 0
      group = Group.new(primary.identifier, primary.title)
      add_child(group)
      group.add_child(primary)
      subs.each {|s| group.add_child(item_class.new(item_delegate, s)) }
    else
      add_child(primary)
    end
  else
    add_child(primary)
  end
end

#child_countObject



43
44
45
# File 'lib/tivohmo/adapters/plex/category.rb', line 43

def child_count
  super_children.size
end

#childrenObject



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
78
79
80
81
82
83
84
85
86
87
# File 'lib/tivohmo/adapters/plex/category.rb', line 47

def children
  synchronize do

    # updated_at doesn't get updated for automatic updates, only
    # for updating from within plex media server web ui
    section_id = delegate.key.split('/').last
    new_delegate = delegate.library.section!(section_id)
    new_modified_at = new_delegate.updated_at.to_i
    if new_modified_at > modified_at.to_i
      logger.info "Plex section was updated, refreshing"
      @delegate = new_delegate
      self.modified_at = Time.at(new_modified_at)
      super.clear
    end

    if super.blank? || @subtitles != config_get(:enable_subtitles)
      super.clear
      @subtitles = config_get(:enable_subtitles)

      if category_value
        listing = delegate.send(category_type, category_value[:key])
      else
        listing = delegate.send(category_type)
      end

      Array(listing).each do |media|
        if media.is_a?(::Plex::Movie)
          add_grouped(Movie, media)
        elsif media.is_a?(::Plex::Episode)
          add_grouped(Episode, media)
        elsif media.is_a?(::Plex::Show)
          add_child(Show.new(media))
        else
          logger.error "Unknown type for #{media.class} in #{self.title}"
        end
      end
    end
  end

  super
end

#find_subtitles(item_delegate) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tivohmo/adapters/plex/category.rb', line 108

def find_subtitles(item_delegate)
  subs = []

  source_filename = CGI.unescape(item_delegate.medias.first.parts.first.file)

  # add in the file based subtitles
  subs.concat(SubtitlesUtil.instance.subtitles_for_media_file(source_filename))

  # add in the embedded subtitles
  item_delegate.medias.each do |media|
    media.parts.each do |part|
      prev_stream_count = 0
      part.streams.each do |stream|

        # stream.stream_type 3=subs, 1=video, 2=audio
        # stream.key.present? means file based srt
        # stream.index and no key, means embedded
        if stream.stream_type.to_i == 3
          if stream.respond_to?(:index) && stream.index.present?
            st = TivoHMO::API::Subtitle.new

            lang_code = stream.respond_to?(:language_code) && stream.language_code || "??"
            st.language_code = lang_code

            iso_entry = ISO_639.find_by_code(lang_code.downcase)
            if iso_entry
              st.language = iso_entry.english_name
            else
              logger.warn "Subtitle stream has unknown language code: #{lang_code}"
              st.language = "Unknown"
            end

            st.format = stream.codec
            st.type = :embedded
            # subtitle index should be the index amongst just the embedded subtitle streams
            st.location = stream.index.to_i - prev_stream_count
            subs << st
          end
        end

        prev_stream_count += 1
      end
    end
  end

  subs
end

#super_childrenObject



41
# File 'lib/tivohmo/adapters/plex/category.rb', line 41

alias_method :super_children, :children