Class: Mediaman::Document
- Inherits:
-
Object
- Object
- Mediaman::Document
show all
- Defined in:
- lib/mediaman/document.rb
Overview
Represents an on-disk folder or file representation of some kind of media.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#path ⇒ Object
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
35
36
37
38
39
40
41
42
43
|
# File 'lib/mediaman/document.rb', line 35
def add_metadata_to_file!
if supports_embedding_video_metadata?
h = metadata.to_subler_hash
h[:artwork] = artwork_path if File.exists?(artwork_path)
MiniSubler::Command.vendored.set_metadata 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_path ⇒ Object
64
65
66
|
# File 'lib/mediaman/document.rb', line 64
def artwork_path
"Artwork#{metadata.canonical_image_url.present? ? File.extname(metadata.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) && metadata.canonical_image_url.present?
FileUtils.mkdir_p(File.dirname(artwork_path))
open(metadata.canonical_image_url) {|f|
File.open(artwork_path, "wb") do |file|
file.puts f.read
end
}
end
end
|
#ext ⇒ Object
49
50
51
|
# File 'lib/mediaman/document.rb', line 49
def ext
File.extname self.path
end
|
145
146
147
|
# File 'lib/mediaman/document.rb', line 145
def (file)
File.join , file
end
|
149
150
151
152
153
|
# File 'lib/mediaman/document.rb', line 149
def (file)
.uniq.map do |x|
File.join x, file
end
end
|
136
137
138
139
140
141
142
143
|
# File 'lib/mediaman/document.rb', line 136
def
d = nil
for path in .uniq
d = path if File.exists?(path)
end
d = .last if d.nil?
d
end
|
132
133
134
|
# File 'lib/mediaman/document.rb', line 132
def
[, standalone_extras_path]
end
|
#filename ⇒ Object
112
113
114
|
# File 'lib/mediaman/document.rb', line 112
def filename
File.basename(self.path, '.*')
end
|
116
117
118
|
# File 'lib/mediaman/document.rb', line 116
def filename_metadata
@filename_metadata ||= MediaFilename.new(self.filename).to_hash.try(:stringify_keys)
end
|
#junk_files ⇒ Object
192
193
194
195
|
# File 'lib/mediaman/document.rb', line 192
def junk_files
sort_junk! unless @junk_files
@junk_files
end
|
128
129
130
|
# File 'lib/mediaman/document.rb', line 128
def
File.join(File.dirname(self.path), "Extras", File.basename(self.path, '.*'))
end
|
84
85
86
87
88
89
90
91
92
|
# File 'lib/mediaman/document.rb', line 84
def local_metadata
@local_metadata ||= begin
metadata = {}
metadata.merge!(sidecar_metadata || {})
metadata.merge!(filename_metadata || {})
metadata.reject!{|k, v| v.blank?}
Metadata.new(metadata)
end
end
|
94
95
96
|
# File 'lib/mediaman/document.rb', line 94
def local_metadata_fetched?
@local_metadata.present?
end
|
73
74
75
76
77
78
79
80
81
|
# File 'lib/mediaman/document.rb', line 73
def metadata
@metadata ||= begin
metadata = {}
metadata.merge!(local_metadata || {})
metadata.merge!(remote_metadata || {})
metadata.reject!{|k, v| v.blank?}
Metadata.new(metadata)
end
end
|
#primary_video_file ⇒ Object
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
|
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/mediaman/document.rb', line 98
def remote_metadata
@remote_metadata ||= begin
if local_metadata['movie']
Metadata.new({'movie_details' => Trakt::Movie.new(title: local_metadata['name'], year: local_metadata['year'], title_slug: local_metadata['title_slug'])})
elsif local_metadata['tv']
tv = Trakt::TVEpisode.new show_title: local_metadata['name'], season_number: local_metadata['season_number'], episode_number: local_metadata['episode_number'], title_slug: local_metadata['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
|
27
28
29
30
31
32
33
|
# File 'lib/mediaman/document.rb', line 27
def save_and_apply_metadata!
save_sidecar!
download_image!
add_metadata_to_file!
end
|
#save_sidecar!(path = extra_path("Metadata.yml")) ⇒ Object
167
168
169
170
|
# File 'lib/mediaman/document.rb', line 167
def save_sidecar!(path = ("Metadata.yml"))
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') {|f| f.write(self.metadata.stringify_keys.to_yaml) }
end
|
#secondary_video_files ⇒ Object
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
|
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/mediaman/document.rb', line 155
def sidecar_metadata
@sidecar_metadata ||= begin
y = {}
("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_path ⇒ Object
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
|
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_files ⇒ Object
172
173
174
175
|
# File 'lib/mediaman/document.rb', line 172
def video_files
sort_junk! unless @video_files
@video_files
end
|
120
121
122
|
# File 'lib/mediaman/document.rb', line 120
def video_metadata
@video_metadata ||= MiniSubler::Command.vendored.get_metadata(self.video_files.first).try(:stringify_keys)
end
|