Class: JimmyJukebox::SongLoader
- Defined in:
- lib/jimmy_jukebox/song_loader.rb
Instance Attribute Summary collapse
-
#user_config ⇒ Object
readonly
Returns the value of attribute user_config.
Instance Method Summary collapse
-
#all_downloadable_songs(genre = nil) ⇒ Object
valid genres: ‘JAZZ’, ‘CLASSICAL’, ‘BLUEGRASS’, ‘BANJO’, ‘ROCK’.
- #all_subdir_music_files(dir) ⇒ Object
- #all_subdir_music_files_extensionless(dir) ⇒ Object
- #create_save_dir(save_dir) ⇒ Object
- #define_artist(name) ⇒ Object
- #download_sample_genre(num_songs = 1, genre = nil) ⇒ Object
- #downloadable(song_urls, current_songs) ⇒ Object
-
#initialize(user_config = nil) ⇒ SongLoader
constructor
A new instance of SongLoader.
- #sample_classical(num_songs) ⇒ Object
- #sample_genre(num_songs, genre = nil) ⇒ Object
- #songs_to_filenames(songs) ⇒ Object
- #strip_music_format_extension(song_filename) ⇒ Object
- #valid_music_format_extension?(song_filename) ⇒ Boolean
- #version_of_song_in_dir_or_subdir?(song_filename, save_dir) ⇒ Boolean
- #version_of_song_under_specific_dir?(song_filename, save_dir) ⇒ Boolean
Constructor Details
#initialize(user_config = nil) ⇒ SongLoader
Returns a new instance of SongLoader.
44 45 46 47 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 44 def initialize(user_config = nil) @user_config = user_config || UserConfig.new ARTISTS.values.each { |artist| define_artist artist[:name].to_sym } end |
Instance Attribute Details
#user_config ⇒ Object (readonly)
Returns the value of attribute user_config.
42 43 44 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 42 def user_config @user_config end |
Instance Method Details
#all_downloadable_songs(genre = nil) ⇒ Object
valid genres: ‘JAZZ’, ‘CLASSICAL’, ‘BLUEGRASS’, ‘BANJO’, ‘ROCK’
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 59 def all_downloadable_songs(genre = nil) # valid genres: 'JAZZ', 'CLASSICAL', 'BLUEGRASS', 'BANJO', 'ROCK' result = {} ARTISTS.values.each do |artist| next if genre && artist[:genre] != genre fn = File.dirname(__FILE__) + "/songs/#{artist_name_to_yaml_file(artist[:name].to_s)}" if File.exists?(fn) YAML::load_file(fn).each do |song| result[song] = artist end end end result end |
#all_subdir_music_files(dir) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 125 def all_subdir_music_files(dir) existing_files = Dir.glob(File.join(dir, '**', '*' )) # all files in all subdirs if "".respond_to?(:force_encoding) # Ruby 1.8 doesn't have string encoding or String#force_encoding existing_files.delete_if { |f| !f.force_encoding("UTF-8").valid_encoding? } # avoid "invalid byte sequence in UTF-8 (ArgumentError)" end existing_files.delete_if { |f| !valid_music_format_extension?(f) } # delete unless valid format existing_files.map { |f| File.basename(f) } # strip any path info preceding the filename end |
#all_subdir_music_files_extensionless(dir) ⇒ Object
134 135 136 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 134 def all_subdir_music_files_extensionless(dir) all_subdir_music_files(dir).map! { |f| strip_music_format_extension(f) } # strip extensions end |
#create_save_dir(save_dir) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 138 def create_save_dir(save_dir) return if File.directory?(save_dir) begin FileUtils.mkdir_p(save_dir) rescue SystemCallError puts "WARNING: Unable to create #{save_dir}" raise end end |
#define_artist(name) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 49 def define_artist(name) .instance_eval do define_method(name) do |max_num = nil| save_dir = user_config.root_music_dir + artist_name_to_subdir_name(name.to_s) songs = YAML::load_file(File.dirname(__FILE__) + "/songs/#{artist_name_to_yaml_file(name.to_s)}") download_num_songs(songs, save_dir, max_num) end end end |
#download_sample_genre(num_songs = 1, genre = nil) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 86 def download_sample_genre(num_songs = 1, genre = nil) sample = sample_genre(num_songs, genre) sample.each do |song_url, artist| save_dir = user_config.root_music_dir + artist_name_to_subdir_name(artist[:name].to_s) download_song(song_url, save_dir) end end |
#downloadable(song_urls, current_songs) ⇒ Object
152 153 154 155 156 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 152 def downloadable(song_urls, current_songs) # song_urls are URLs # current_songs are filenames song_urls.delete_if { |url| current_songs.include?(File.basename(url)) } end |
#sample_classical(num_songs) ⇒ Object
94 95 96 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 94 def sample_classical(num_songs) raise "not yet implemented" end |
#sample_genre(num_songs, genre = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 73 def sample_genre(num_songs, genre = nil) # loop through array and download num_songs new songs (or until end of array reached) sample = {} available_songs = all_downloadable_songs(genre) num_songs.times do unless available_songs.length == 0 k, v = available_songs.rand_pair! sample[k] = v end end sample end |
#songs_to_filenames(songs) ⇒ Object
148 149 150 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 148 def songs_to_filenames(songs) songs.map { |song| File.basename(song.music_file) } end |
#strip_music_format_extension(song_filename) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 104 def strip_music_format_extension(song_filename) fn = song_filename.dup JimmyJukebox::AUDIO_FORMATS.keys.each do |k| fn.gsub!(k,"") if fn =~ k end fn end |
#valid_music_format_extension?(song_filename) ⇒ Boolean
98 99 100 101 102 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 98 def valid_music_format_extension?(song_filename) JimmyJukebox::AUDIO_FORMATS.keys.any? { |k| song_filename =~ k } end |
#version_of_song_in_dir_or_subdir?(song_filename, save_dir) ⇒ Boolean
112 113 114 115 116 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 112 def version_of_song_in_dir_or_subdir?(song_filename, save_dir) extensionless_song_filename = strip_music_format_extension(song_filename) existing_files = all_subdir_music_files_extensionless(save_dir) existing_files.include?(extensionless_song_filename) # does extensionless song_filename exist in directory? end |
#version_of_song_under_specific_dir?(song_filename, save_dir) ⇒ Boolean
118 119 120 121 122 123 |
# File 'lib/jimmy_jukebox/song_loader.rb', line 118 def version_of_song_under_specific_dir?(song_filename, save_dir) extensionless_song_filename = strip_music_format_extension(song_filename) existing_files = Dir.entries(".").delete_if { |f| !valid_music_format_extension?(f) } # delete unless valid format existing_files.map! { |f| strip_music_format_extension(f) } # strip extensions existing_files.include?(extensionless_song_filename) # does extensionless song_filename exist in directory? end |