-
Notifications
You must be signed in to change notification settings - Fork 38
/
__init__.py
1395 lines (1220 loc) · 53.3 KB
/
__init__.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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 Åke Forslund
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
INTRO:
Spotify is a little different than some music services. The APIs encourage
a sort of network of music players. So this skill will act as a remote
controller when another Spotify player is already running and it is invoked.
Otherwise it begins playing the music locally using the Mycroft-controlled
hardware. (Which, depending on the audio setup, might not be the main
speaker on the equipment.)
"""
import random
import re
import signal
import time
from enum import Enum
from os.path import abspath, dirname, join
from subprocess import call, Popen, DEVNULL
from socket import gethostname
import spotipy
from mycroft.skills.core import intent_handler
from mycroft.util.parse import match_one, fuzzy_match
from mycroft.api import DeviceApi
from mycroft.messagebus import Message
from adapt.intent import IntentBuilder
from requests import HTTPError
from .exceptions import (NoSpotifyDevicesError,
PlaylistNotFoundError,
SpotifyNotAuthorizedError)
from .spotify import (MycroftSpotifyCredentials, SpotifyConnect,
get_album_info, get_artist_info, get_song_info,
get_show_info, load_local_credentials)
from mycroft.skills.common_play_skill import CommonPlaySkill, CPSMatchLevel
class DeviceType(Enum):
MYCROFT = 1
DEFAULT = 2
DESKTOP = 3
FIRSTBEST = 4
NOTFOUND = 5
# Platforms for which the skill should start the spotify player
MANAGED_PLATFORMS = ['mycroft_mark_1', 'mycroft_mark_2pi']
# Return value definition indication nothing was found
# (confidence None, data None)
NOTHING_FOUND = (None, 0.0)
# Confidence levels for generic play handling
DIRECT_RESPONSE_CONFIDENCE = 0.8
MATCH_CONFIDENCE = 0.5
def best_result(results):
"""Return best result from a list of result tuples.
Arguments:
results (list): list of spotify result tuples
Returns:
Best match in list
"""
if len(results) == 0:
return NOTHING_FOUND
else:
results.reverse()
return sorted(results, key=lambda x: x[0])[-1]
def best_confidence(title, query):
"""Find best match for a title against a query.
Some titles include ( Remastered 2016 ) and similar info. This method
will test the raw title and a version that has been parsed to remove
such information.
Arguments:
title: title name from spotify search
query: query from user
Returns:
(float) best condidence
"""
best = title.lower()
best_stripped = re.sub(r'(\(.+\)|-.+)$', '', best).strip()
return max(fuzzy_match(best, query),
fuzzy_match(best_stripped, query))
def update_librespot():
try:
call(["bash", join(dirname(abspath(__file__)), "requirements.sh")])
except Exception as e:
print('Librespot Update failed, {}'.format(repr(e)))
def status_info(status):
"""Return track, artist, album tuple from spotify status.
Arguments:
status (dict): Spotify status info
Returns:
tuple (track, artist, album)
"""
try:
artist = status['item']['artists'][0]['name']
except Exception:
artist = 'unknown'
try:
track = status['item']['name']
except Exception:
track = 'unknown'
try:
album = status['item']['album']['name']
except Exception:
album = 'unknown'
return track, artist, album
class SpotifySkill(CommonPlaySkill):
"""Spotify control through the Spotify Connect API."""
def __init__(self):
super(SpotifySkill, self).__init__()
self.index = 0
self.spotify = None
self.process = None
self.device_name = None
self.dev_id = None
self.idle_count = 0
self.ducking = False
self.is_player_remote = False # when dev is remote control instance
self.mouth_text = None
self.librespot_starting = False
self.librespot_failed = False
self.__device_list = None
self.__devices_fetched = 0
self.OAUTH_ID = 1
enclosure_config = self.config_core.get('enclosure')
self.platform = enclosure_config.get('platform', 'unknown')
self.DEFAULT_VOLUME = 80 if self.platform == 'mycroft_mark_1' else 100
self._playlists = None
self.saved_tracks = None
self.regexes = {}
self.last_played_type = None # The last uri type that was started
self.is_playing = False
self.__saved_tracks_fetched = 0
self.allow_master_control = self.settings.get('allow_master_control')
def translate_regex(self, regex):
if regex not in self.regexes:
path = self.find_resource(regex + '.regex')
if path:
with open(path) as f:
string = f.read().strip()
self.regexes[regex] = string
return self.regexes[regex]
def launch_librespot(self):
"""Launch the librespot binary for the Mark-1."""
self.librespot_starting = True
path = self.settings.get('librespot_path', None)
if self.platform in MANAGED_PLATFORMS and not path:
path = 'librespot'
if (path and self.device_name and
'user' in self.settings and 'password' in self.settings):
# Disable librespot logging if not specifically requested
log_level = self.config_core.get('log_level', '')
if 'librespot_log' in self.settings or log_level == 'DEBUG':
outs = None
else:
outs = DEVNULL
# TODO: Error message when provided username/password don't work
self.process = Popen([path, '-n', self.device_name,
'-u', self.settings['user'],
'-p', self.settings['password']],
stdout=outs, stderr=outs)
time.sleep(3) # give libreSpot time to start-up
if self.process and self.process.poll() is not None:
self.log.error('librespot failed to start.')
# libreSpot shut down immediately. Bad user/password?
if self.settings.get('user'):
self.librespot_failed = True
self.process = None
self.librespot_starting = False
return
# Lower the volume since max volume sounds terrible on the Mark-1
dev = self.device_by_name(self.device_name)
if dev:
self.spotify.volume(dev['id'], self.DEFAULT_VOLUME)
self.librespot_starting = False
def initialize(self):
# Make sure the spotify login scheduled event is shutdown
super().initialize()
self.cancel_scheduled_event('SpotifyLogin')
# Setup handlers for playback control messages
self.add_event('mycroft.audio.service.next', self.next_track)
self.add_event('mycroft.audio.service.prev', self.prev_track)
self.add_event('mycroft.audio.service.pause', self.pause)
self.add_event('mycroft.audio.service.resume', self.resume)
# Check and then monitor for credential changes
self.settings_change_callback = self.on_websettings_changed
# Retry in 5 minutes
self.schedule_repeating_event(self.on_websettings_changed,
None, 5 * 60, name='SpotifyLogin')
if self.platform in MANAGED_PLATFORMS:
update_librespot()
self.on_websettings_changed()
def on_websettings_changed(self):
if not self.spotify:
try:
self.load_credentials()
except Exception as e:
self.log.debug('Credentials could not be fetched. '
'({})'.format(repr(e)))
if self.spotify:
self.cancel_scheduled_event('SpotifyLogin')
if 'user' in self.settings and 'password' in self.settings:
if self.process:
self.stop_librespot()
self.launch_librespot()
# Refresh saved tracks
# We can't get this list when the user asks because it takes
# too long and causes
# mycroft-playback-control.mycroftai:PlayQueryTimeout
self.refresh_saved_tracks()
def load_local_creds(self):
try:
creds = load_local_credentials(self.settings['user'])
spotify = SpotifyConnect(client_credentials_manager=creds)
except Exception:
self.log.exception('Couldn\'t fetch credentials')
spotify = None
return spotify
def load_remote_creds(self):
try:
creds = MycroftSpotifyCredentials(self.OAUTH_ID)
spotify = SpotifyConnect(client_credentials_manager=creds)
except HTTPError:
self.log.info('Couldn\'t fetch credentials')
spotify = None
return spotify
def load_credentials(self):
"""Retrieve credentials and connect to spotify.
This will load local credentials if available otherwise fetching
remote settings from mycroft backend will be attempted.
NOTE: the remote fetching is only a preparation for the future and
will always fail at the moment.
"""
self.spotify = self.load_local_creds() or self.load_remote_creds()
if self.spotify:
# Spotfy connection worked, prepare for usage
# TODO: Repeat occasionally on failures?
# If not able to authorize, the method will be repeated after 60
# seconds
self.create_intents()
# Should be safe to set device_name here since home has already
# been connected
self.device_name = DeviceApi().get().get('name')
def failed_auth(self):
if 'user' not in self.settings:
self.log.error('Settings hasn\'t been received yet')
self.speak_dialog('NoSettingsReceived')
elif not self.settings.get("user"):
self.log.error('User info has not been set.')
# Assume this is initial setup
self.speak_dialog('NotConfigured')
else:
# Assume password changed or there is a typo
self.log.error('User info has been set but Auth failed.')
self.speak_dialog('NotAuthorized')
######################################################################
# Handle auto ducking when listener is started.
def handle_listener_started(self, message):
"""Handle auto ducking when listener is started.
The ducking is enabled/disabled using the skill settings on home.
TODO: Evaluate the Idle check logic
"""
if (self.spotify.is_playing() and self.is_player_remote and
self.settings.get('use_ducking', False)):
self.__pause()
self.ducking = True
# Start idle check
self.idle_count = 0
self.cancel_scheduled_event('IdleCheck')
self.schedule_repeating_event(self.check_for_idle, None,
1, name='IdleCheck')
def check_for_idle(self):
"""Repeating event checking for end of auto ducking."""
if not self.ducking:
self.cancel_scheduled_event('IdleCheck')
return
active = self.enclosure.display_manager.get_active()
if not active == '' or active == 'SpotifySkill':
# No activity, start to fall asleep
self.idle_count += 1
if self.idle_count >= 5:
# Resume playback after 5 seconds of being idle
self.cancel_scheduled_event('IdleCheck')
self.ducking = False
self.resume()
else:
self.idle_count = 0
######################################################################
# Mycroft display handling
def start_monitor(self):
"""Monitoring and current song display."""
# Clear any existing event
self.stop_monitor()
# Schedule a new one every 5 seconds to monitor/update display
self.schedule_repeating_event(self._update_display,
None, 5,
name='MonitorSpotify')
self.add_event('recognizer_loop:record_begin',
self.handle_listener_started)
def stop_monitor(self):
# Clear any existing event
self.cancel_scheduled_event('MonitorSpotify')
def _update_display(self, message):
# Checks once a second for feedback
status = self.spotify.status() if self.spotify else {}
self.is_playing = self.spotify.is_playing()
if not status or not status.get('is_playing'):
self.stop_monitor()
self.mouth_text = None
self.enclosure.mouth_reset()
if not self.allow_master_control:
self.disable_playing_intents()
return
# Get the current track info
try:
artist = status['item']['artists'][0]['name']
except Exception:
artist = ''
try:
track = status['item']['name']
except Exception:
track = ''
try:
image = status['item']['album']['images'][0]['url']
except Exception:
image = ''
self.CPS_send_status(artist=artist, track=track, image=image)
# Mark-1
if artist and track:
text = '{}: {}'.format(artist, track)
else:
text = ''
# Update the "Now Playing" display if needed
if text != self.mouth_text:
self.mouth_text = text
self.enclosure.mouth_text(text)
def CPS_match_query_phrase(self, phrase):
"""Handler for common play framework Query."""
# Not ready to play
if not self.playback_prerequisits_ok():
self.log.debug('Spotify is not available to play')
if 'spotify' in phrase:
return phrase, CPSMatchLevel.GENERIC
else:
return None
spotify_specified = 'spotify' in phrase
bonus = 0.1 if spotify_specified else 0.0
phrase = re.sub(self.translate_regex('on_spotify'), '', phrase,
re.IGNORECASE)
confidence, data = self.continue_playback(phrase, bonus)
if not data:
confidence, data = self.specific_query(phrase, bonus)
if not data:
confidence, data = self.generic_query(phrase, bonus)
if data:
self.log.info('Spotify confidence: {}'.format(confidence))
self.log.info(' data: {}'.format(data))
if data.get('type') in ['saved_tracks', 'album', 'artist',
'track', 'playlist', 'show']:
if spotify_specified:
# " play great song on spotify'
level = CPSMatchLevel.EXACT
else:
if confidence > 0.9:
# TODO: After 19.02 scoring change
# level = CPSMatchLevel.MULTI_KEY
level = CPSMatchLevel.TITLE
elif confidence < 0.5:
level = CPSMatchLevel.GENERIC
else:
level = CPSMatchLevel.TITLE
phrase += ' on spotify'
elif data.get('type') == 'continue':
if spotify_specified > 0:
# "resume playback on spotify"
level = CPSMatchLevel.EXACT
else:
# "resume playback"
level = CPSMatchLevel.GENERIC
phrase += ' on spotify'
else:
self.log.warning('Unexpected spotify type: '
'{}'.format(data.get('type')))
level = CPSMatchLevel.GENERIC
return phrase, level, data
else:
self.log.debug('Couldn\'t find anything to play on Spotify')
def continue_playback(self, phrase, bonus):
if phrase.strip() == 'spotify':
return (1.0,
{
'data': None,
'name': None,
'type': 'continue'
})
else:
return NOTHING_FOUND
def specific_query(self, phrase, bonus):
"""
Check if the phrase can be matched against a specific spotify request.
This includes asking for saved items, playlists, albums, podcasts,
artists or songs.
Arguments:
phrase (str): Text to match against
bonus (float): Any existing match bonus
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
# Check if saved
match = re.match(self.translate_regex('saved_songs'), phrase,
re.IGNORECASE)
if match and self.saved_tracks:
return (1.0, {'data': None,
'type': 'saved_tracks'})
# Check if playlist
match = re.match(self.translate_regex('playlist'), phrase,
re.IGNORECASE)
if match:
return self.query_playlist(match.groupdict()['playlist'])
# Check album
match = re.match(self.translate_regex('album'), phrase,
re.IGNORECASE)
if match:
bonus += 0.1
album = match.groupdict()['album']
return self.query_album(album, bonus)
# Check artist
match = re.match(self.translate_regex('artist'), phrase,
re.IGNORECASE)
if match:
artist = match.groupdict()['artist']
return self.query_artist(artist, bonus)
match = re.match(self.translate_regex('song'), phrase,
re.IGNORECASE)
if match:
song = match.groupdict()['track']
return self.query_song(song, bonus)
# Check if podcast
match = re.match(self.translate_regex('podcast'), phrase,
re.IGNORECASE)
if match:
return self.query_show(match.groupdict()['podcast'])
return NOTHING_FOUND
def generic_query(self, phrase, bonus):
"""Check for a generic query, not asking for any special feature.
This will try to parse the entire phrase in the following order
- As a user playlist
- As an album
- As a track
- As a public playlist
Arguments:
phrase (str): Text to match against
bonus (float): Any existing match bonus
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
self.log.info('Handling "{}" as a genric query...'.format(phrase))
results = []
self.log.info('Checking users playlists')
playlist, conf = self.get_best_user_playlist(phrase)
if playlist:
uri = self.playlists[playlist]
data = {
'data': uri,
'name': playlist,
'type': 'playlist'
}
if conf and conf > DIRECT_RESPONSE_CONFIDENCE:
return (conf, data)
elif conf and conf > MATCH_CONFIDENCE:
results.append((conf, data))
# Check for artist
self.log.info('Checking artists')
conf, data = self.query_artist(phrase, bonus)
if conf and conf > DIRECT_RESPONSE_CONFIDENCE:
return conf, data
elif conf and conf > MATCH_CONFIDENCE:
results.append((conf, data))
# Check for track
self.log.info('Checking tracks')
conf, data = self.query_song(phrase, bonus)
if conf and conf > DIRECT_RESPONSE_CONFIDENCE:
return conf, data
elif conf and conf > MATCH_CONFIDENCE:
results.append((conf, data))
# Check for album
self.log.info('Checking albums')
conf, data = self.query_album(phrase, bonus)
if conf and conf > DIRECT_RESPONSE_CONFIDENCE:
return conf, data
elif conf and conf > MATCH_CONFIDENCE:
results.append((conf, data))
# Check for public playlist
self.log.info('Checking tracks')
conf, data = self.get_best_public_playlist(phrase)
if conf and conf > DIRECT_RESPONSE_CONFIDENCE:
return conf, data
elif conf and conf > MATCH_CONFIDENCE:
results.append((conf, data))
return best_result(results)
def query_artist(self, artist, bonus=0.0):
"""Try to find an artist.
Arguments:
artist (str): Artist to search for
bonus (float): Any bonus to apply to the confidence
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
bonus += 0.1
data = self.spotify.search(artist, type='artist')
if data and data['artists']['items']:
best = data['artists']['items'][0]['name']
confidence = fuzzy_match(best, artist.lower()) + bonus
confidence = min(confidence, 1.0)
return (confidence,
{
'data': data,
'name': None,
'type': 'artist'
})
else:
return NOTHING_FOUND
def query_album(self, album, bonus):
"""Try to find an album.
Searches Spotify by album and artist if available.
Arguments:
album (str): Album to search for
bonus (float): Any bonus to apply to the confidence
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
data = None
by_word = ' {} '.format(self.translate('by'))
if len(album.split(by_word)) > 1:
album, artist = album.split(by_word)
album_search = '*{}* artist:{}'.format(album, artist)
bonus += 0.1
else:
album_search = album
data = self.spotify.search(album_search, type='album')
if data and data['albums']['items']:
best = data['albums']['items'][0]['name'].lower()
confidence = best_confidence(best, album)
# Also check with parentheses removed for example
# "'Hello Nasty ( Deluxe Version/Remastered 2009" as "Hello Nasty")
confidence = min(confidence + bonus, 1.0)
self.log.info((album, best, confidence))
return (confidence,
{
'data': data,
'name': None,
'type': 'album'
})
return NOTHING_FOUND
def query_playlist(self, playlist):
"""Try to find a playlist.
First searches the users playlists, then tries to find a public
one.
Arguments:
playlist (str): Playlist to search for
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
result, conf = self.get_best_user_playlist(playlist)
if playlist and conf > 0.5:
uri = self.playlists[result]
return (conf, {'data': uri,
'name': playlist,
'type': 'playlist'})
else:
return self.get_best_public_playlist(playlist)
def query_show(self, podcast):
"""Try to find a podcast.
First searches the users playlists, then tries to find a public
one.
Arguments:
podcast (str): Playlist to search for
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
data = self.spotify.search(podcast, type='show')
if data and data['shows']['items']:
best = data['shows']['items'][0]['name'].lower()
confidence = best_confidence(best, podcast)
return (confidence, {'data': data, 'type': 'show'})
def query_song(self, song, bonus):
"""Try to find a song.
Searches Spotify for song and artist if provided.
Arguments:
song (str): Song to search for
bonus (float): Any bonus to apply to the confidence
Returns: Tuple with confidence and data or NOTHING_FOUND
"""
data = None
by_word = ' {} '.format(self.translate('by'))
if len(song.split(by_word)) > 1:
song, artist = song.split(by_word)
song_search = '*{}* artist:{}'.format(song, artist)
else:
song_search = song
data = self.spotify.search(song_search, type='track')
if data and len(data['tracks']['items']) > 0:
tracks = [(best_confidence(d['name'], song), d)
for d in data['tracks']['items']]
tracks.sort(key=lambda x: x[0])
tracks.reverse() # Place best matches first
# Find pretty similar tracks to the best match
tracks = [t for t in tracks if t[0] > tracks[0][0] - 0.1]
# Sort remaining tracks by popularity
tracks.sort(key=lambda x: x[1]['popularity'])
self.log.debug([(t[0], t[1]['name'], t[1]['artists'][0]['name'])
for t in tracks])
data['tracks']['items'] = [tracks[-1][1]]
return (tracks[-1][0] + bonus,
{'data': data, 'name': None, 'type': 'track'})
else:
return NOTHING_FOUND
def CPS_start(self, phrase, data):
"""Handler for common play framework start playback request."""
try:
if not self.spotify:
raise SpotifyNotAuthorizedError
# Wait for librespot to start
if self.librespot_starting:
self.log.info('Restarting Librespot...')
for i in range(10):
time.sleep(0.5)
if not self.librespot_starting:
break
else:
self.log.error('LIBRESPOT NOT STARTED')
dev = self.get_default_device()
if not dev:
raise NoSpotifyDevicesError
if data['type'] == 'continue':
self.acknowledge()
self.continue_current_playlist(dev)
elif data['type'] == 'playlist':
self.start_playlist_playback(dev, data['name'],
data['data'])
else: # artist, album track
self.log.info('playing {}'.format(data['type']))
self.play(dev, data=data['data'], data_type=data['type'])
self.enable_playing_intents()
if data.get('type') and data['type'] != 'continue':
self.last_played_type = data['type']
self.is_playing = True
except NoSpotifyDevicesError:
if self.librespot_failed:
self.speak_dialog('FailedToStart')
self.librespot_failed = False
else:
self.log.error("Unable to get a default device while trying "
"to play something.")
self.speak_dialog(
'PlaybackFailed',
{'reason': self.translate('NoDevicesAvailable')})
except SpotifyNotAuthorizedError:
self.failed_auth()
except PlaylistNotFoundError:
self.speak_dialog('PlaybackFailed',
{'reason': self.translate('PlaylistNotFound')})
except Exception as e:
self.log.exception(str(e))
self.speak_dialog('PlaybackFailed', {'reason': str(e)})
def create_intents(self):
"""Setup the spotify intents."""
intent = IntentBuilder('').require('Spotify').require('Search') \
.require('For')
self.register_intent(intent, self.search_spotify)
self.register_intent_file('ShuffleOn.intent', self.shuffle_on)
self.register_intent_file('ShuffleOff.intent', self.shuffle_off)
self.register_intent_file('WhatSong.intent', self.song_info)
self.register_intent_file('WhatAlbum.intent', self.album_info)
self.register_intent_file('WhatArtist.intent', self.artist_info)
self.register_intent_file('StopMusic.intent', self.handle_stop)
time.sleep(0.5)
if not self.allow_master_control:
self.disable_playing_intents()
def enable_playing_intents(self):
self.enable_intent('WhatSong.intent')
self.enable_intent('WhatAlbum.intent')
self.enable_intent('WhatArtist.intent')
self.enable_intent('StopMusic.intent')
def disable_playing_intents(self):
self.disable_intent('WhatSong.intent')
self.disable_intent('WhatAlbum.intent')
self.disable_intent('WhatArtist.intent')
self.disable_intent('StopMusic.intent')
@property
def playlists(self):
"""Playlists, cached for 5 minutes."""
if not self.spotify:
return [] # No connection, no playlists
now = time.time()
if not self._playlists or (now - self.__playlists_fetched > 5 * 60):
self._playlists = {}
playlists = self.spotify.current_user_playlists().get('items', [])
for p in playlists:
self._playlists[p['name'].lower()] = p
self.__playlists_fetched = now
return self._playlists
def refresh_saved_tracks(self):
"""Saved tracks are cached for 4 hours."""
if not self.spotify:
return []
now = time.time()
if (not self.saved_tracks or
(now - self.__saved_tracks_fetched > 4 * 60 * 60)):
saved_tracks = []
offset = 0
while True:
batch = self.spotify.current_user_saved_tracks(50, offset)
for item in batch.get('items', []):
saved_tracks.append(item['track'])
offset += 50
if not batch['next']:
break
self.saved_tracks = saved_tracks
self.__saved_tracks_fetched = now
@property
def devices(self):
"""Devices, cached for 60 seconds."""
if not self.spotify:
return [] # No connection, no devices
now = time.time()
if not self.__device_list or (now - self.__devices_fetched > 60):
self.__device_list = self.spotify.get_devices()
self.__devices_fetched = now
return self.__device_list
def device_by_name(self, name):
"""Get a Spotify devices from the API.
Arguments:
name (str): The device name (fuzzy matches)
Returns:
(dict) None or the matching device's description
"""
devices = self.devices
if devices and len(devices) > 0:
# Otherwise get a device with the selected name
devices_by_name = {d['name'].lower(): d for d in devices}
key, confidence = match_one(name, list(devices_by_name.keys()))
if confidence > 0.5:
return devices_by_name[key]
return None
def get_default_device(self):
"""Get preferred playback device."""
if self.spotify:
# If user has set config allowing skill to control other devices,
# first check if any devices are currently playing or recently have
# been playing (current_playback() will return info about the most
# recently played device for about 10 minutes after playback is
# paused, after which it will return None)
# If a device has been playing recently, use it as the default
# Otherwise continue with 'normal' procedure for choosing a default
if self.allow_master_control:
current_playback = self.spotify.current_playback()
if current_playback:
device_name = current_playback['device']['name']
device_id = current_playback['device']['id']
self.log.debug(f'using device {device_name} as default, '
f'device id: {device_id}')
return current_playback['device']
# When there is an active Spotify device somewhere, use it
if (self.devices and len(self.devices) > 0 and
self.spotify.is_playing()):
for dev in self.devices:
if dev['is_active']:
self.log.info('Playing on an active device '
'[{}]'.format(dev['name']))
return dev # Use this device
# No playing device found, use the default Spotify device
default_device = self.settings.get('default_device', '')
dev = None
device_type = DeviceType.NOTFOUND
if default_device:
dev = self.device_by_name(default_device)
self.is_player_remote = True
device_type = DeviceType.DEFAULT
# if not set or missing try playing on this device
if not dev:
dev = self.device_by_name(self.device_name or '')
self.is_player_remote = False
device_type = DeviceType.MYCROFT
# if not check if a desktop spotify client is playing
if not dev:
dev = self.device_by_name(gethostname())
self.is_player_remote = False
device_type = DeviceType.DESKTOP
# use first best device if none of the prioritized works
if not dev and len(self.devices) > 0:
dev = self.devices[0]
self.is_player_remote = True # ?? Guessing it is remote
device_type = DeviceType.FIRSTBEST
if dev and not dev['is_active']:
self.spotify.transfer_playback(dev['id'], False)
self.log.info('Device detected: {}'.format(device_type))
return dev
return None
def get_best_user_playlist(self, playlist):
"""Get best playlist matching the provided name
Arguments:
playlist (str): Playlist name
Returns: ((str)best match, (float)confidence)
"""
playlists = list(self.playlists.keys())
if len(playlists) > 0:
# Only check if the user has playlists
key, confidence = match_one(playlist.lower(), playlists)
if confidence > 0.7:
return key, confidence
return NOTHING_FOUND
def get_best_public_playlist(self, playlist):
data = self.spotify.search(playlist, type='playlist')
if data and data['playlists']['items']:
best = data['playlists']['items'][0]
confidence = fuzzy_match(best['name'].lower(), playlist)
if confidence > 0.7:
return (confidence, {'data': best,
'name': best['name'],
'type': 'playlist'})
return NOTHING_FOUND
def continue_current_playlist(self, dev):
"""Send the play command to the selected device."""
time.sleep(2)
self.spotify_play(dev['id'])
def playback_prerequisits_ok(self):
"""Check that playback is possible, launch client if neccessary."""
if self.spotify is None:
return False
devs = [d['name'] for d in self.devices]
if self.process and self.device_name not in devs:
self.log.info('Librespot not responding, restarting...')
self.stop_librespot()
self.__devices_fetched = 0 # Make sure devices are fetched again
if not self.process:
self.schedule_event(self.launch_librespot, 0,
name='launch_librespot')
return True
def spotify_play(self, dev_id, uris=None, context_uri=None):
"""Start spotify playback and log any exceptions."""
try:
self.log.info(u'spotify_play: {}'.format(dev_id))
self.spotify.play(dev_id, uris, context_uri)
self.start_monitor()
self.dev_id = dev_id
except spotipy.SpotifyException as e:
# TODO: Catch other conditions?
if e.http_status == 403:
self.log.error('Play command returned 403, play is likely '
'already in progress. \n {}'.format(repr(e)))
else:
raise SpotifyNotAuthorizedError from e
except Exception as e:
self.log.exception(e)
raise
def start_playlist_playback(self, dev, name, uri):
name = name.replace('|', ':')
if uri:
self.log.info(u'playing {} using {}'.format(name, dev['name']))