Better support for fetching stickers from MPD
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
diff --git a/examples/search_and_replace_playlist.rb b/examples/search_and_replace_playlist.rb
index 1a902b2..210fdc1 100644
--- a/examples/search_and_replace_playlist.rb
+++ b/examples/search_and_replace_playlist.rb
@@ -3,12 +3,18 @@
require 'bundler'
Bundler.setup :default
+require 'logger'
require 'mpd_client'
+#MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+
type = ARGV[0]
what = ARGV[1]
client = MPDClient.new
+client.log = Logger.new($stderr)
# Connecting to the server
client.connect('localhost', 6600)
diff --git a/examples/stickers.rb b/examples/stickers.rb
new file mode 100644
index 0000000..b4b0cd5
--- /dev/null
+++ b/examples/stickers.rb
@@ -0,0 +1,50 @@
+# encoding: utf-8
+
+require 'bundler'
+Bundler.setup :default
+
+require 'logger'
+require 'mpd_client'
+
+type = ARGV[0]
+what = ARGV[1]
+
+client = MPDClient.new
+client.log = Logger.new($stderr)
+
+# Connecting to the server
+client.connect('/var/run/mpd/socket')
+
+puts "MPD version: #{client.mpd_version}"
+puts "mpd_client version: #{MPDClient::VERSION}"
+
+uri = "world/j/Jane Air/2012.Иллюзия полёта/12. Любить любовь.ogg"
+
+# sticker set {TYPE} {URI} {NAME} {VALUE}
+# Adds a sticker value to the specified object. If a sticker item with that name already exists, it is replaced.
+#
+client.sticker_set('song', uri, 'rating', '1')
+
+# sticker get {TYPE} {URI} {NAME}
+# Reads a sticker value for the specified object.
+#
+puts client.sticker_get('song', uri, 'rating')
+
+# sticker list {TYPE} {URI}
+# Lists the stickers for the specified object.
+#
+puts client.sticker_list('song', uri)
+
+# sticker find {TYPE} {URI} {NAME}
+# Searches the sticker database for stickers with the specified name, below the specified directory (URI).
+# For each matching song, it prints the URI and that one sticker's value.
+#
+puts client.sticker_find('song', '/', 'rating')
+
+# sticker delete {TYPE} {URI} [NAME]
+# Deletes a sticker value from the specified object. If you do not specify a sticker name, all sticker values are deleted.
+#
+puts client.sticker_delete('song', uri, 'rating')
+
+client.close
+client.disconnect
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 2f5336c..70872b1 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -87,10 +87,10 @@ COMMANDS = {
"update" => "fetch_item",
"rescan" => "fetch_item",
# Sticker Commands
- "sticker get" => "fetch_item",
+ "sticker get" => "fetch_sticker",
"sticker set" => "fetch_nothing",
"sticker delete" => "fetch_nothing",
- "sticker list" => "fetch_list",
+ "sticker list" => "fetch_stickers",
"sticker find" => "fetch_songs",
# Connection Commands
"close" => "",
@@ -183,7 +183,7 @@ class MPDClient
return fetch_command_list
end
- # Sets the +logger+ used by this instance of MPDClient
+ # The current logger. If no logger has been set MPDClient.log is used
#
def log
@log || MPDClient.log
@@ -195,8 +195,6 @@ class MPDClient
@log = logger
end
-
-
private
def execute(command, *args, retval)
@@ -334,6 +332,19 @@ class MPDClient
return 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
+ result << Hash[*value]
+ end
+
+ return result
+ end
+
+ def fetch_sticker; fetch_stickers[0]; end
+
def fetch_command_list
result = []
begin