-
Notifications
You must be signed in to change notification settings - Fork 2
/
xmmsinatra.rb
executable file
·232 lines (193 loc) · 9.45 KB
/
xmmsinatra.rb
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
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra'
require 'erb'
require 'xmmsclient'
Conn = Xmms::Client.new('XMMSinatra').connect(ENV['XMMS_PATH'])
get '/' do
erb :index
end
get '/info' do
content_type 'text/json'
id = Conn.playback_current_id.wait.value
if (!id)
%({"id": "", "artist": "", "album": "", "title": ""})
else
info = Conn.medialib_get_info(id).wait.value.to_propdict
if (!info[:title].nil?)
title = info[:title]
else
title = info[:url]
end
%({"id": "#{info[:id]}", "artist": "#{info[:artist]}", "album": "#{info[:album]}", "title": "#{title}"})
end
end
get '/playlist' do
content_type 'text/json'
body = %({"items": [)
Conn.playlist.entries.wait.value.each_with_index do |id, index|
if (id.zero?)
next
end
info = Conn.medialib_get_info(id).wait.value.to_propdict
if (!index.zero?)
body += ','
end
if (!info[:title].nil?)
title = info[:title]
else
title = info[:url]
end
body += %({"id": "#{info[:id]}", "artist": "#{info[:artist]}", "album": "#{info[:album]}", "title": "#{title}"})
end
body += ']}'
body
end
get '/art/:id' do |id|
content_type 'image/jpeg'
info = Conn.medialib_get_info(id.to_i).wait.value.to_propdict
if (info[:picture_front])
Conn.bindata_retrieve(info[:picture_front]).wait.value
else
send_file 'nocover.png'
end
end
get '/search/:query' do |query|
c = Xmms::Collection.parse(query)
reply = Conn.coll_query_ids(c).wait.value
if (reply.length > 0)
Conn.playlist.clear.wait
reply.each do |id|
if (id.zero?)
next
end
Conn.playlist.add_entry(id).wait
end
"OK"
else
"FAIL"
end
end
get '/select/:id' do |id|
Conn.playlist_set_next(id.to_i - 1).wait
Conn.playback_tickle.wait
end
get '/playpause' do
if (Conn.playback_status.wait.value == Xmms::Client::PLAY)
Conn.playback_pause.wait
else
Conn.playback_start.wait
end
end
get '/prev' do
Conn.playlist_set_next_rel(-1).wait
Conn.playback_tickle.wait
end
get '/next' do
Conn.playlist_set_next_rel(1).wait
Conn.playback_tickle.wait
end
__END__
@@ index
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XMMSinatra</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="icon" type="image/png" href="/favicon.ico" />
</head>
<body>
<div id="header">
<a href="http://github.com/ZaneA/XMMSinatra" target="_blank">XMMSinatra</a>
</div>
<div id="info">
<span id="id"></span>
<span id="artist"></span>
<span id="title"></span>
<span id="album"></span>
</div>
<div id="menu">
<ul>
<li><a id="previous"><img src="media-previous-inv.png" /></a></li>
<li><a id="playpause"><img src="media-play-pause-resume-inv.png" /></a></li>
<li><a id="next"><img src="media-next-inv.png" /></a></li>
</ul>
<ul>
<li><input type="text" id="search" value="Search" /></li>
</ul>
</div>
<div id="playlist">
</div>
<script type="text/javascript">
$(function () {
var lastAlbum = "";
$("#playlist").click(function (e) {
if ($(e.target).parent().is(".item")) {
$.get('/select/' + $(e.target).parent().find(".pid").html());
setTimeout(updateInfo, 500);
}
});
function updateInfo() {
$.getJSON("/info", function (data) {
if (data.id != $("#id").html()) {
$("#id").html(data.id);
$("#artist").html(data.artist);
$("#title").html(data.title.replace(/ \(.*?\)/, ''));
$("#album").html(data.album.replace(/ \(.*?\)/, ''));
document.title = data.title + " - " + data.artist + " - XMMSinatra";
updatePlaylist();
}
});
};
function updatePlaylist() {
$("#playlist").html("");
lastAlbum = "";
$.getJSON("/playlist", function (data) {
$.each(data.items, function (i, item) {
if (lastAlbum != item.album) {
lastAlbum = item.album;
$("#playlist").append("<div class=\"albumitem\">" + item.album + "</div>");
$("#playlist").append("<div class=\"albumartwrap\"><img class=\"albumart\" src=\"/art/" + item.id + "\" /></div>");
}
if (item.id == $("#id").html()) {
$("#playlist").append("<div class=\"item active\"><span class=\"pid\">" + (i + 1) + "</span><span class=\"id\">" + item.id + "</span><span class=\"artist\">" + item.artist + "</span><span class=\"title\">" + item.title + "</span></div>");
} else {
$("#playlist").append("<div class=\"item\"><span class=\"pid\">" + (i + 1) + "</span><span class=\"id\">" + item.id + "</span><span class=\"artist\">" + item.artist + "</span><span class=\"title\">" + item.title + "</span></div>");
}
});
});
};
updateInfo();
setInterval(updateInfo, 10000);
$("#playpause").click(function () {
$.get('/playpause');
});
$("#previous").click(function () {
$.get('/prev');
setTimeout(updateInfo, 500);
});
$("#next").click(function () {
$.get('/next');
setTimeout(updateInfo, 500);
});
$("#search").keydown(function (event) {
$("#search").removeClass("invalid");
if (event.which == 13) {
$.get("/search/" + $("#search").val(), function (data) {
if (data == "OK")
updatePlaylist();
else
$("#search").addClass("invalid");
});
}
});
$(".item").click(function () {
$.get('/select/' + $(this).find(".pid").html());
setTimeout(updateInfo, 500);
});
});
</script>
</body>
</html>