forked from vim-scripts/vimmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmms2wrap.py
234 lines (200 loc) · 7.1 KB
/
xmms2wrap.py
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
#coding: utf-8
import os.path
import os
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
import xmmsclient
import urllib
import locale
class Controller(object):
"""A simple wrapper class of xmmsclient.XMMS."""
def __init__(self, options):
self.x = xmmsclient.XMMS('VIM')
self.type = "xmms2"
try:
self.x.connect()
except IOError:
os.system('xmms2-launcher > /dev/null')
self.x.connect()
self.options = options
self.system_encoding = locale.getdefaultlocale()[1]
def _get_status(self):
r = self.x.playback_status()
r.wait()
val = r.value()
if val == xmmsclient.PLAYBACK_STATUS_PLAY:
return "play"
elif val == xmmsclient.PLAYBACK_STATUS_PAUSE:
return "pause"
elif val == xmmsclient.PLAYBACK_STATUS_STOP:
return "stop"
else:
return "unknown"
def play(self, pos):
if self.current_position() != pos:
self.x.playlist_set_next(pos).wait()
self.x.playback_tickle().wait()
if self._get_status() != "play":
self.x.playback_start().wait()
def delete(self, pos):
"""Remove a song from playlist.
If the song to be removed is playing now, we first stop it.
"""
if self._get_status() == "play" and self.current_position() == pos:
self.x.playback_stop().wait()
self.x.playlist_remove_entry(pos).wait()
def clear(self):
self.x.playback_stop().wait()
self.x.playlist_clear().wait()
def pause(self):
if self._get_status() == "pause":
self.x.playback_start().wait()
else:
self.x.playback_pause().wait()
def get_volume(self):
r = self.x.playback_volume_get()
r.wait()
return r.value()
def _set_volume(self, val):
self.x.playback_volume_set('left', val).wait()
self.x.playback_volume_set('right', val).wait()
def change_volume(self, num):
value = self.get_volume() + num
if 0 <= value <= 100:
self._set_volume(value)
else:
self._set_volume(0 if value < 0 else 100)
return self.get_volume()
def stop(self):
self.x.playback_stop().wait()
def shuffle(self):
self.x.playlist_shuffle().wait()
def sort_playlist(self, mode):
if mode == "artist":
self.x.playlist_sort(('plugin/id3v2', 'artist')).wait()
elif mode == "title":
self.x.playlist_sort(('plugin/id3v2', 'title')).wait()
elif mode == "file":
self.x.playlist_sort(('server', 'url')).wait()
def save_playlist(self, name):
self.x.playlist_create(name).wait()
r = self.x.playlist_list_entries()
r.wait()
for i, id in enumerate(r.value()):
self.x.playlist_insert_id(i, id, name).wait()
def load_playlist(self, name):
self.x.playlist_load(name).wait()
def get_playlists(self):
r = self.x.playlist_list()
r.wait()
return [x.encode(self.system_encoding) for x in r.value()]
def get_current_playlist(self):
r = self.x.playlist_current_active()
r.wait()
return r.value().encode(self.system_encoding)
def add(self, path):
url = 'file://' + path
if os.path.isfile(path):
self.x.playlist_add_url(url).wait()
else:
self.x.playlist_radd(url).wait()
def current_position(self):
"""Return current position in the playlist."""
# It is an error to call playlist_current_pos when there are
# no entries in the playlist.
r = self.x.playlist_current_pos()
r.wait()
if r.iserror():
if self.options['debug']:
print r.get_error(), "in Controller.current_position."
return None
else:
return r.value()['position']
def playlist(self):
"""Format the playlist.
First we try to get imformation from id3v2, if it fails,
then try id3, else we just display the file name.
"""
def iconv(s):
encoding = self.options["id3_encoding"]
try:
if encoding:
t = s.encode('latin1').decode(encoding)
return t.encode(self.system_encoding)
else:
return s.encode('latin1')
except UnicodeEncodeError:
return ""
lst = []
r = self.x.playlist_list_entries()
r.wait()
for id in r.value():
r = self.x.medialib_get_info(id)
r.wait()
if r.iserror():
if self.options['debug']:
print r.get_error(), "in Controller.playlist."
lst.append(' ')
continue
song = r.value()
try:
artist = iconv(song[('plugin/id3v2', 'artist')])
except KeyError:
try:
artist = iconv(song[('plugin/mad', 'artist')])
except KeyError:
artist = ''
try:
title = iconv(song[('plugin/id3v2', 'title')])
except KeyError:
try:
title = iconv(song[('plugin/mad', 'title')])
except KeyError:
title = ''
try:
album = iconv(song[('plugin/id3v2', 'album')])
except KeyError:
try:
album = iconv(song[('plugin/mad', 'album')])
except KeyError:
album = ''
if artist == "" and title == "":
name = os.path.split(song[('server', 'url')])[1]
name = os.path.splitext(name)[0]
name = urllib.unquote(
name.decode(self.system_encoding).encode('latin1'))
name = name.replace("+", " ")
lst.append(' ' + name)
else:
line = self.options["playlist_format"]
line = line.replace("%title", title)
line = line.replace("%artist", artist)
line = line.replace("%album", album)
lst.append(' ' + line)
return lst
def set_repeat_mode(self, mode):
def set(key, val):
self.x.config_set_value(key, val).wait()
if mode == "all":
set("playlist.repeat_one", "0")
set("playlist.repeat_all", "1")
elif mode == "one":
set("playlist.repeat_one", "1")
set("playlist.repeat_all", "0")
elif mode == "off":
set("playlist.repeat_one", "0")
set("playlist.repeat_all", "0")
def get_repeat_mode(self):
d = {"0": False, "1": True}
r = self.x.config_get_value("playlist.repeat_one")
r.wait()
one = d[r.value()]
r = self.x.config_get_value("playlist.repeat_all")
r.wait()
all = d[r.value()]
if all:
return "all"
elif one:
return "one"
else:
return "off"