Sha256: 237c9260e7c86cbc102ecb805ce840951b1a26a97494e75589762bc795e1d0d2

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Kagu

  class Tracks

    include Enumerable

    EXTENSIONS = %w(.aac .flac .mp3 .wav)

    attr_reader :library

    def initialize(library)
      raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
      @library = library
    end

    def each(&block)
      return unless block_given?
      Kagu.logger.debug('Kagu') { "Loading iTunes library tracks from #{library.path.inspect}" }
      File.open(library.path, 'r') do |file|
        while !file.eof? && (line = file.readline.strip)
          next unless line.starts_with?('<key>Track ID</key>')
          attributes = {}
          begin
            match = line.match(/<key>(.+)<\/key><(\w+)>(.*)<\/\2>/)
            next unless match
            name = "itunes_#{match[1].downcase.gsub(' ', '_')}"
            value = match[3]
            attributes[name] = value
          end while (line = file.readline.strip) != '</dict>'
          yield(Track.new(attributes)) if attributes['itunes_track_type'] == 'File' && attributes['itunes_podcast'].blank? && EXTENSIONS.include?(File.extname(attributes['itunes_location'].try(:downcase)))
        end
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kagu-1.0.0 lib/kagu/tracks.rb