Class: SpotifyCli::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify_cli/api.rb

Constant Summary collapse

PLAY =
""
STOP =
""
SPOTIFY_SEARCH_API =
"https://api.spotify.com/v1/search"

Class Method Summary collapse

Class Method Details

.help(mappings) ⇒ Object

Display Help

Usage:

- spotify help


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/spotify_cli/api.rb', line 149

def help(mappings)
  require 'method_source'

  Dex::UI.frame('Spotify CLI', timing: false) do
    puts "CLI interface for Spotify"
  end

  mappings.group_by { |_,v| v }.each do |k, v|
    v.reject! { |mapping| mapping.first == k.to_s }
    doc = self.method(k).comment.gsub(/^#\s*/, '')
    doc = strip_heredoc(doc)

    Dex::UI.frame(k, timing: false) do
      puts strip_heredoc(doc)
      next if v.empty?
      puts "\nAliases:"
      v.each { |mapping| puts " - #{mapping.first}" }
    end
  end
end

.nextObject

Changes to the next song

Usage:

- spotify next


14
15
16
17
# File 'lib/spotify_cli/api.rb', line 14

def next
  puts "Playing next song"
  SpotifyCli::App.next!
end

.pauseObject

Pause/stop the current song

Usage:

- spotify pause
- spotify stop


88
89
90
91
# File 'lib/spotify_cli/api.rb', line 88

def pause
  SpotifyCli::App.pause!
  status
end

.play_pauseObject

Play/Pause the current song, or play a specified artist, track, album, or uri

Usage:

- spotify play artist [name]
- spotify play track [name]
- spotify play album [name]
- spotify play uri [spotify uri]


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spotify_cli/api.rb', line 54

def play_pause
  args = ARGV[1..-1]

  if args.empty?
    # no specifying paremeter, this is a standard play/pause
    SpotifyCli::App.play_pause!
    status
    return
  end

  arg = args.shift
  type = arg == 'song' ? 'track' : arg

  Dex::UI.frame("Searching for #{type}", timing: false) do
    play_uri = case type
    when 'album', 'artist', 'track'
      results = search_and_play(type: type, query: args.join(' '))
      results.first
    when 'uri'
      args.first
    end
    puts "Results found, playing"
    SpotifyCli::App.play_uri!(play_uri)
    sleep 0.05 # Give time for the app to switch otherwise status may be stale
  end

  status
end

.previousObject

Changes to the previous song

Usage:

- spotify previous


23
24
25
26
# File 'lib/spotify_cli/api.rb', line 23

def previous
  puts "Playing previous song"
  SpotifyCli::App.prev!
end

.replayObject

Replays the current song

Usage:

- spotify replay


41
42
43
44
# File 'lib/spotify_cli/api.rb', line 41

def replay
  puts "Restarting song"
  SpotifyCli::App.replay!
end

.set_posObject

Sets the position in the song

Usage:

- spotify set_pos 60


32
33
34
35
# File 'lib/spotify_cli/api.rb', line 32

def set_pos
  puts "Setting position to #{ARGV[1]}"
  SpotifyCli::App.set_pos!(ARGV[1])
end

.statusObject

Show the current song

Usage:

- spotify
- spotify status


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spotify_cli/api.rb', line 98

def status
  stat = SpotifyCli::App.status

  time = "#{stat[:position]} / #{stat[:duration]}"
  state_sym = case stat[:state]
  when 'playing'
    PLAY
  else
    STOP
  end
  # 3 for padding around time, and symbol, and space for the symbol, 2 for frame
  width = Dex::UI::Terminal.width - time.size - 5

  yield if block_given?

  Dex::UI.frame(stat[:track], timing: false) do
    puts Dex::UI.resolve_text([
      "{{bold:Artist:}} #{stat[:artist]}",
      "{{bold:Album:}} #{stat[:album]}",
    ].join("\n"))
    puts [
      Dex::UI::Progress.progress(stat[:percent_done], width),
      state_sym,
      time
    ].join(' ')
  end
end

.watchObject

Watch and report the status the current song

Usage:

- spotify watch


130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/spotify_cli/api.rb', line 130

def watch
  printed = false
  while true
    status do
      if printed
        up = "\x1b[1A\x1b[1G"
        clear = "\x1b[2K\x1b[1G"
        5.times { print [up, clear].join }
      end
      printed = true
    end
    sleep 1
  end
end