forked from mark-me/Pi-Jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpd_client.py
executable file
·853 lines (752 loc) · 32.9 KB
/
mpd_client.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# This file is part of pi-jukebox.
#
# pi-jukebox is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pi-jukebox 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with pi-jukebox. If not, see < http://www.gnu.org/licenses/ >.
#
# (C) 2015- by Mark Zwart, <[email protected]>
"""
==================================================================
**mpd_client.py**: controlling and monitoring mpd via python-mpd2.
==================================================================
"""
import sys, pygame
import time
import subprocess
import os
import glob
import mpd
from collections import deque
from mutagen import File
MPD_TYPE_ARTIST = 'artist'
MPD_TYPE_ALBUM = 'album'
MPD_TYPE_SONGS = 'title'
DEFAULT_COVER = 'default_cover_art.png'
TEMP_PLAYLIST_NAME = '_pi-jukebox_temp'
reload(sys)
sys.setdefaultencoding('utf8')
class MPDNowPlaying(object):
""" Song information
"""
def __init__(self):
self.playing_type = ''
self.__now_playing = None
self.title = "" # Currently playing song name
self.artist = "" # Currently playing artist
self.album = "" # Album the currently playing song is on
self.file = "" # File with path relative to MPD music directory
self.__time_current_sec = 0 # Currently playing song time (seconds)
self.time_current = "" # Currently playing song time (string format)
self.__time_total_sec = 0 # Currently playing song duration (seconds)
self.time_total = "" # Currently playing song duration (string format)
self.time_percentage = 0 # Currently playing song time as a percentage of the song duration
self.music_directory = ""
def now_playing_set(self, now_playing=None):
if now_playing is not None:
try:
self.file = now_playing['file']
except KeyError:
return False
if self.file[:7] == "http://":
self.playing_type = 'radio'
else:
self.playing_type = 'file'
if 'title' in now_playing:
self.title = now_playing['title'] # Song title of current song
else:
self.title = os.path.splitext(os.path.basename(now_playing['file']))[0]
if self.playing_type == 'file':
if 'artist' in now_playing:
self.artist = now_playing['artist'] # Artist of current song
else:
self.artist = "Unknown"
if 'album' in now_playing:
self.album = now_playing['album'] # Album the current song is on
else:
self.album = "Unknown"
current_total = self.str_to_float(now_playing['time'])
self.__time_total_sec = current_total
self.time_total = self.make_time_string(current_total) # Total time current
elif self.playing_type == 'radio':
if 'name' in now_playing:
self.album = now_playing['name'] # The radio station name
else:
self.album = "Unknown"
self.artist = ""
elif now_playing is None: # Changed to no current song
self.__now_playing = None
self.title = ""
self.artist = ""
self.album = ""
self.file = ""
self.time_percentage = 0
self.__time_total_sec = 0
self.time_total = self.make_time_string(0) # Total time current
def current_time_set(self, seconds):
if self.__time_current_sec != seconds: # Playing time current
self.__time_current_sec = seconds
self.time_current = self.make_time_string(seconds)
if self.playing_type != 'radio':
self.time_percentage = int(self.__time_current_sec / self.__time_total_sec * 100)
else:
self.time_percentage = 0
return True
else:
return False
def cover_art_get(self, dest_file_name="covert_art.jpg"):
if self.file == "" or self.playing_type == 'radio':
return DEFAULT_COVER
try:
music_file = File(self.music_directory + self.file)
except IOError:
return DEFAULT_COVER
cover_art = None
if 'covr' in music_file:
cover_art = music_file.tags['covr'].data
elif 'APIC:' in music_file:
cover_art = music_file.tags['APIC:'].data
else:
return DEFAULT_COVER
with open(dest_file_name, 'wb') as img:
img.write(cover_art) # write artwork to new image
return dest_file_name
def make_time_string(self, seconds):
minutes = int(seconds / 60)
seconds_left = int(round(seconds - (minutes * 60), 0))
time_string = str(minutes) + ':'
seconds_string = ''
if seconds_left < 10:
seconds_string = '0' + str(seconds_left)
else:
seconds_string = str(seconds_left)
time_string += seconds_string
return time_string
def str_to_float(self, s):
try:
return float(s)
except ValueError:
return float(0)
class MPDController(object):
""" Controls playback and volume
"""
def __init__(self):
self.mpd_client = mpd.MPDClient()
self.host = 'localhost'
self.port = 6600
self.update_interval = 1000 # Interval between mpc status update calls (milliseconds)
self.volume = 0 # Playback volume
self.playlist_current = [] # Current playlist song title
self.repeat = False #
self.random = False
self.single = False
self.consume = False
self.updating_library = False
self.__radio_mode = False
self.now_playing = MPDNowPlaying()
self.events = deque([]) # Queue of mpd events
# Database search results
self.searching_artist = "" # Search path user goes through
self.searching_album = ""
self.list_albums = []
self.list_artists = []
self.list_songs = []
self.list_query_results = []
self.__music_directory = ""
self.__now_playing = None # Dictionary containing currently playing song info
self.__now_playing_changed = False
self.__player_control = '' # Indicates whether mpd is playing, pausing or has stopped playing music
self.__muted = False # Indicates whether muted
self.__playlist_current_playing_index = 0
self.__last_update_time = 0 # For checking last update time (milliseconds)
self.__status = None # mps's current status output
def connect(self):
""" Connects to mpd server.
:return: Boolean indicating if successfully connected to mpd server.
"""
try:
self.mpd_client.connect(self.host, self.port)
except Exception:
return False
# Retrieve lists
self.artists_get()
self.albums_get()
try:
self.songs_get()
except Exception:
pass
self.__starts_with_radio()
# See if currently playing is radio station
return True
def __starts_with_radio(self):
was_playing = False # Indicates whether mpd was playing on start
now_playing = MPDNowPlaying()
try:
now_playing.now_playing_set(self.mpd_client.currentsong()) # Get currenly plating info
except mpd.ConnectionError:
self.mpd_client.connect(self.host, self.port)
now_playing.now_playing_set(self.mpd_client.currentsong())
if self.player_control_get() == 'play':
was_playing = True
if now_playing.playing_type == 'radio':
station_URL = now_playing.file # If now playing is radio station temporarily store
self.playlist_current_clear() # Clear playlist
try:
self.__radio_mode = False
self.mpd_client.load(TEMP_PLAYLIST_NAME) # Try to load previously temporarily stored playlist
self.playlist_current_get() # Set playlist
except Exception:
pass
self.__radio_mode = True # Turn on radio mode
self.mpd_client.clear() # Clear playlist
self.mpd_client.addid(station_URL) # Reload station
if was_playing:
self.mpd_client.play(0) # Resume playing
def disconnect(self):
""" Closes the connection to the mpd server. """
self.mpd_client.close()
self.mpd_client.disconnect()
def music_directory_set(self, path):
self.now_playing.music_directory = path
self.__music_directory = path
def __parse_mpc_status(self):
""" Parses the mpd status and fills mpd event queue
:return: Boolean indicating if the status was changed
"""
current_seconds = 0
current_total = 0
try:
now_playing = self.mpd_client.currentsong()
except Exception:
return False
if self.__now_playing != now_playing and len(now_playing) > 0: # Changed to a new song
self.now_playing.now_playing_set(now_playing)
if self.now_playing.playing_type == 'radio':
self.__radio_mode = True
else:
self.__radio_mode = False
self.__now_playing_changed = True
if self.__now_playing is None or self.__now_playing.track_file != now_playing.track_file:
self.events.append('playing_file')
self.events.append('playing_time_percentage')
try:
status = self.mpd_client.status()
except Exception:
return False
if self.__status == status:
return False
self.__status = status
self.playback_options_get(status)
if self.volume != int(status['volume']): # Current volume
self.volume = int(status['volume'])
self.events.append(['volume', self.volume])
self.__muted = self.volume == 0
if self.__player_control != status['state']:
self.__player_control = status['state']
self.events.append('player_control')
if self.__player_control != 'stop':
if self.__playlist_current_playing_index != int(status['song']): # Current playlist index
self.__playlist_current_playing_index = int(status['song'])
self.events.append('playing_index')
if self.now_playing.current_time_set(self.str_to_float(status['elapsed'])):
self.events.append('time_elapsed')
else:
if self.__playlist_current_playing_index != -1:
self.__playlist_current_playing_index = -1
self.events.append('playing_index')
if self.now_playing.current_time_set(0):
self.events.append('time_elapsed')
return True
def playback_options_get(self, status):
if self.repeat != status['repeat'] == '1':
self.repeat = status['repeat'] == '1'
self.events.append('repeat')
if self.random != status['random'] == '1':
self.random = status['random'] == '1'
self.events.append('random')
if self.single != status['single'] == '1':
self.single = status['single'] == '1'
self.events.append('single')
if self.consume != status['consume'] == '1':
self.consume = status['consume'] == '1'
self.events.append('consume')
def str_to_float(self, s):
try:
return float(s)
except ValueError:
return float(0)
def status_get(self):
""" Updates mpc data, returns True when status data is updated. Wait at
least 'update_interval' milliseconds before updating mpc status data.
:return: Returns boolean whether updated or not.
"""
self.mpd_client.ping()
time_elapsed = pygame.time.get_ticks() - self.__last_update_time
if pygame.time.get_ticks() > self.update_interval and time_elapsed < self.update_interval:
return False
self.__last_update_time = pygame.time.get_ticks() # Reset update
return self.__parse_mpc_status() # Parse mpc status output
def current_song_changed(self):
if self.__now_playing_changed:
self.__now_playing_changed = False
return True
else:
return False
def get_cover_art(self, dest_file_name="covert_art.jpg"):
return self.now_playing.cover_art_get()
def player_control_set(self, play_status):
""" Controls playback
:param play_status: Playback action ['play', 'pause', 'stop', 'next', 'previous'].
"""
if play_status == 'play':
if self.__player_control == 'pause':
self.mpd_client.pause(0)
else:
self.mpd_client.play()
elif play_status == 'pause':
self.mpd_client.pause(1)
elif play_status == 'stop':
self.mpd_client.stop()
elif play_status == 'next':
self.mpd_client.next()
elif play_status == 'previous':
self.mpd_client.previous()
def player_control_get(self):
""" :return: Current playback mode. """
self.status_get()
return self.__player_control
def play_playlist_item(self, index):
""" Starts playing in playlist on item.
:param index: Playlist item index
"""
if self.__radio_mode:
self.__radio_mode_set(False)
try:
self.mpd_client.play(index - 1)
except mpd.ConnectionError:
self.mpd_client.connect(self.host, self.port)
self.mpd_client.play(index - 1)
def volume_set(self, percentage):
""" Sets volume in absolute percentage.
:param percentage: Percentage at which volume should be set.
"""
if percentage < 0 or percentage > 100: return
try:
self.mpd_client.setvol(percentage)
except mpd.ConnectionError:
self.mpd_client.connect(self.host, self.port)
self.mpd_client.setvol(percentage)
self.volume = percentage
def volume_set_relative(self, percentage):
""" Sets volume relatively to current volume.
:param percentage: Percentage point volume increase.
"""
if self.volume + percentage < 0:
self.volume = 0
elif self.volume + percentage > 100:
self.volume = 100
else:
self.volume += percentage
self.mpd_client.setvol(self.volume)
def volume_mute_switch(self):
""" Switches volume muting on or off. """
if self.__muted:
self.mpd_client.setvol(self.volume)
self.__muted = False
else:
self.mpd_client.setvol(0)
self.__muted = True
def volume_mute_get(self):
return self.__muted
def random_switch(self):
""" Switches random playing on or off. """
self.random = not self.random
if self.random:
self.mpd_client.random(1)
else:
self.mpd_client.random(0)
def repeat_switch(self):
""" Switches repeat playing on or off. """
self.repeat = not self.repeat
if self.repeat:
self.mpd_client.repeat(1)
else:
self.mpd_client.repeat(0)
def single_switch(self):
self.single = not self.single
if self.consume:
self.mpd_client.single(1)
else:
self.mpd_client.single(0)
def consume_switch(self):
""" Switches playlist consuming on or off. """
self.consume = not self.consume
if self.consume:
self.mpd_client.consume(1)
else:
self.mpd_client.consume(0)
def playlist_current_get(self):
if not self.__radio_mode:
self.playlist_current = []
playlist_info = []
track_no = 0
playlist_info = self.mpd_client.playlistinfo()
for i in playlist_info:
track_no += 1
if 'title' in i:
self.playlist_current.append(str(track_no) + '. ' + i['title'])
else:
self.playlist_current.append(
str(track_no) + '. ' + os.path.splitext(os.path.basename(i['file']))[0])
return self.playlist_current
def playlist_current_playing_index_get(self):
"""
:return: The track number playing on the current playlist.
"""
if self.__radio_mode:
return -1
else:
self.status_get()
return self.__playlist_current_playing_index
def playlist_current_playing_index_set(self, index):
""" Starts playing item _index_ of the current playlist.
:param index: The track number to be played
:return: The current playing index
"""
if self.__radio_mode:
self.__radio_mode_set(False)
if index > 0 and index <= self.playlist_current_count():
self.mpd_client.playid(index)
self.__playlist_current_playing_index = index
return self.__playlist_current_playing_index
def playlist_current_count(self):
"""
:return: The number of items in the current playlist
"""
return len(self.playlist_current)
def playlist_current_clear(self):
""" Removes everything from the current playlist """
self.mpd_client.clear()
if not self.__radio_mode:
self.playlist_current = []
def library_update(self):
""" Updates the mpd library """
self.mpd_client.update()
def library_rescan(self):
""" Rebuild library. """
self.mpd_client.rescan()
def __search(self, tag_type):
""" Searches all entries of a certain type.
:param tag_type: ["artist"s, "album"s, song"title"s]
:return: A list with search results.
"""
self.list_query_results = self.mpd_client.list(tag_type)
self.list_query_results.sort()
return self.list_query_results
def __search_first_letter(self, tag_type, first_letter):
""" Searches all entries of a certain type matching a first letter
:param tag_type: ["artist"s, "album"s, song"title"s]
:param first_letter: The first letter
:return: A list with search results.
"""
temp_results = []
for i in self.list_query_results:
if i[:1].upper() == first_letter.upper():
temp_results.append(i)
self.list_query_results = temp_results
return self.list_query_results
def __search_partial(self, tag_type, part):
""" Searches all entries of a certain type partially matching search string.
:param tag_type: ["artist"s, "album"s, song"title"s]
:param part: Search string.
:return: A list with search results.
"""
all_results = []
all_results = self.mpd_client.list(tag_type)
self.list_query_results = []
all_results.sort()
for i in all_results:
result = i.upper()
if result.find(part.upper()) > -1:
self.list_query_results.append(i)
return self.list_query_results
def __search_of_type(self, type_result, type_filter, name_filter):
""" Searching one type depending on another type (very clear description isn't it?)
:param type_result: The type of result-set generated ["artist"s, "album"s, song"title"s]
:param type_filter: The type of filter used ["artist"s, "album"s, song"title"s]
:param name_filter: The name used to filter
:return:
"""
if self.searching_artist == "" and self.searching_album == "":
self.list_query_results = self.mpd_client.list(type_result, type_filter, name_filter)
elif self.searching_artist != "" and self.searching_album == "":
self.list_query_results = self.mpd_client.list(type_result, 'artist', self.searching_artist,
type_filter,
name_filter)
elif self.searching_artist == "" and self.searching_album != "":
self.list_query_results = self.mpd_client.list(type_result, 'album', self.searching_album, type_filter,
name_filter)
elif self.searching_artist != "" and self.searching_album != "":
self.list_query_results = self.mpd_client.list(type_result, 'artist', self.searching_artist, 'album',
self.searching_album, type_filter, name_filter)
self.list_query_results.sort()
return self.list_query_results
def artists_get(self, part=None, only_start=True):
""" Retrieves all artist names or matching by first letter(s) or partial search string.
:param part: Search string
:param only_start: Only search as first letter(s).
:return: A list of matching artist names.
"""
self.searching_artist = ""
self.searching_album = ""
if part is None:
if len(self.list_artists) == 0:
self.list_artists = self.__search('artist')
return self.list_artists
elif only_start:
self.list_query_results = self.__search_first_letter('artist', part)
else:
self.list_query_results = self.__search_partial('artist', part)
return self.list_query_results
def albums_get(self, part=None, only_start=True):
""" Retrieves all album titles or matching by first letter(s) or partial search string.
:param part: Search string.
:param only_start: Only search as first letter(s).
:return: A list of matching album titles.
"""
self.searching_artist = ""
self.searching_album = ""
if part is None:
if len(self.list_albums) == 0:
self.list_albums = self.__search('album')
return self.list_albums
elif only_start:
self.list_query_results = self.__search_first_letter('album', part)
else:
self.list_query_results = self.__search_partial('album', part)
return self.list_query_results
def songs_get(self, part=None, only_start=True):
""" Retrieves all song titles or matching by first letter(s) or partial search string
:param part: Search string
:param only_start: Only search as first letter(s)
:return: A list of matching song titles
"""
self.searching_artist = ""
self.searching_album = ""
if part is None:
if len(self.list_songs) == 0:
self.list_songs = self.__search('title')
return self.list_songs
elif only_start:
self.list_query_results = self.__search_first_letter('title', part)
else:
self.list_query_results = self.__search_partial('title', part)
return self.list_query_results
def artist_albums_get(self, artist_name):
""" Retrieves artist's albums.
:param artist_name: The name of the artist to retrieve the albums of.
:return: A list of album titles.
"""
self.searching_artist = artist_name
return self.__search_of_type('album', 'artist', artist_name)
def artist_songs_get(self, artist_name):
""" Retrieves artist's songs.
:param artist_name: The name of the artist to retrieve the songs of.
:return: A list of song titles
"""
self.searching_artist = artist_name
return self.__search_of_type('title', 'artist', artist_name)
def album_songs_get(self, album_name):
""" Retrieves all song titles of an album.
:param album_name: The name of the album
:return: A list of song titles
"""
self.searching_album = album_name
return self.__search_of_type('title', 'album', album_name)
def playlists_get(self, first_letter=None):
""" Retrieves all playlists or those matching the first letter
:param first_letter: Letter
:return: A list of playlist names
"""
result_list = []
all_playlists = []
all_playlists = self.mpd_client.listplaylists()
if first_letter is None:
for playlist in all_playlists:
result_list.append(playlist['playlist'])
else:
for playlist in all_playlists:
if playlist['playlist'][:1].upper() == first_letter.upper():
result_list.append(playlist['playlist'])
return result_list
def directory_list(self, path="", first_letter=None):
""" Retrieves the contents of a directory
:param path: Subpath
:param first_letter: Retrieve all directories starting with letter
"""
result_list = []
directory_list = []
path_entries = None
path_entries = self.mpd_client.lsinfo(path)
for entry in path_entries:
if 'directory' in entry:
directory_list.append(('directory', entry['directory']))
elif 'file' in entry:
directory_list.append(('file', entry['file']))
if first_letter is None:
result_list = directory_list
else:
for entry in directory_list:
if 'directory' in entry:
if entry['directory'][:1].upper() == first_letter.upper():
result_list.append(('directory', entry['directory']))
elif 'file' in entry:
if entry['file'][:1].upper() == first_letter.upper():
result_list.append(('file', os.path.basename(entry['file'])))
return result_list
def directory_songs_get(self, path=""):
""" Gets all files in the directory and from the directories below
:param path: Directory which is searched recursively for files
:return: list with files
"""
contents_list = self.__directory_recurse_get(path)
songs_list = []
for entry in contents_list:
if 'file' in entry:
songs_list.append(entry)
return songs_list
def __directory_recurse_get(self, path=""):
"""
:param path: Recurses through directories
:return:
"""
content_list = []
content_list = self.mpd_client.lsinfo(path)
for entry in content_list:
if 'directory' in entry:
content_list += self.__directory_recurse_get(entry['directory'])
# elif 'file' in entry:
# content_list.append(('file', os.path.basename(entry['file'])))
return content_list
def playlist_add(self, tag_type, tag_name, play=False, clear_playlist=False):
""" Adds songs to the current playlist
:param tag_type: Kind of add you want to do ["artist", "album", song"title"].
:param tag_name: The name of the tag_type.
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
if self.__radio_mode:
self.__radio_mode_set(False)
if clear_playlist:
self.playlist_current_clear()
i = self.playlist_current_count()
if self.searching_artist == "" and self.searching_album == "":
self.mpd_client.findadd(tag_type, tag_name)
elif self.searching_artist != "" and self.searching_album == "":
self.mpd_client.findadd('artist', self.searching_artist, tag_type, tag_name)
elif self.searching_artist == "" and self.searching_album != "":
self.mpd_client.findadd('album', self.searching_album, tag_type, tag_name)
elif self.searching_artist != "" and self.searching_album != "":
self.mpd_client.findadd('artist', self.searching_artist, 'album', self.searching_album, tag_type, tag_name)
if play:
self.play_playlist_item(i + 1)
def playlist_add_artist(self, artist_name, play=False, clear_playlist=False):
""" Adds all artist's songs to the current playlist
:param artist_name: The name of the artist.
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
self.playlist_add('artist', artist_name, play, clear_playlist)
def playlist_add_album(self, album_name, play=False, clear_playlist=False):
""" Adds all album's songs to the current playlist
:param album_name: The album name
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
self.playlist_add('album', album_name, play, clear_playlist)
def playlist_add_song(self, song_name, play=False, clear_playlist=False):
""" Adds a song to the current playlist
:param song_name: The song's name
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
self.playlist_add('title', song_name, play, clear_playlist)
def playlist_add_playlist(self, playlist_name, play=False, clear_playlist=False):
""" Adds a playlist to the current playlist
:param playlist_name: The playlist's name
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
if self.__radio_mode:
self.__radio_mode_set(False)
if clear_playlist:
self.playlist_current_clear()
i = self.playlist_current_count()
try:
self.mpd_client.load(playlist_name)
except mpd.ConnectionError:
self.mpd_client.connect(self.host, self.port)
self.mpd_client.load(playlist_name)
if play:
self.play_playlist_item(i + 1)
def playlist_add_file(self, uri, play=False, clear_playlist=False):
""" Adds file to the playlist
:param uri: The file including path
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
if self.__radio_mode:
self.__radio_mode_set(False)
if clear_playlist:
self.playlist_current_clear()
i = self.playlist_current_count()
self.mpd_client.addid(uri)
if play:
self.play_playlist_item(i + 1)
def playlist_add_directory(self, path, play=False, clear_playlist=False):
""" Adds all songs from the directory recursively to the playlist
:param path: Directory
:param play: Boolean indicating whether you want to start playing what was just added.
:param clear_playlist: Boolean indicating whether to remove all previous entries from the current playlist.
"""
if self.__radio_mode:
self.__radio_mode_set(False)
if clear_playlist:
self.playlist_current_clear()
i = self.playlist_current_count()
songs = self.directory_songs_get(path)
for song in songs:
self.mpd_client.addid(song['file'])
if play:
self.play_playlist_item(i + 1)
def radio_station_start(self, station_URL):
self.playlist_current_get()
self.__radio_mode = True
try:
self.mpd_client.rm(TEMP_PLAYLIST_NAME)
except Exception:
pass
self.mpd_client.save(TEMP_PLAYLIST_NAME)
self.playlist_current_clear()
self.mpd_client.addid(station_URL)
self.mpd_client.play(0)
def radio_mode_get(self):
return self.__radio_mode
def __radio_mode_set(self, radio_mode):
if self.__radio_mode == True and radio_mode == False:
try:
self.playlist_current_clear()
self.mpd_client.load(TEMP_PLAYLIST_NAME)
self.mpd_client.rm(TEMP_PLAYLIST_NAME)
except Exception:
pass
self.__radio_mode = radio_mode
mpd = MPDController()