Class: MusicBox::Catalog::Release

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

Instance Attribute Summary collapse

Attributes inherited from Group::Item

#dir, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Group::Item

#fields, #info_file, #initialize, load, #save, #serialize

Constructor Details

This class inherits a constructor from MusicBox::Group::Item

Instance Attribute Details

#albumObject

linked on load



47
48
49
# File 'lib/musicbox/catalog/release.rb', line 47

def album
  @album
end

#artistsObject

Returns the value of attribute artists.



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

def artists
  @artists
end

#artists_sortObject

Returns the value of attribute artists_sort.



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

def artists_sort
  @artists_sort
end

#blocked_from_saleObject

Returns the value of attribute blocked_from_sale.



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

def blocked_from_sale
  @blocked_from_sale
end

#communityObject

Returns the value of attribute community.



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

def community
  @community
end

#companiesObject

Returns the value of attribute companies.



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

def companies
  @companies
end

#countryObject

Returns the value of attribute country.



12
13
14
# File 'lib/musicbox/catalog/release.rb', line 12

def country
  @country
end

#data_qualityObject

Returns the value of attribute data_quality.



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

def data_quality
  @data_quality
end

#date_addedObject

Returns the value of attribute date_added.



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

def date_added
  @date_added
end

#date_changedObject

Returns the value of attribute date_changed.



15
16
17
# File 'lib/musicbox/catalog/release.rb', line 15

def date_changed
  @date_changed
end

#estimated_weightObject

Returns the value of attribute estimated_weight.



16
17
18
# File 'lib/musicbox/catalog/release.rb', line 16

def estimated_weight
  @estimated_weight
end

#extraartistsObject

Returns the value of attribute extraartists.



17
18
19
# File 'lib/musicbox/catalog/release.rb', line 17

def extraartists
  @extraartists
end

#format_quantityObject

Returns the value of attribute format_quantity.



18
19
20
# File 'lib/musicbox/catalog/release.rb', line 18

def format_quantity
  @format_quantity
end

#formatsObject

Returns the value of attribute formats.



19
20
21
# File 'lib/musicbox/catalog/release.rb', line 19

def formats
  @formats
end

#genresObject

Returns the value of attribute genres.



20
21
22
# File 'lib/musicbox/catalog/release.rb', line 20

def genres
  @genres
end

#identifiersObject

Returns the value of attribute identifiers.



21
22
23
# File 'lib/musicbox/catalog/release.rb', line 21

def identifiers
  @identifiers
end

#imagesObject

Returns the value of attribute images.



22
23
24
# File 'lib/musicbox/catalog/release.rb', line 22

def images
  @images
end

#labelsObject

Returns the value of attribute labels.



23
24
25
# File 'lib/musicbox/catalog/release.rb', line 23

def labels
  @labels
end

#lowest_priceObject

Returns the value of attribute lowest_price.



24
25
26
# File 'lib/musicbox/catalog/release.rb', line 24

def lowest_price
  @lowest_price
end

#main_releaseObject

Returns the value of attribute main_release.



25
26
27
# File 'lib/musicbox/catalog/release.rb', line 25

def main_release
  @main_release
end

#main_release_urlObject

Returns the value of attribute main_release_url.



26
27
28
# File 'lib/musicbox/catalog/release.rb', line 26

def main_release_url
  @main_release_url
end

#masterObject

linked on load



46
47
48
# File 'lib/musicbox/catalog/release.rb', line 46

def master
  @master
end

#master_idObject

Returns the value of attribute master_id.



27
28
29
# File 'lib/musicbox/catalog/release.rb', line 27

def master_id
  @master_id
end

#master_urlObject

Returns the value of attribute master_url.



28
29
30
# File 'lib/musicbox/catalog/release.rb', line 28

def master_url
  @master_url
end

#most_recent_releaseObject

Returns the value of attribute most_recent_release.



29
30
31
# File 'lib/musicbox/catalog/release.rb', line 29

def most_recent_release
  @most_recent_release
end

#most_recent_release_urlObject

Returns the value of attribute most_recent_release_url.



30
31
32
# File 'lib/musicbox/catalog/release.rb', line 30

