Class: TLS::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/the_little_streamer/finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ Finder

Returns a new instance of Finder.



6
7
8
9
10
# File 'lib/the_little_streamer/finder.rb', line 6

def initialize root, path
  @root = root
  @paths = [path] #maybe support multiple paths in future
  @songs = nil
end

Instance Method Details

#each_fileObject



20
21
22
23
24
25
26
# File 'lib/the_little_streamer/finder.rb', line 20

def each_file
  @paths.each do |path|
    Dir.glob "#{path}/**/*.{mp3,mP3,Mp3,MP3,ogg,Ogg,OGg,OGG,oGg,oGG,ogG,OgG}" do |file|
      yield file
    end
  end
end

#each_song(&block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/the_little_streamer/finder.rb', line 12

def each_song &block
  if @songs
    @songs.each(&block)
  else
    get_songs(&block)
  end
end

#get_song(file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/the_little_streamer/finder.rb', line 45

def get_song file
  song = nil

  begin
    TagLib::FileRef.open(file) do |fileref|
      info = fileref.tag

      artist, album, title, track = info.artist, info.album, info.title, info.track

      artist = "Unknown" if artist.nil? or artist.empty?
      album = "Unknown" if album.nil? or album.empty?
      title = File.basename(file).split('.').first if title.nil? or title.empty?

      if title
        [artist, album, title].each { |i| i.tr!('/', '-') }

        song = Song.new(artist, album, title, track, Pathname.new(file).relative_path_from(@root).to_s)
      end
    end
  rescue Exception => e
    p e
    #Should probably do something here?
  end

  song
end

#get_songsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/the_little_streamer/finder.rb', line 28

def get_songs
  puts "Getting songs..."
  @songs = []

  each_file do |file|
    song = get_song file

    if song
      @songs << song
      yield song
    end
  end

  GC.start #Clean up after TagLib2
  @songs
end