Class: MusicBox::Catalog::Album

Inherits:
Group::Item show all
Defined in:
lib/musicbox/catalog/album.rb

Instance Attribute Summary collapse

Attributes inherited from Group::Item

#dir, #id

Instance Method Summary collapse

Methods inherited from Group::Item

#<=>, #fields, #info_file, load, #save

Constructor Details

#initialize(params = {}) ⇒ Album

Returns a new instance of Album.



13
14
15
16
# File 'lib/musicbox/catalog/album.rb', line 13

def initialize(params={})
  @tracks = []
  super
end

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



8
9
10
# File 'lib/musicbox/catalog/album.rb', line 8

def artist
  @artist
end

#discsObject

Returns the value of attribute discs.



10
11
12
# File 'lib/musicbox/catalog/album.rb', line 10

def discs
  @discs
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/musicbox/catalog/album.rb', line 7

def title
  @title
end

#tracksObject

Returns the value of attribute tracks.



11
12
13
# File 'lib/musicbox/catalog/album.rb', line 11

def tracks
  @tracks
end

#yearObject

Returns the value of attribute year.



9
10
11
# File 'lib/musicbox/catalog/album.rb', line 9

def year
  @year
end

Instance Method Details

#cover_fileObject

Raises:



43
44
45
46
47
# File 'lib/musicbox/catalog/album.rb', line 43

def cover_file
  files = @dir.glob('cover.{jpg,png}')
  raise Error, "Multiple cover files" if files.length > 1
  files.first
end

#date=(date) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/musicbox/catalog/album.rb', line 22

def date=(date)
  @year = case date
  when Date
    date.year
  when String
    date.to_i
  else
    date
  end
end

#extract_coverObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/musicbox/catalog/album.rb', line 136

def extract_cover
  if has_cover?
    puts "#{@id}: already has cover"
    return
  end
  file = @dir / @tracks.first.file
  begin
    run_command('mp4art',
      '--extract',
      '--art-index', 0,
      '--overwrite',
      '--quiet',
      file)
  rescue RunCommandFailed => e
    # ignore
  end
  # cover is in FILE.art[0].TYPE
  files = @dir.glob('*.art*.*').reject { |f| f.extname.downcase == '.gif' }
  if files.length == 0
    puts "#{@id}: no cover to extract"
  elsif files.length > 1
    raise Error, "Multiple covers found"
  else
    file = files.first
    new_cover_file = (@dir / 'cover').add_extension(file.extname)
    puts "#{@id}: extracted cover: #{new_cover_file.basename}"
    file.rename(new_cover_file)
  end
end

#has_cover?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/musicbox/catalog/album.rb', line 49

def has_cover?
  !cover_file.nil?
end

#log_files=Object



37
# File 'lib/musicbox/catalog/album.rb', line 37

def log_files=(*); end

#release_id=(id) ⇒ Object



33
34
35
# File 'lib/musicbox/catalog/album.rb', line 33

def release_id=(id)
  @id = id
end

#serializeObject



166
167
168
169
170
171
172
173
# File 'lib/musicbox/catalog/album.rb', line 166

def serialize
  super(
    title: @title,
    artist: @artist,
    year: @year,
    discs: @discs,
    tracks: @tracks.map(&:to_h))
end

#show_cover(width: nil, height: nil, preserve_aspect_ratio: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/musicbox/catalog/album.rb', line 53

def show_cover(width: nil, height: nil, preserve_aspect_ratio: nil)
  # see https://iterm2.com/documentation-images.html
  file = cover_file
  if file && file.exist?
    data = Base64.strict_encode64(file.read)
    args = {
      name: Base64.strict_encode64(file.to_s),
      size: data.length,
      width: width,
      height: height,
      preserveAspectRatio: preserve_aspect_ratio,
      inline: 1,
    }.compact
    puts "\033]1337;File=%s:%s\a" % [
      args.map { |a| a.join('=') }.join(';'),
      data,
    ]
  end
end

#update_tags(force: false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
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
# File 'lib/musicbox/catalog/album.rb', line 95

def update_tags(force: false)
  changes = []
  @tracks.each do |track|
    track.update_tags
    changes << track if track.tags.changed?
  end
  unless changes.empty?
    puts
    puts "#{@title} [#{@dir}]"
    changes.each do |track|
      puts "\t" + track.file.to_s
      track.tags.changes.each do |change|
        puts "\t\t" + change.inspect
      end
    end
    if force || TTY::Prompt.new.yes?('Update track files?')
      changes.each do |track|
        track.save_tags
      end
    end
  end
  if has_cover?
    # --replace apparently doesn't work, so must do --remove, then --add
    @tracks.each do |track|
      begin
        run_command('mp4art',
          '--quiet',
          '--remove',
          track.path)
      rescue RunCommandFailed => e
        # ignore
      end
      run_command('mp4art',
        '--quiet',
        '--add',
        cover_file,
        track.path)
    end
  end
end

#validate_logsObject

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/musicbox/catalog/album.rb', line 73

def validate_logs
  log_files = @dir.glob('*.log')
  raise Error, "No rip logs" if log_files.empty?
  state = :initial
  log_files.each do |log_file|
    log_file.readlines.map(&:chomp).each do |line|
      case state
      when :initial
        if line =~ /^AccurateRip Summary/
          state = :accuraterip_summary
        end
      when :accuraterip_summary
        if line =~ /^\s+Track \d+ : (\S+)/
          raise Error, "Not accurately ripped" unless $1 == 'OK'
        else
          break
        end
      end
    end
  end
end