Merge pull request #8 from mamantoha/binary-response fetch binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
diff --git a/.travis.yml b/.travis.yml
index f1a5a8f..d0ab184 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,9 +1,9 @@
language: ruby
rvm:
- - 3.0.0
- - 2.7.2
- - 2.6.6
+ - 3.1.2
+ - 3.0.4
+ - 2.7.6
script:
- rspec
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bea2963..d2614a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
# MPD::Client CHANGELOG
+## 0.2.0
+
+* Tested with Ruby 3.1
+* Add `albumart` command
+* Add `readpicture` command
+* Remove `playlist` command. Use `playlistinfo` instead
+
## 0.1.0
* Rename `MPDClient` to `MPD::Client`
diff --git a/Gemfile b/Gemfile
index 12e41e1..0f11a4b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -8,6 +8,7 @@ gemspec
group :development, :test do
gem 'pry'
gem 'rubocop'
+ gem 'solargraph'
end
group :test do
diff --git a/LICENSE b/LICENSE
index 49a7f85..e46750d 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2012 Anton Maminov
+Copyright (c) 2012-2022 Anton Maminov
MIT License
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/MPD_COMMANDS.md b/MPD_COMMANDS.md
index 5d7eb38..ac108ce 100644
--- a/MPD_COMMANDS.md
+++ b/MPD_COMMANDS.md
@@ -102,7 +102,7 @@ If the optional `SUBSYSTEMS` argument is used, MPD will only send notifications
---
`mixrampdb {deciBels} => fetch_nothing`
-> Sets the threshold at which songs will be overlapped. Like crossfading but doesn't fade the track volume, just overlaps. The songs need to have MixRamp tags added by an external tool. 0dB is the normalized maximum volume so use negative values, I prefer -17dB. In the absence of mixramp tags crossfading will be used. See [mixramp](https://sourceforge.net/projects/mixramp/)
+> Sets the threshold at which songs will be overlapped. Like crossfading but doesn't fade the track volume, just overlaps. The songs need to have MixRamp tags added by an external tool. 0dB is the normalized maximum volume so use negative values, I prefer -17dB. In the absence of mixramp tags crossfading will be used. See [mixramp](https://mpd.readthedocs.io/en/latest/user.html?highlight=mixramp#mixramp)
---
`mixrampdelay {SECONDS} => fetch_nothing`
diff --git a/README.md b/README.md
index bfd5ed9..8c64fdd 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,8 @@ gem install mpd_client
All functionality is contained in the `MPD::Client` class. Creating an instance of this class is as simple as:
```ruby
+require 'mpd_client'
+
client = MPD::Client.new
```
@@ -64,6 +66,26 @@ client.status # insert the status command into the list
client.command_list_end # result will be a Array with the results
```
+### Binary responses
+
+Some commands can return binary data.
+
+```ruby
+require 'mpd_client'
+
+client = MPD::Client.new
+client.connect('localhost', 6600)
+
+if (current_song = client.currentsong)
+ data, io = client.readpicture(current_song['file'])
+ io # StringIO
+ data # => {"size"=>"322860", "type"=>"image/jpeg", "binary"=>"3372"}
+ File.write('cover.jpg', io.string)
+end
+```
+
+The above will locate album art for the current song and save image to `cover.jpg` file.
+
### Ranges
Some commands(e.g. `move`, `delete`, `load`, `shuffle`, `playlistinfo`) support integer ranges(`[START:END]`) as argument. This is done in `mpd_client` by using two element array:
@@ -103,7 +125,7 @@ client = MPD::Client.new
client.log = Logger.new($stderr)
```
-For more information about logging configuration, see [Logger](https://ruby-doc.org/stdlib-2.5.1/libdoc/logger/rdoc/Logger.html)
+For more information about logging configuration, see [Logger](https://ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html)
## Development
@@ -121,6 +143,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
## License and Author
-Copyright (c) 2013-2018 by Anton Maminov
+Copyright (c) 2012-2022 by Anton Maminov
This library is distributed under the MIT license. Please see the LICENSE file.
diff --git a/examples/albumart.rb b/examples/albumart.rb
new file mode 100644
index 0000000..6e81635
--- /dev/null
+++ b/examples/albumart.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require 'bundler'
+Bundler.setup :default
+
+require 'logger'
+require 'mpd_client'
+
+# MPD::Client.log = Logger.new($stderr)
+
+client = MPD::Client.new
+client.connect('localhost', 6600)
+
+if (current_song = client.currentsong)
+ data, io = client.readpicture(current_song['file'])
+ puts data
+ File.write('cover.jpg', io.string)
+end
diff --git a/examples/client.rb b/examples/client.rb
new file mode 100644
index 0000000..a5bb0d5
--- /dev/null
+++ b/examples/client.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+require 'bundler'
+Bundler.setup :default
+
+require 'logger'
+require 'mpd_client'
+
+MPD::Client.log = Logger.new($stderr)
+
+client = MPD::Client.new
+client.connect('localhost', 6600)
+
+puts client.stats
+puts client.status
+puts client.currentsong
+puts client.playlistinfo
diff --git a/examples/stickers.rb b/examples/stickers.rb
index 6908f08..653e245 100644
--- a/examples/stickers.rb
+++ b/examples/stickers.rb
@@ -9,7 +9,7 @@ require 'mpd_client'
MPD::Client.log = Logger.new($stderr)
# Stickers
-# http://www.musicpd.org/doc/protocol/ch03s07.html
+# https://mpd.readthedocs.io/en/latest/protocol.html#stickers
client = MPD::Client.new
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 82b9655..6c2c82d 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -1,17 +1,17 @@
# frozen_string_literal: true
require 'socket'
+require 'stringio'
require 'mpd_client/version'
module MPD
HELLO_PREFIX = 'OK MPD '
ERROR_PREFIX = 'ACK '
- SUCCESS = 'OK'
- NEXT = 'list_OK'
+ SUCCESS = "OK\n"
+ NEXT = "list_OK\n"
- # MPD changelog: http://git.musicpd.org/cgit/master/mpd.git/plain/NEWS
- # http://www.musicpd.org/doc/protocol/command_reference.html
- # http://git.musicpd.org/cgit/cirrus/mpd.git/plain/doc/protocol.xml
+ # MPD changelog: https://github.com/MusicPlayerDaemon/MPD/blob/master/NEWS
+ # Protocol: https://mpd.readthedocs.io/en/latest/protocol.html
COMMANDS = {
# Status Commands
'clearerror' => 'fetch_nothing',
@@ -52,7 +52,6 @@ module MPD
'deleteid' => 'fetch_nothing',
'move' => 'fetch_nothing',
'moveid' => 'fetch_nothing',
- 'playlist' => 'fetch_playlist',
'playlistfind' => 'fetch_songs',
'playlistid' => 'fetch_songs',
'playlistinfo' => 'fetch_songs',
@@ -160,6 +159,7 @@ module MPD
def add_command(name, retval)
escaped_name = name.tr(' ', '_')
+
define_method escaped_name.to_sym do |*args|
ensure_connected
@@ -189,11 +189,12 @@ module MPD
def reconnect
log&.info("MPD (re)connect #{@host}, #{@port}")
- @socket = if @host.start_with?('/')
- UNIXSocket.new(@host)
- else
- TCPSocket.new(@host, @port)
- end
+ @socket =
+ if @host.start_with?('/')
+ UNIXSocket.new(@host)
+ else
+ TCPSocket.new(@host, @port)
+ end
hello
@connected = true
@@ -222,6 +223,7 @@ module MPD
raise 'Already in command list' unless @command_list.nil?
write_command('command_list_ok_begin')
+
@command_list = []
end
@@ -241,6 +243,14 @@ module MPD
# Sets the +logger+ used by this instance of MPD::Client
attr_writer :log
+ def albumart(uri)
+ fetch_binary(StringIO.new, 0, 'albumart', uri)
+ end
+
+ def readpicture(uri)
+ fetch_binary(StringIO.new, 0, 'readpicture', uri)
+ end
+
private
def ensure_connected
@@ -266,30 +276,34 @@ module MPD
reconnect
@socket.puts line
end
+
@socket.flush
end
def write_command(command, *args)
parts = [command]
+
args.each do |arg|
- line = if arg.is_a?(Array)
- arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\""
- else
- "\"#{escape(arg)}\""
- end
+ line =
+ if arg.is_a?(Array)
+ arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\""
+ else
+ "\"#{escape(arg)}\""
+ end
parts << line
end
+
# log.debug("Calling MPD: #{command}#{args}") if log
log&.debug("Calling MPD: #{parts.join(' ')}")
write_line(parts.join(' '))
end
def read_line
- line = @socket.gets.force_encoding('utf-8')
+ line = @socket.gets
+
raise 'Connection lost while reading line' unless line.end_with?("\n")
- line.chomp!
if line.start_with?(ERROR_PREFIX)
error = line[/#{ERROR_PREFIX}(.*)/, 1].strip
raise error
@@ -305,22 +319,22 @@ module MPD
line
end
- def read_pair(separator)
+ def read_pair
line = read_line
- return if line.nil?
- pair = line.split(separator, 2)
- raise "Could now parse pair: '#{line}'" if pair.size < 2
+ return if line.nil?
- pair # Array
+ line.split(': ', 2)
end
- def read_pairs(separator = ': ')
+ def read_pairs
result = []
- pair = read_pair(separator)
+
+ pair = read_pair
+
while pair
result << pair
- pair = read_pair(separator)
+ pair = read_pair
end
result
@@ -328,6 +342,7 @@ module MPD
def fetch_item
pairs = read_pairs
+
return nil if pairs.size != 1
pairs[0][1]
@@ -335,6 +350,7 @@ module MPD
def fetch_nothing
line = read_line
+
raise "Got unexpected value: #{line}" unless line.nil?
end
@@ -343,6 +359,8 @@ module MPD
seen = nil
read_pairs.each do |key, value|
+ value = value.chomp.force_encoding('utf-8')
+
if key != seen
raise "Expected key '#{seen}', got '#{key}'" unless seen.nil?
@@ -358,14 +376,18 @@ module MPD
def fetch_objects(delimeters = [])
result = []
obj = {}
+
read_pairs.each do |key, value|
key = key.downcase
+ value = value.chomp.force_encoding('utf-8')
+
if delimeters.include?(key)
result << obj unless obj.empty?
obj = {}
elsif obj.include?(key)
obj[key] << value
end
+
obj[key] = value
end
@@ -380,6 +402,41 @@ module MPD
objs ? objs[0] : {}
end
+ def fetch_binary(io = StringIO.new, offset = 0, *args)
+ data = {}
+
+ @mutex.synchronize do
+ write_command(*args, offset)
+
+ binary = false
+
+ read_pairs.each do |item|
+ if binary
+ io << item.join(': ')
+ next
+ end
+
+ key = item[0]
+ value = item[1].chomp
+
+ binary = (key == 'binary')
+
+ data[key] = value
+ end
+ end
+
+ size = data['size'].to_i
+ binary = data['binary'].to_i
+
+ next_offset = offset + binary
+
+ return [data, io] if next_offset >= size
+
+ io.seek(-1, IO::SEEK_CUR)
+
+ fetch_binary(io, next_offset, *args)
+ end
+
def fetch_changes
fetch_objects(['cpos'])
end
@@ -416,17 +473,9 @@ module MPD
fetch_objects(['playlist'])
end
- def fetch_playlist
- result = []
- read_pairs(':').each do |_key, value|
- result << value
- end
-
- result
- end
-
def fetch_stickers
result = []
+
read_pairs.each do |_key, sticker|
value = sticker.split('=', 2)
raise "Could now parse sticker: #{sticker}" if value.size < 2
@@ -443,6 +492,7 @@ module MPD
def fetch_command_list
result = []
+
begin
@command_list.each do |retval|
result << (eval retval)
@@ -456,9 +506,11 @@ module MPD
def hello
line = @socket.gets
+
raise 'Connection lost while reading MPD hello' unless line.end_with?("\n")
line.chomp!
+
raise "Got invalid MPD hello: #{line}" unless line.start_with?(HELLO_PREFIX)
@mpd_version = line[/#{HELLO_PREFIX}(.*)/, 1]
diff --git a/lib/mpd_client/version.rb b/lib/mpd_client/version.rb
index dd7e7b5..ce99ef8 100644
--- a/lib/mpd_client/version.rb
+++ b/lib/mpd_client/version.rb
@@ -2,6 +2,6 @@
module MPD
class Client
- VERSION = '0.1.1'
+ VERSION = '0.2.0'
end
end
diff --git a/mpd_client.gemspec b/mpd_client.gemspec
index 195c492..ccc7320 100644
--- a/mpd_client.gemspec
+++ b/mpd_client.gemspec
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'
gem.add_development_dependency 'bundler'
+ gem.metadata['rubygems_mfa_required'] = 'true'
end