Class: Play::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/play/library.rb

Class Method Summary collapse

Class Method Details

.fs_get_artist_and_title_and_album(path) ⇒ Object

Splits a music file up into three constituent parts: artist, title, album.

path - the String path to the music file on-disk

Returns an Array with three String elements: the artist, the song title, and the album.



65
66
67
68
69
70
71
72
# File 'lib/play/library.rb', line 65

def self.fs_get_artist_and_title_and_album(path)
  AudioInfo.open(path) do |info|
    return info.artist.try(:strip),
           info.title.try(:strip),
           info.album.try(:strip)
  end
rescue AudioInfoError
end

.fs_songs(path) ⇒ Object

Search a directory and return all of the files in it, recursively.

Returns an Array of String file paths.



19
20
21
# File 'lib/play/library.rb', line 19

def self.fs_songs(path)
  `find "#{path}" -type f ! -name '.*'`.split("\n")
end

.import_song(path) ⇒ Object

Imports a song into the database. This will identify a file’s artist and albums, run through the associations, and so on. It should be idempotent, so you should be able to run it repeatedly on the same set of files and not screw anything up.

path - the String path to the music file on-disk

Returns the imported (or found) Song.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/play/library.rb', line 43

def self.import_song(path)
  artist_name,title,album_name = fs_get_artist_and_title_and_album(path)
  artist = Artist.find_or_create_by_name(artist_name)
  song = Song.where(:path => path).first

  if !song
    album = Album.where(:artist_id => artist.id, :name => album_name).first ||
            Album.create(:artist_id => artist.id, :name => album_name)
    Song.create(:path => path,
                :artist => artist,
                :album => album,
                :title => title)
  end
end

.import_songs(path = Play.path) ⇒ Object

Imports an array of songs into the database.

path = the String path of the directory to search in. Will default to the

default Play path if not specified.

Returns nothing.



29
30
31
32
33
# File 'lib/play/library.rb', line 29

def self.import_songs(path=Play.path)
  fs_songs(path).each do |path|
    import_song(path)
  end
end

.monitorObject

Monitors the music directory for any new music added to it. Once changed, Play will run through and reindex those directories.

Returns nothing.



8
9
10
11
12
13
14
# File 'lib/play/library.rb', line 8

def self.monitor
  FSSM.monitor(Play.path, '**/**/**', :directories => true) do
    update {|base, relative| Library.import_songs("#{base}/#{relative}") }
    delete {|base, relative| nil }
    create {|base, relative| Library.import_songs("#{base}/#{relative}") }
  end
end