-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitd.py
537 lines (474 loc) · 16.8 KB
/
itd.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# Python MPD/iTunes library
# Copyright (C) 2010 Nathan Charles
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
HELLO_PREFIX = "OK MPD "
ERROR_PREFIX = "ACK "
SUCCESS = "OK"
NEXT = "list_OK"
import appscript
import math
import SocketServer
class iTunesModel(object):
"""This class maps mpd keywords to iTunes applescript calls
"""
def __init__(self):
try:
self.iTunes = appscript.app('iTunes')
except:
return -1
def _getstate(self):
"""This is a setup function in case iTunes views have changed and
current state is undefined
returns tuple of playlist and track"""
playlist = ""
track = ""
try:
playlist = self.iTunes.current_playlist()
track = self.iTunes.current_track()
except:
try:
self.iTunes.play()
self.iTunes.pause()
except:
return -1,-1
return playlist, track
def undefined():
"""
playid [SONGID]
Begins playing the playlist at song SONGID.
seek {SONGPOS} {TIME}
Seeks to position TIME (in seconds) of entry SONGPOS in the playlist.
seekid {SONGID} {TIME}
Seeks to the position TIME (in seconds) of song SONGID.
"""
pass
def play(self, song = -1):
"""
play [SONGPOS]
Begins playing the playlist at song number SONGPOS.
"""
self.iTunes.play()
#return True
def pause(self, bool = 1):
"""
pause {PAUSE}
Toggles pause/resumes playing, PAUSE is 0 or 1.
Note
The use of pause command w/o the PAUSE argument is deprecated.
"""
self.iTunes.pause()
#return True
def next(self):
"""
Plays next song in the playlist.
"""
self.iTunes.next_track()
#return True
def previous(self):
"""
Plays previous song in the playlist.
"""
self.iTunes.previous_track()
#return True
def stop(self):
"""
Stops playing.
"""
self.iTunes.stop()
#return True
#STATUS
def status(self):
"""
volume: 0-100
repeat: 0 or 1
single: 0 or 1
consume: 0 or 1
playlist: 31-bit unsigned integer, the playlist version number
playlistlength: integer, the length of the playlist
state: play, stop, or pause
song: playlist song number of the current song stopped on or playing
songid: playlist songid of the current song stopped on or playing
nextsong: playlist song number of the next song to be played
nextsongid: playlist songid of the next song to be played
time: total time elapsed (of current playing/paused song)
elapsed:
Total time elapsed within the current song, with higher resolution.
bitrate: instantaneous bitrate in kbps
xfade: crossfade in seconds
audio: sampleRate:bits:channels
updatings_db: job id
error: if there is an error, returns message here
volume: -1\nrepeat: 0\nrandom: 0\nsingle: 0\nconsume: 0\nplaylist: 2\nplaylistlength: 0\nxfade: 0\nstate: stop\nlist_OK\nOK\n
0.15.0
volume: 100
repeat: 0
random: 0
single: 0
consume: 0
playlist: 5
playlistlength: 3
xfade: 0
state: play
song: 0
songid: 0
time: 284:390
bitrate: 256
audio: 44100:24:2
nextsong: 1
nextsongid: 1
list_OK
file: Us vs Us/01 Banner.mp3
Time: 390
Artist: Psalters
Title: Banner
Album: Us vs Us
Track: 1/14
Date: 2004
Genre: World
Disc: 1/1
Pos: 0
Id: 0
list_OK
OK
"""
cplaylist, ctrack = self._getstate()
repeat = {appscript.k.off: [0,0],
appscript.k.one:[1,1],
appscript.k.all:[1,0]}
#stopped/playing/paused/fast forwarding/rewinding'
state = { appscript.k.stopped: "stop",
appscript.k.playing: "play",
appscript.k.paused: "pause",
appscript.k.fast_forwarding: "play",
appscript.k.rewinding: "play"}
random = { True : 1,
False : 0}
ret = "volume: %s\n" % self.iTunes.sound_volume.get()
ret += "repeat: %s\n" % repeat[cplaylist.song_repeat.get()][0]
ret += "random: %s\n" % random[cplaylist.shuffle.get()]
ret += "single: %s\n" % repeat[cplaylist.song_repeat.get()][1]
ret += "consume: %s\n" % "0"
ret += "playlist: %s\n" % "2"
ret += "playlistlength: %s\n" % cplaylist.duration.get()
ret += "xfade: %s\n" % "0"
ret += "state: %s\n" % state[self.iTunes.player_state.get()]
ret += "song: %s\n" % "1"
ret += "songid: %s\n" % "1"
ret += "time: %s:%s\n" % ( self.iTunes.player_position.get(),
math.trunc(ctrack.duration.get()))
ret += "bitrate: %s\n" % "256"
ret += "audio: %s\n" % "44100:24:2"
ret += "nextsong: %s\n" % "2"
ret += "nextsongid: %s\n" % "2"
return ret
def stats(self):
"""
Displays statistics.
artists: number of artists
songs: number of albums
uptime: daemon uptime in seconds
db_playtime: sum of all song times in the db
db_update: last db update in UNIX time
playtime: time length of music played
"""
pass
def currentsong(self):
cplaylist, ctrack = self._getstate()
ret = "Time: %s\n" % math.trunc(ctrack.duration.get())
ret += "Album: %s\n" % ctrack.album.get()
ret += "Artist: %s\n" % ctrack.artist.get()
ret += "Title: %s\n" % self.iTunes.current_track.name.get()
ret += "Track: %s\n" % self.iTunes.current_track.track_number.get()
ret += "Pos: %s\n" % self.iTunes.player_position.get()
return ret
def plchanges(self, args):
pass
def lsinfo(self, args):
"""
Usage
lsinfo [<string directory>]
Purpose
List contents of <string directory>, from the database.
Arguments
<string directory>
"""
pass
def search(self, args):
"""
search <string type> <string what>
search filename bastards_pick_wrong_man
file: vocal/onion_radio_news/the_onion_radio_news_-_bastards_pick_wrong_man.mp3
Time: 36
Artist: Onion Radio News
Title: Bastards Pick Wrong Man to Mess With
Genre: Comedy
OK
"""
print args
cplaylist, ctrack = self._getstate()
type,what = args.split('" "')
ret = ""
query = what.strip().strip('\"')
#print cplaylist.file_tracks()
print "query '%s'" % query
for i in cplaylist.search(for_=query):
ret += "file: %s/%s.mp3\n" % (i.artist(), i.name())
#print i.file_track.location()
ret += "Time: %s\n" % math.trunc(i.duration())
ret += "Title: %s\n" % i.name()
ret += "Artist: %s\n" % i.artist()
ret += "Album: %s\n" % i.album()
ret += "Track: %s\n" % i.track_number()
ret += "Date: %s\n" % i.year()
ret += "Genre: %s\n" % i.genre()
return ret
#PLAYBACK OPTIONS
def consume(self, args):
"""
consume {STATE}
Sets consume state to STATE, STATE should be 0 or 1. When consume is activated, each song played is removed from playlist.
"""
pass
def crossfade(self, args):
"""
crossfade {SECONDS}
Sets crossfading between songs.
"""
pass
def random(self, args):
"""
random {STATE}
Sets random state to STATE, STATE should be 0 or 1.
song repeat (off/one/all) : playback repeat mode
"""
pass
def repeat(self, args):
"""
repeat {STATE}
Sets repeat state to STATE, STATE should be 0 or 1.
song repeat (off/one/all) : playback repeat mode
"""
cplaylist, ctrack = self._getstate()
state = { "off":0, "all":1}
cplaylist.song_repeat.set(state[args])
def single(self, args):
"""
single {STATE}
Sets single state to STATE, STATE should be 0 or 1. When single is
activated, playback is stopped after current song, or song is repeated
if the 'repeat' mode is enabled.
"""
cplaylist, ctrack = self._getstate()
state = { "off":0, "one":1}
cplaylist.song_repeat.set(state[args])
def setvol(self, args):
"""
setvol {VOL}
Sets volume to VOL, the range of volume is 0-100.
"""
self.iTunes.sound_volume.set(args)
def to_code():
"""
Playback options
replay_gain_mode {MODE}
Sets the replay gain mode. One of off, track, album.
Changing the mode during playback may take several seconds, because
the new settings does not affect the buffered data.
This command triggers the options idle event.
replay_gain_status
Prints replay gain options. Currently, only the variable
replay_gain_mode is returned.
"""
pass
class controller(object):
"""This class impliments a controller that parses data into commands and
handles executes those commands
The default backend is iTunes via applescript but something else could be
implimented.
"""
def __init__(self, backend = iTunesModel()):
self.iterate = False
self.model = backend
self._commands = {
# Status Commands
"clearerror": self._undefined,
"currentsong": self.model.currentsong,
"idle": self._undefined,
"noidle": None,
"status": self.model.status,
"stats": self._undefined,
# Playback Option Commands
"consume": self._undefined,
"crossfade": self._undefined,
"random": self.model.random,
"repeat": self.model.repeat,
"setvol": self.model.setvol,
"single": self.model.single,
"volume": self._undefined,
# Playback Control Commands
"next": self.model.next,
"pause": self.model.pause,
"play": self.model.play,
"playid": self._undefined,
"previous": self.model.previous,
"seek": self._undefined,
"seekid": self._undefined,
"stop": self._undefined,
# Playlist Commands
"add": self._undefined,
"addid": self._undefined,
"clear": self._undefined,
"delete": self._undefined,
"deleteid": self._undefined,
"move": self._undefined,
"moveid": self._undefined,
"playlist": self._undefined,
"playlistfind": self._undefined,
"playlistid": self._undefined,
"playlistinfo": self._undefined,
"playlistsearch": self._undefined,
"plchanges": self.model.plchanges,
"plchangesposid": self._undefined,
"shuffle": self._undefined,
"swap": self._undefined,
"swapid": self._undefined,
# Stored Playlist Commands
"listplaylist": self._undefined,
"listplaylistinfo": self._undefined,
"listplaylists": self._undefined,
"load": self._undefined,
"playlistadd": self._undefined,
"playlistclear": self._undefined,
"playlistdelete": self._undefined,
"playlistmove": self._undefined,
"rename": self._undefined,
"rm": self._undefined,
"save": self._undefined,
# Database Commands
"count": self._undefined,
"find": self._undefined,
"list": self._undefined,
"listall": self._undefined,
"listallinfo": self._undefined,
"lsinfo": self._undefined,
"search": self.model.search,
"update": self._undefined,
# Connection Commands
"close": None,
"kill": None,
"password": self._undefined,
"ping": self._undefined,
# Audio Output Commands
"disableoutput": self._undefined,
"enableoutput": self._undefined,
"outputs": self._undefined,
# Reflection Commands
"commands": self._undefined,
"notcommands": self._undefined,
"tagtypes": self._undefined,
"urlhandlers": self._undefined,
# stuff
"command_list_ok_begin": self._undefined,
"command_list_end": self._undefined,
}
def _undefined(self, attr=None):
print "undefined"
def _execute(self, command):
cmd, sep, args = command.partition(' ')
print "running command: %s" % cmd
if cmd:
try:
if args:
return self._commands[cmd.strip()](args)
#return getattr(self.model, cmd.strip())(args)
else:
return self._commands[cmd.strip()]()
#return getattr(self.model, cmd.strip())(args)
except:
return self._undefined()
print "undefined"
def handle(self, command):
retval = ''
if command.find('list_ok') is not -1:
ListOK = True
else:
ListOK = False
if command.find('command_list') is not -1:
command_list = command.strip().split('\n')
print command_list
for c in command_list:
if c:
rc = self._execute(c)
if rc:
retval += rc
if ListOK:
retval += NEXT + "\n"
else:
retval = self._execute(command)
if retval:
return "%sOK\n" % retval
else:
return "OK\n"
def escape(text):
return text.replace("\\", "\\\\").replace('"', '\\"').replace(' ',"_")
class iTDRequestHandler(SocketServer.BaseRequestHandler ):
"""This class provides an interface which impliments the mpd spec
"""
def setup(self):
print self.client_address, 'connected!'
self.request.send('OK MPD ' + version + '\n')
def handle(self):
data = 'dummy'
while data:
data = self.request.recv(1024)
print data
#self.request.send(mpd.command(data))
ret = mpd.handle(data)
print ret
self.request.send(ret)
def finish(self):
print self.client_address, 'disconnected!'
def usage():
pass
if __name__ == "__main__":
import getopt
import sys
opts, args = getopt.getopt(sys.argv[1:], 'dfh')
if opts:
for o,a in opts:
if o == '-h':
usage()
sys.exit(1)
if o == '-d':
#run in background
import daemon
daemon.daemonize()
if o == '-f':
#run in forground
print "Running in Foreground"
else:
usage()
sys.exit(1)
try:
version ='0.16.0'
mpdport = 6600
mpd = controller()
server = SocketServer.ThreadingTCPServer(('', mpdport),
iTDRequestHandler)
server.serve_forever()
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
raise