Class: MusicBox::Catalog
- Inherits:
-
Object
- Object
- MusicBox::Catalog
- Defined in:
- lib/musicbox/catalog.rb,
lib/musicbox/catalog/tags.rb,
lib/musicbox/catalog/album.rb,
lib/musicbox/catalog/track.rb,
lib/musicbox/catalog/albums.rb,
lib/musicbox/catalog/artist.rb,
lib/musicbox/catalog/format.rb,
lib/musicbox/catalog/artists.rb,
lib/musicbox/catalog/release.rb,
lib/musicbox/catalog/releases.rb,
lib/musicbox/catalog/collection.rb,
lib/musicbox/catalog/album_track.rb,
lib/musicbox/catalog/release_artist.rb,
lib/musicbox/catalog/collection_item.rb
Defined Under Namespace
Classes: Album, AlbumTrack, Albums, Artist, Artists, Collection, CollectionItem, Format, Release, ReleaseArtist, Releases, Tags, Track
Instance Attribute Summary collapse
-
#albums ⇒ Object
Returns the value of attribute albums.
-
#artists ⇒ Object
Returns the value of attribute artists.
-
#catalog_dir ⇒ Object
Returns the value of attribute catalog_dir.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#config ⇒ Object
Returns the value of attribute config.
-
#extract_dir ⇒ Object
Returns the value of attribute extract_dir.
-
#extract_done_dir ⇒ Object
Returns the value of attribute extract_done_dir.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#images_dir ⇒ Object
Returns the value of attribute images_dir.
-
#import_dir ⇒ Object
Returns the value of attribute import_dir.
-
#import_done_dir ⇒ Object
Returns the value of attribute import_done_dir.
-
#masters ⇒ Object
Returns the value of attribute masters.
-
#releases ⇒ Object
Returns the value of attribute releases.
-
#root_dir ⇒ Object
Returns the value of attribute root_dir.
Instance Method Summary collapse
- #artist_keys(artists) ⇒ Object
- #categorize_files(dir) ⇒ Object
- #dirs_for_args(base_dir, args) ⇒ Object
- #find(*selectors, group: nil, prompt: false, multiple: true) ⇒ Object
- #find_dups(releases) ⇒ Object
-
#initialize(root: nil) ⇒ Catalog
constructor
A new instance of Catalog.
- #link_groups ⇒ Object
- #load_config ⇒ Object
- #orphaned ⇒ Object
- #orphaned_images ⇒ Object
Constructor Details
#initialize(root: nil) ⇒ Catalog
Returns a new instance of Catalog.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/musicbox/catalog.rb', line 20 def initialize(root: nil) @root_dir = Path.new(root || ENV['MUSICBOX_ROOT'] || '~/Music/MusicBox'). raise Error, "#{@root_dir} doesn't exist" unless @root_dir.exist? load_config @import_dir = @root_dir / 'import' @import_done_dir = @root_dir / 'import-done' @extract_dir = @root_dir / 'extract' @extract_done_dir = @root_dir / 'extract-done' @catalog_dir = @root_dir / 'catalog' @collection = Collection.new(root: @catalog_dir / 'collection') @releases = Releases.new(root: @catalog_dir / 'releases') @masters = Releases.new(root: @catalog_dir / 'masters') @artists = Artists.new(root: @catalog_dir / 'artists') @albums = Albums.new(root: @catalog_dir / 'albums') @images_dir = @catalog_dir / 'images' link_groups @prompt = TTY::Prompt.new end |
Instance Attribute Details
#albums ⇒ Object
Returns the value of attribute albums.
17 18 19 |
# File 'lib/musicbox/catalog.rb', line 17 def albums @albums end |
#artists ⇒ Object
Returns the value of attribute artists.
16 17 18 |
# File 'lib/musicbox/catalog.rb', line 16 def artists @artists end |
#catalog_dir ⇒ Object
Returns the value of attribute catalog_dir.
10 11 12 |
# File 'lib/musicbox/catalog.rb', line 10 def catalog_dir @catalog_dir end |
#collection ⇒ Object
Returns the value of attribute collection.
13 14 15 |
# File 'lib/musicbox/catalog.rb', line 13 def collection @collection end |
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/musicbox/catalog.rb', line 12 def config @config end |
#extract_dir ⇒ Object
Returns the value of attribute extract_dir.
8 9 10 |
# File 'lib/musicbox/catalog.rb', line 8 def extract_dir @extract_dir end |
#extract_done_dir ⇒ Object
Returns the value of attribute extract_done_dir.
9 10 11 |
# File 'lib/musicbox/catalog.rb', line 9 def extract_done_dir @extract_done_dir end |
#groups ⇒ Object
Returns the value of attribute groups.
18 19 20 |
# File 'lib/musicbox/catalog.rb', line 18 def groups @groups end |
#images_dir ⇒ Object
Returns the value of attribute images_dir.
11 12 13 |
# File 'lib/musicbox/catalog.rb', line 11 def images_dir @images_dir end |
#import_dir ⇒ Object
Returns the value of attribute import_dir.
6 7 8 |
# File 'lib/musicbox/catalog.rb', line 6 def import_dir @import_dir end |
#import_done_dir ⇒ Object
Returns the value of attribute import_done_dir.
7 8 9 |
# File 'lib/musicbox/catalog.rb', line 7 def import_done_dir @import_done_dir end |
#masters ⇒ Object
Returns the value of attribute masters.
15 16 17 |
# File 'lib/musicbox/catalog.rb', line 15 def masters @masters end |
#releases ⇒ Object
Returns the value of attribute releases.
14 15 16 |
# File 'lib/musicbox/catalog.rb', line 14 def releases @releases end |
#root_dir ⇒ Object
Returns the value of attribute root_dir.
5 6 7 |
# File 'lib/musicbox/catalog.rb', line 5 def root_dir @root_dir end |
Instance Method Details
#artist_keys(artists) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/musicbox/catalog.rb', line 68 def artist_keys(artists) keys = {} names = {} non_personal_names = Set.new artists.map { |a| a.kind_of?(ReleaseArtist) ? a : ReleaseArtist.new(name: a) }.each do |artist| non_personal_names << artist.name if artist.name == artist.canonical_name key = artist.key (keys[key] ||= Set.new) << artist.name (names[artist.name] ||= Set.new) << key end { non_personal_names: non_personal_names.sort, keys: keys.sort.map { |k, s| [k, s.to_a] }.to_h, names: names.sort.map { |k, s| [k, s.to_a] }.to_h, } end |
#categorize_files(dir) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/musicbox/catalog.rb', line 179 def categorize_files(dir) categories = {} dir.children.sort.each do |path| type = case (ext = path.extname.downcase) when '.m4a', '.m4p', '.mp3' :audio else ext.delete_prefix('.').to_sym end categories[type] ||= [] categories[type] << path end categories end |
#dirs_for_args(base_dir, args) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/musicbox/catalog.rb', line 194 def dirs_for_args(base_dir, args) if args.empty? dirs = base_dir.children.select(&:dir?) else dirs = args.map { |p| Path.new(p) } end dirs.sort_by { |d| d.to_s.downcase } end |
#find(*selectors, group: nil, prompt: false, multiple: true) ⇒ Object
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 135 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 |
# File 'lib/musicbox/catalog.rb', line 98 def find(*selectors, group: nil, prompt: false, multiple: true) unless group.kind_of?(Group) group = case group&.to_sym when :releases, nil @releases when :masters @masters when :albums @albums else raise Error, "Unknown group: #{group.inspect}" end end ;;puts "searching #{group.items.count} items in #{group.class}" selectors = [selectors].compact.flatten selectors = [':all'] if selectors.empty? selected = [] selectors.each do |selector| case selector.to_s when ':all' selected += group.items when ':recent' selected += group.items.select { |c| (Date.today - c.date_added) < 7 } when ':recently-added' selected += @collection.items.select { |c| (Date.today - c.date_added) < 30 }.map(&:release) when ':multiformat' selected += group.items.select { |r| r.formats&.length > 1 } when ':cd' selected += group.items.select(&:cd?) when ':unripped' selected += group.items.select(&:cd?).reject(&:album) when ':no-cover' selected += group.items.select(&:album).reject { |r| r.album.has_cover? } when ':odd-positions' selected += group.items.select(&:cd?).select { |r| r.tracklist_flattened.find { |t| t.position !~ /^\d+$/ } } when /^-?\d+$/ n = selector.to_i item = group[n.abs] or raise Error, "Can't find item #{selector.inspect} in #{group.class}" if n > 0 selected += [group[n]] else selected -= [group[-n]] end else selected += group.search(query: selector.to_s, fields: [:title, :artists, :id]) end end selected.uniq.sort! if prompt choices = selected.map { |r| [r.to_s, r.id] }.to_h if multiple ids = @prompt.multi_select('Item?', filter: true, per_page: 100, quiet: true) do || # menu.default *(1..choices.length).to_a choices.each do |name, value| .choice name, value end end selected = ids.map { |id| group[id] } else id = @prompt.select('Item?', choices, filter: true, per_page: 100, quiet: true) selected = [group[id]] if id end end selected end |
#find_dups(releases) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/musicbox/catalog.rb', line 85 def find_dups(releases) dups = {} releases.select(&:master_id).each do |release| dups[release.master_id] ||= {} #FIXME: wrong release.formats.map(&:name).each do |format_name| dups[release.master_id][format_name] ||= [] dups[release.master_id][format_name] << release end end dups end |
#link_groups ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/musicbox/catalog.rb', line 164 def link_groups @releases.items.each do |release| release.master = @masters[release.master_id] if release.master_id release.artists.each do |release_artist| release_artist.artist = @artists[release_artist.id] end release.album = @albums[release.id] release.link_images(@images_dir) release.master&.link_images(@images_dir) end @collection.items.each do |item| item.release = @releases[item.id] end end |
#load_config ⇒ Object
39 40 41 42 43 |
# File 'lib/musicbox/catalog.rb', line 39 def load_config @config = YAML.load((@root_dir / 'config.yaml').read) ReleaseArtist.class_variable_set(:@@personal_names, @config['personal_names']) ReleaseArtist.class_variable_set(:@@canonical_names, @config['canonical_names']) end |
#orphaned ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/musicbox/catalog.rb', line 45 def orphaned orphaned = %i[releases masters artists albums].map { |k| [k, send(k).items.dup] }.to_h @collection.items.each do |item| release = item.release or raise orphaned[:releases].delete(release) orphaned[:masters].delete(release.master) if release.master release.artists.each do |release_artist| orphaned[:artists].delete(release_artist.artist) end orphaned[:albums].delete(release.album) if release.album end orphaned end |
#orphaned_images ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/musicbox/catalog.rb', line 59 def orphaned_images all_files = [@releases, @masters].map do |group| group.items.select(&:images).map do |release| release.images.map { |image| image['file'].basename.to_s } end end.flatten.compact @images_dir.children.map(&:basename).map(&:to_s) - all_files end |