def most_recent_release_url
  @most_recent_release_url
end

#notesObject

Returns the value of attribute notes.



31
32
33
# File 'lib/musicbox/catalog/release.rb', line 31

def notes
  @notes
end

#num_for_saleObject

Returns the value of attribute num_for_sale.



32
33
34
# File 'lib/musicbox/catalog/release.rb', line 32

def num_for_sale
  @num_for_sale
end

#releasedObject

Returns the value of attribute released.



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

def released
  @released
end

#released_formattedObject

Returns the value of attribute released_formatted.



34
35
36
# File 'lib/musicbox/catalog/release.rb', line 34

def released_formatted
  @released_formatted
end

#resource_urlObject

Returns the value of attribute resource_url.



35
36
37
# File 'lib/musicbox/catalog/release.rb', line 35

def resource_url
  @resource_url
end

#seriesObject

Returns the value of attribute series.



36
37
38
# File 'lib/musicbox/catalog/release.rb', line 36

def series
  @series
end

#statusObject

Returns the value of attribute status.



37
38
39
# File 'lib/musicbox/catalog/release.rb', line 37

def status
  @status
end

#stylesObject

Returns the value of attribute styles.



38
39
40
# File 'lib/musicbox/catalog/release.rb', line 38

def styles
  @styles
end

#thumbObject

Returns the value of attribute thumb.



39
40
41
# File 'lib/musicbox/catalog/release.rb', line 39

def thumb
  @thumb
end

#titleObject

Returns the value of attribute title.



41
42
43
# File 'lib/musicbox/catalog/release.rb', line 41

def title
  @title
end

#tracklistObject

Returns the value of attribute tracklist.



40
41
42
# File 'lib/musicbox/catalog/release.rb', line 40

def tracklist
  @tracklist
end

#uriObject

Returns the value of attribute uri.



42
43
44
# File 'lib/musicbox/catalog/release.rb', line 42

def uri
  @uri
end

#versions_urlObject

Returns the value of attribute versions_url.



43
44
45
# File 'lib/musicbox/catalog/release.rb', line 43

def versions_url
  @versions_url
end

#videosObject

Returns the value of attribute videos.



44
45
46
# File 'lib/musicbox/catalog/release.rb', line 44

def videos
  @videos
end

#yearObject

Returns the value of attribute year.



45
46
47
# File 'lib/musicbox/catalog/release.rb', line 45

def year
  @year
end

Class Method Details

.csv_headerObject



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

def self.csv_header
  %w[ID year artist title].to_csv
end

Instance Method Details

#<=>(other) ⇒ Object



101
102
103
# File 'lib/musicbox/catalog/release.rb', line 101

def <=>(other)
  sort_tuple <=> other.sort_tuple
end

#artistObject



93
94
95
# File 'lib/musicbox/catalog/release.rb', line 93

def artist
  ReleaseArtist.artists_to_s(@artists)
end

#artist_keyObject



97
98
99
# File 'lib/musicbox/catalog/release.rb', line 97

def artist_key
  @artists.first.key
end

#cd?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/musicbox/catalog/release.rb', line 89

def cd?
  @formats.find(&:cd?) != nil
end

#details_to_sObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/musicbox/catalog/release.rb', line 130

def details_to_s
  info = [
    ['ID', @id],
    ['Master ID', @master_id || '-'],
    ['Artist', ReleaseArtist.artists_to_s(@artists)],
    ['Title', @title],
    ['Formats', Format.to_s(@formats)],
    ['Released', release_year || '-'],
    ['Originally released', original_release_year || '-'],
    ['Discogs URI', @uri || '-'],
    ['Album dir', @album&.dir || '-'],
    ['Tracks', nil, tracklist_to_info],
  ]
  MusicBox.info_to_s(info)
end

#download_image(uri:, file:) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/musicbox/catalog/release.rb', line 205

def download_image(uri:, file:)
  if uri && file
    unless file.exist?
      puts "#{@id}: downloading #{uri}"
      file.dirname.mkpath unless file.dirname.exist?
      file.write(HTTP.get(uri))
      sleep(1)
    end
  end
