forked from ZaneA/XMMSinatra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmmsinatra.rb
executable file
·129 lines (105 loc) · 3.47 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
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra'
require 'erb'
require 'xmmsclient'
require 'utils'
require 'uri'
configure(:development) do |c|
require "sinatra/reloader"
c.also_reload "utils.rb"
end
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
=begin
#irb ad-hoc test code
irb -r xmmsclient -r utils.rb -r uri
Conn = Xmms::Client.new('XMMSinatra').connect(ENV['XMMS_PATH'])
pl = Conn.playlist.entries.wait.value.select(&:nonzero?).map{|id| Conn.medialib_get_info(id).wait.value.to_propdict}
pl.split_by_(proc {|i| i[:artist]}) {|i,j| puts i}
pl.split_by{|i| i[:artist]}
pl.split_by_(proc {|i| i[:album]}){|al,is| ar = nil; is.each{|i| if ar == nil; ar = i[:artist].split(/; | \/ /); else ar &= i[:artist].split(/; | \/ /); end}; ar}
pl.split_by_(proc {|i| i[:album]}){|al,is| ar = is.map{|i| i[:artist].split(/; | \/ /)}.common}
=end
def album(i)
i[:album]._?((path = URI.unescape(i[:url]).gsub('+',' ').path_components)[1..[path.length - 4, 1].max].reverse.join(": "))
end
def artists(i)
i[:artist].split(/; | \/ /)
end
def title(i)
i[:title]._? i[:url]
end
get '/playlist' do
content_type 'text/json'
%({"albums": [) +
Conn.playlist.entries.wait.value.
select(&:nonzero?).map{|id| Conn.medialib_get_info(id).wait.value.to_propdict}.
split_by {|i| album(i)}.map{|al,is|
ar = is.common{|i| artists(i)}
%({"album": "#{al}", "artist":"#{ar.join "; "}", "items":[) + (is.map{|i|
%({"id": "#{i[:id]}", "artist": "#{(artists(i) - ar).join "; "}", "album": "#{album i}", "title": "#{title i}"})
}.join(", ")) + "]}"
}.join(", ") + "]}"
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
File.read('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