Class: KBL::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/kbl/cli.rb

Defined Under Namespace

Classes: MetaData

Instance Method Summary collapse

Instance Method Details

#dump(input_filename = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kbl/cli.rb', line 106

def dump(input_filename=nil)
  input = open_file(input_filename) || STDIN

  if output_filename = options[:output]
    output = open_file(output_filename, 'w')
    if output.nil?
      puts "Cannot open file #{output_filename} for output."
      exit 1
    end
  else
    output = STDOUT
  end

  ids = input.readlines
  ids.each(&:chomp!).keep_if {|id| id.match(/\d+/) }#.delete("")

  if ids.empty?
    puts "No songs to export. Exit."
    exit 2
  end

   = {}

  db_files = Dir.glob(File.expand_path("~/Library/Application Support/KKBOX/*/Playlists2.db"))

  db_files.each do |db_file|
    db = SQLite3::Database.new db_file

    sql = <<-SQL
      SELECT song_id, song_name, artist_id, artist_name, album_id, album_name
      FROM song_tracks
      WHERE song_id IN (#{ids.map {|id|"'#{id}'"}.join(',')});
    SQL

    db.execute(sql) do |row|
      data = MetaData.new(*row)

      [data.song_id] = data
    end
  end

  ids.each do |id|
    if data = [id]
      output.puts data.send(:"to_#{options[:format]}")
    else
      STDERR.puts "ID Not Found: #{id}"
    end
  end
end

#import(filename = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kbl/cli.rb', line 38

def import(filename=nil)
  input = open_file(filename) || STDIN

  ids = input.readlines
  ids.each(&:chomp!).keep_if {|id| id.match(/\d+/) }#.delete("")

  if ids.empty?
    puts "No songs to import. Exit."
    exit 2
  end

  if !options[:yes]
    puts "\e[1;31mThis will import #{ids.size} songs into KKBOX and play the last song.\e[m"

    while answer = Readline.readline("Continue? [Y/n] ", false)
      case answer.downcase
      when 'n', 'no'
        puts "Cancelled."
        exit 3
      when 'y', 'yes', ''
        break # stop asking and continue importing
      else
        puts "Please answer 'y' or 'n', or hit enter for 'y'."
      end
    end
  end

  puts "Importing..."

  system "open #{ids.map { |id| "kkbox://play_song_#{id}" }.join(' ')}"
end