blob: 97ade60a2b4effbe77518ce13e7a8c8e8d45ae6a (
plain) (
blame)
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
|
#!/usr/bin/env ruby
#
# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
require 'socket'
class MPD
def initialize host='localhost', port=6600
@host = host
@port = port
end
def connect
# Connect to MPD and return response.
@socket = TCPSocket.new @host, @port
return @socket.gets.chomp
end
def send_request command
# Escape backslashes in command.
@socket.puts command.gsub('\\', '\\\\\\')
return get_response
end
def get_response
response = String.new
while true
line = @socket.gets
break if line == "OK\n"
return false if line =~ /^ACK/
response << line
end
return true if response.empty?
return response
end
def generate_hash str
hash = Hash.new
str.split("\n").each do |line|
field, value = line.split(': ')
hash[field.downcase.to_sym] = value
end
return hash
end
def currentsong
return generate_hash send_request 'currentsong'
end
def status
return generate_hash send_request 'status'
end
def stats
return generate_hash send_request 'stats'
end
def play songpos=false
command = 'play'
command << ' ' + songpos.to_s if songpos
return send_request command
end
def pause pause
return send_request 'pause ' + pause.to_s
end
def stop
return send_request 'stop'
end
def next
return send_request 'next'
end
def previous
return send_request 'previous'
end
def seek songpos, time
return send_request 'seek %s %s' % [songpos, time]
end
def add uri
return send_request 'add "%s"' % uri
end
def delete songpos
return send_request 'delete ' + songpos.to_s
end
def clear
return send_request 'clear'
end
def split_and_hash str
songs = str.split(/(?!\n)(?=file:)/).map do |song|
generate_hash song
end
return songs
end
# Not yet complete.
def playlistinfo
return split_and_hash send_request 'playlistinfo'
end
def find type, what
return split_and_hash send_request 'find %s "%s"' % [type, what]
end
def search type, what
return split_and_hash send_request 'search %s "%s"' % [type, what]
end
end
|