end

#download_imagesObject



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/musicbox/catalog/release.rb', line 193

def download_images
  image = @images.find { |image| image['type'] == 'primary' }
  if image
    download_image(uri: image['uri'], file: image['file'])
  else
    @images.each do |image|
      download_image(uri: image['uri'], file: image['file'])
    end
  end
  @master.download_images if @master
end


184
185
186
187
188
189
190
191
# File 'lib/musicbox/catalog/release.rb', line 184

def link_images(images_dir)
  if @images
    @images.each do |image|
      uri = URI.parse(image['uri'])
      image['file'] = images_dir / Path.new(uri.path).basename
    end
  end
end

#original_release_yearObject



85
86
87
# File 'lib/musicbox/catalog/release.rb', line 85

def original_release_year
  @master&.release_year || release_year
end

#release_yearObject



77
78
79
80
81
82
83
# File 'lib/musicbox/catalog/release.rb', line 77

def release_year
  if @year && @year != 0
    @year
  elsif @released
    @released.to_s.split('-').first&.to_i
  end
end

#select_coverObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/musicbox/catalog/release.rb', line 216

def select_cover
  if @album.has_cover?
    puts "#{@id}: cover already exists"
    return
  end
  download_images
  @album.extract_cover
  choices = [
    @master&.images&.map { |i| i['file'] },
    @images&.map { |i| i['file'] },
    @album.cover_file,
  ].flatten.compact.uniq.select(&:exist?)
  if choices.empty?
    puts "#{@id}: no covers exist"
  else
    choices.each { |f| run_command('open', f) }
    choice = TTY::Prompt.new.select('Cover?', choices)
    cover_file = (album.dir / 'cover').add_extension(choice.extname)
    choice.cp(cover_file)
    @album.update_tags
  end
end

#sort_tupleObject



105
106
107
# File 'lib/musicbox/catalog/release.rb', line 105

def sort_tuple
  [artist_key, original_release_year || 0, @title]
end

#summary_to_sObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/musicbox/catalog/release.rb', line 117

def summary_to_s
  '%-8s | %1s%1s | %-4s %4s | %-50.50s | %-60.60s | %-6s' % [
    @id,
    @album ? 'A' : '',
    @album&.has_cover? ? 'C' : '',
    artist_key,
    original_release_year || '-',
    artist,
    @title,
    Format.to_s(@formats),
  ]
end

#to_csvObject



109
110
111
# File 'lib/musicbox/catalog/release.rb', line 109

def to_csv
  [@id, original_release_year, artist, @title].to_csv
end

#to_labelObject



173
174
175
176
177
178
179
180
181
182
# File 'lib/musicbox/catalog/release.rb', line 173

def to_label
  {
    artist: artist,
    title: title,
    key: artist_key,
    year: original_release_year,
    format: Format.to_s(@formats),
    id: id,
  }
end

#to_sObject



113
114
115
# File 'lib/musicbox/catalog/release.rb', line 113

def to_s
  summary_to_s
end

#tracklist_flattened(tracklist = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/musicbox/catalog/release.rb', line 146

def tracklist_flattened(tracklist=nil)
  tracklist ||= @tracklist
  tracks = []
  tracklist.each do |track|
    tracks << track
    tracks += tracklist_flattened(track.sub_tracks) if track.type == 'index'
  end
  tracks
end

#tracklist_to_info(tracklist = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/musicbox/catalog/release.rb', line 156

def tracklist_to_info(tracklist=nil)
  tracklist ||= @tracklist
  max_position_length = tracklist.select(&:position).map { |t| t.position.to_s.length }.max
  tracklist.map do |track|
    [
      track.type,
      [
        !track.position.to_s.empty? ? ('%*s:' % [max_position_length, track.position]) : nil,
        track.title || '-',
        track.artists ? "(#{ReleaseArtist.artists_to_s(track.artists)})" : nil,
        !track.duration.to_s.empty? ? "[#{track.duration}]" : nil,
      ].compact.join(' '),
      track.sub_tracks ? tracklist_to_info(track.sub_tracks) : nil,
    ]
  end
end