Class: Mediaman::Document

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

Overview

Represents an on-disk folder or file representation of some kind of media.

Direct Known Subclasses

LibraryDocument, TemporaryDocument

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path.



18
19
20
# File 'lib/mediaman/document.rb', line 18

def path
  @path
end

Class Method Details

.from_path(path) ⇒ Object



12
13
14
15
16
# File 'lib/mediaman/document.rb', line 12

def self.from_path(path)
  f = self.new
  f.path = File.expand_path path
  f
end

Instance Method Details

#add_metadata_to_file!Object



35
36
37
38
39
40
41
42
43
# File 'lib/mediaman/document.rb', line 35

def 
  if supports_embedding_video_metadata?
    h = .to_subler_hash
    h[:artwork] = artwork_path if File.exists?(artwork_path)
    MiniSubler::Command.vendored. primary_video_file, h
  end
rescue
  puts "Exception while adding metadata to file."
end

#add_to_itunes!Object



68
69
70
71
# File 'lib/mediaman/document.rb', line 68

def add_to_itunes!
  cmd = "osascript -e \"tell application \\\"iTunes\\\" to add POSIX file \\\"#{self.path}\\\"\""
  `#{cmd}`
end

#artwork_pathObject



64
65
66
# File 'lib/mediaman/document.rb', line 64

def artwork_path
  extra_path "Artwork#{.canonical_image_url.present? ? File.extname(.canonical_image_url) : ".jpg"}"
end

#download_image!Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/mediaman/document.rb', line 53

def download_image!
  if !File.exists?(artwork_path) && .canonical_image_url.present?
    FileUtils.mkdir_p(File.dirname(artwork_path))
    open(.canonical_image_url) {|f|
       File.open(artwork_path, "wb") do |file|
         file.puts f.read
       end
    }
  end
end

#extObject



49
50
51
# File 'lib/mediaman/document.rb', line 49

def ext
  File.extname self.path
end

#extra_path(file) ⇒ Object



145
146
147
# File 'lib/mediaman/document.rb', line 145

def extra_path(file)
  File.join extras_path, file
end

#extra_paths(file) ⇒ Object



149
150
151
152
153
# File 'lib/mediaman/document.rb', line 149

def extra_paths(file)
  extras_paths.uniq.map do |x|
    File.join x, file
  end
end

#extras_pathObject



136
137
138
139
140
141
142
143
# File 'lib/mediaman/document.rb', line 136

def extras_path
  d = nil
  for path in extras_paths.uniq
    d = path if File.exists?(path)
  end
  d = extras_paths.last if d.nil?
  d
end

#extras_pathsObject



132
133
134
# File 'lib/mediaman/document.rb', line 132

def extras_paths
  [library_extras_path, standalone_extras_path]
end

#filenameObject



112
113
114
# File 'lib/mediaman/document.rb', line 112

def filename
  File.basename(self.path, '.*')
end

#filename_metadataObject



116
117
118
# File 'lib/mediaman/document.rb', line 116

def 
  @filename_metadata ||= MediaFilename.new(self.filename).to_hash.try(:stringify_keys)
end

#junk_filesObject



192
193
194
195
# File 'lib/mediaman/document.rb', line 192

def junk_files
  sort_junk! unless @junk_files
  @junk_files
end

#library_extras_pathObject



128
129
130
# File 'lib/mediaman/document.rb', line 128

def library_extras_path
  File.join(File.dirname(self.path), "Extras", File.basename(self.path, '.*'))
end

#local_metadataObject



84
85
86
87
88
89
90
91
92
# File 'lib/mediaman/document.rb', line 84

def 
  @local_metadata ||= begin
     = {}
    .merge!( || {})
    .merge!( || {})
    .reject!{|k, v| v.blank?}
    Metadata.new()
  end
end

#local_metadata_fetched?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/mediaman/document.rb', line 94

def 
  @local_metadata.present?
end

#metadataObject Also known as: extract_metadata!



73
74
75
76
77
78
79
80
81
# File 'lib/mediaman/document.rb', line 73

def 
  @metadata ||= begin
     = {}
    .merge!( || {})
    .merge!( || {})
    .reject!{|k, v| v.blank?}
    Metadata.new()
  end
end

#primary_video_fileObject



187
188
189
190
# File 'lib/mediaman/document.rb', line 187

def primary_video_file
  sort_junk! unless @video_files
  @primary_video_file ||= @video_files.sort{|a, b| File.size?(a) <=> File.size?(b) }.first
end

#rebase!(path) ⇒ Object



20
21
22
23
24
25
# File 'lib/mediaman/document.rb', line 20

def rebase!(path)
  self.path = path
  @primary_video_file = nil
  @junk_files = nil
  @video_files = nil
end

#remote_metadataObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mediaman/document.rb', line 98

def 
  @remote_metadata ||= begin
     if ['movie']
       Metadata.new({'movie_details' => Trakt::Movie.new(title: ['name'], year: ['year'], title_slug: ['title_slug'])})
     elsif ['tv']
       tv = Trakt::TVEpisode.new show_title: ['name'], season_number: ['season_number'], episode_number: ['episode_number'], title_slug: ['title_slug']
       Metadata.new({'episode_details' => tv.to_hash, 'show_details' => tv.show.to_hash})
     end
  rescue
    puts "TRAKT_API_KEY not set. Please set this environment variable to use remote metadata." unless ENV["TRAKT_API_KEY"]
    {}
  end
end

#save_and_apply_metadata!Object



27
28
29
30
31
32
33
# File 'lib/mediaman/document.rb', line 27

def save_and_apply_metadata!
  extract_metadata!
  save_sidecar!
  download_image!
  
  # Rewrap mkv to mp4.
end

#save_sidecar!(path = extra_path("Metadata.yml")) ⇒ Object



167
168
169
170
# File 'lib/mediaman/document.rb', line 167

def save_sidecar!(path = extra_path("Metadata.yml"))
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w') {|f| f.write(self..stringify_keys.to_yaml) }
end

#secondary_video_filesObject



177
178
179
180
181
182
183
184
185
# File 'lib/mediaman/document.rb', line 177

def secondary_video_files
  if video_files && primary_video_file
    v = video_files.dup
    v.delete primary_video_file
    v
  else
    []
  end
end

#sidecar_metadataObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mediaman/document.rb', line 155

def 
  @sidecar_metadata ||= begin
    y = {}
    extra_paths("Metadata.yml").each do |path|
      if File.exists?(path)
        y.merge! YAML::load(File.open(path))
      end
    end
    y.stringify_keys
  end
end

#standalone_extras_pathObject



124
125
126
# File 'lib/mediaman/document.rb', line 124

def standalone_extras_path
  File.join(File.dirname(self.path), File.basename(self.path, '.*') + " Extras")
end

#supports_embedding_video_metadata?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mediaman/document.rb', line 45

def supports_embedding_video_metadata?
  ext =~ /mp4/i || ext =~ /m4v/i || ext =~ /mkv/i || ext =~ /mov/i
end

#video_filesObject



172
173
174
175
# File 'lib/mediaman/document.rb', line 172

def video_files
  sort_junk! unless @video_files
  @video_files
end

#video_metadataObject



120
121
122
# File 'lib/mediaman/document.rb', line 120

def 
  @video_metadata ||= MiniSubler::Command.vendored.(self.video_files.first).try(:stringify_keys)
end