Class: Spotify::Music::Importer::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify/music/importer.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



46
47
48
49
50
51
52
53
54
# File 'lib/spotify/music/importer.rb', line 46

def client
  @client ||= begin
                if @options[:access_token]
                  Spotify::Client.new({:raise_errors => true}.merge(@options))
                else
                  Spotify::Client.new(:raise_errors => true)
                end
              end
end

#format_query(record) ⇒ Object



42
43
44
# File 'lib/spotify/music/importer.rb', line 42

def format_query(record)
  "track:#{record.name} artist:#{record.artist} album:#{record.album}"
end

#import(filename, options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spotify/music/importer.rb', line 13

def import(filename, options)
  @options = options.dup
  @library = SpotifyLibrary.new(client)
  require 'pry' ; binding.pry
  limit = options.fetch(:limit) { nil }
  skip  = options.fetch(:skip) { 0 }

  collection = CSV.read(filename, :headers => true)
  collection.each_with_index do |row, index|
    next if skip && skip > index+1
    break if limit && index+1 > limit+skip

    record = CollectionRecord.new(row, :clean_album => true, :clean_track => true)
    results = SpotifyMatch.new(client.search(:track, format_query(record)), :clean_album => true, :clean_track => true)
    @library.find_and_add_to_library(record, results, index)
  end

  if options[:missing]
    File.open(options[:missing], 'w') do |f|
      puts "Writing to file #{options[:missing]}"
      f.write(JSON.pretty_generate(importer.missing))
    end
  end
end

#missingObject



38
39
40
# File 'lib/spotify/music/importer.rb', line 38

def missing
  @library.missing
end