Fixed not random song when providing only artist (spotify skill)

This commit is contained in:
Mathieu 2021-08-28 16:56:15 +02:00
parent 06a4658326
commit ee657624a5
3 changed files with 11 additions and 15 deletions

View File

@ -11,22 +11,22 @@ class SpotifySkill(Skill, metaclass=SkillRegistering):
def handle_play_a_song(self, data): def handle_play_a_song(self, data):
print(data) print(data)
song_lists_matching = spotify.query_song(data['song'] if 'song' in data else None, matching_song = spotify.query_song(data['song'] if 'song' in data else None,
data['artist'] if 'artist' in data else None) data['artist'] if 'artist' in data else None)
if song_lists_matching is not None and len(song_lists_matching) >= 1: if matching_song is not None and len(matching_song) >= 1:
# pause the music before speaking dialog # pause the music before speaking dialog
if spotify.is_music_playing(): if spotify.is_music_playing():
spotify.get_spotify().pause_playback() spotify.get_spotify().pause_playback()
if 'artist' in data and 'song' not in data: if 'artist' in data and 'song' not in data:
self.speak_dialog("play_from_artist", {'artist': song_lists_matching[0]['artists'][0]['name']}) self.speak_dialog("play_from_artist", {'artist': matching_song['artists'][0]['name']})
else: else:
self.speak_dialog("play_song_from_artist", {'song': song_lists_matching[0][ self.speak_dialog("play_song_from_artist", {'song': matching_song[
'name'], 'artist': song_lists_matching[0]['artists'][0]['name']}) 'name'], 'artist': matching_song['artists'][0]['name']})
spotify.get_spotify().add_to_queue(uri=song_lists_matching[0]['uri']) spotify.get_spotify().add_to_queue(uri=matching_song['uri'])
spotify.get_spotify().next_track() spotify.get_spotify().next_track()
else: else:
self.speak_dialog("nothing_found") self.speak_dialog("nothing_found")

View File

@ -42,19 +42,14 @@ def query_song(song=None, artist=None):
tracks = [(best_confidence(d['name'], 'None'), d) for d in data] tracks = [(best_confidence(d['name'], 'None'), d) for d in data]
tracks.sort(key=lambda x: x[0]) tracks.sort(key=lambda x: x[0])
tracks.reverse() # Place best matches first tracks.reverse() # Place best matches first
# Find pretty similar tracks to the best match # Find pretty similar tracks to the best match
tracks = [t for t in tracks if t[0] > tracks[0][0] - 0.1] # tracks = [t for t in tracks if t[0] > tracks[0][0] - 0.1]
# Sort remaining tracks by popularity # Sort remaining tracks by popularity
tracks.sort(key=lambda x: x[1]['popularity']) # tracks.sort(key=lambda x: x[1]['popularity'])
# print([(t[0], t[1]['name'], t[1]['artists'][0]['name']) for t in tracks]) # DEBUG
data = [tracks[-1][1]]
# return tracks[-1][0], {'data': data, 'name': None, 'type': 'track'} return random.choice(tracks)[1]
return data
def is_music_playing(): def is_music_playing():

View File

@ -23,4 +23,5 @@ Tu peux jouer le morceau {song}
Tu peux jouer le morceau {song} (de la chanteuse|du chanteur|de) {artist} Tu peux jouer le morceau {song} (de la chanteuse|du chanteur|de) {artist}
Joue moi voir {song} (sur spotify|) Joue moi voir {song} (sur spotify|)
Joue moi voir {song} de {artist} (sur spotify|) Joue moi voir {song} de {artist} (sur spotify|)
(Joue|Mets) (moi|) (un peu de|du) {artist} (sur spotify|) (Joue|Mets) (moi|) (un peu de|du) {artist} (sur spotify|)
(Joue|Mets) (moi|) un peu d'{artist} (sur spotify|)