From d1ddb1664c306fa08ac2390fec8df8aad3311269 Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Fri, 30 Jul 2021 14:10:35 +0200 Subject: [PATCH] Improved spotify intent and added result (json) response in RESTAPI --- jarvis/main.py | 6 ++--- .../skills/entertainement/spotify/__init__.py | 27 ++++++++++++------- .../skills/entertainement/spotify/spotify.py | 16 ++++++++++- .../spotify/vocab/fr-fr/From.voc | 3 --- .../spotify/vocab/fr-fr/Play.voc | 6 ----- .../spotify/vocab/fr-fr/Spotify.voc | 3 --- .../spotify/vocab/fr-fr/current_song.intent | 3 ++- .../spotify/vocab/fr-fr/play_a_song.intent | 10 ++++++- jarvis/skills/intent_manager.py | 11 ++++++-- 9 files changed, 55 insertions(+), 30 deletions(-) delete mode 100644 jarvis/skills/entertainement/spotify/vocab/fr-fr/From.voc delete mode 100644 jarvis/skills/entertainement/spotify/vocab/fr-fr/Play.voc delete mode 100644 jarvis/skills/entertainement/spotify/vocab/fr-fr/Spotify.voc diff --git a/jarvis/main.py b/jarvis/main.py index d091282..db3fbda 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -7,7 +7,7 @@ from jarvis.skills.entertainement.jokes import JokesSkill from jarvis.skills.entertainement.spotify import SpotifySkill from jarvis.skills.research.wikipedia import WikipediaSkill from jarvis.utils import languages_utils -from utils import config_utils, flask_utils, utils +from utils import config_utils, flask_utils app = Flask(__name__) @@ -19,7 +19,7 @@ def process_request(): if 'sentence' not in data or not data['sentence']: flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!')) - return {} + return jsonify(intent_manager.recognise(sentence=data['sentence'])) if __name__ == '__main__': @@ -32,7 +32,7 @@ if __name__ == '__main__': JokesSkill().register() SpotifySkill().register() - intent_manager.process_handlers() + intent_manager.load_all_skills() intent_manager.recognise("cherche sur wikipédia Elon Musk") # TO CHECK intent_manager.recognise("c'est qui Elon Musk") # TO CHECK diff --git a/jarvis/skills/entertainement/spotify/__init__.py b/jarvis/skills/entertainement/spotify/__init__.py index 049c22b..bbcbea6 100644 --- a/jarvis/skills/entertainement/spotify/__init__.py +++ b/jarvis/skills/entertainement/spotify/__init__.py @@ -9,19 +9,26 @@ class SpotifySkill(Skill, metaclass=SkillRegistering): @intent_file_handler("play_a_song.intent", "PlaySongWithSpotifyIntent") def handle_play_a_song(self, data): + print(data) + + song_lists_matching = None + if 'song' in data: song_lists_matching = spotify.query_song(data['song']) - if 'singer' in data: - song_lists_matching = spotify.query_song(data['song'], data['singer']) + if 'singer' in data: + song_lists_matching = spotify.query_song(None, data['singer']) + if 'song' in data and 'singer' in data: + song_lists_matching = spotify.query_song(data['song'], data['singer']) - if song_lists_matching is not None and len(song_lists_matching) >= 1: - print( - "[INFO INTENT] - Now playing : " + song_lists_matching[0]['uri'] + " / " + song_lists_matching[0][ - 'name'] + " / " + - song_lists_matching[0]['artists'][0]['name']) - spotify.get_spotify().add_to_queue(uri=song_lists_matching[0]['uri']) - spotify.get_spotify().next_track() - # spotify.get_spotify().start_playback(context_uri=song_lists_matching[0]['uri']) + if song_lists_matching is not None and len(song_lists_matching) >= 1: + print( + "[INFO INTENT] - Now playing : " + song_lists_matching[0]['uri'] + " / " + song_lists_matching[0][ + 'name'] + " / " + + song_lists_matching[0]['artists'][0]['name']) + + spotify.get_spotify().add_to_queue(uri=song_lists_matching[0]['uri']) + spotify.get_spotify().next_track() + # spotify.get_spotify().start_playback(context_uri=song_lists_matching[0]['uri']) @intent_file_handler("pause_music.intent", "PauseSpotifyIntent") def pause_music(self, data): diff --git a/jarvis/skills/entertainement/spotify/spotify.py b/jarvis/skills/entertainement/spotify/spotify.py index 7f0f42a..4949756 100644 --- a/jarvis/skills/entertainement/spotify/spotify.py +++ b/jarvis/skills/entertainement/spotify/spotify.py @@ -12,6 +12,8 @@ sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_secret=config_utils.get_in_config("SPOTIFY_CLIENT_SECRET"), redirect_uri='http://localhost:8888/callback/', open_browser=False)) + + # TODO: Investigate the open_browser and automatic auth renewing without user interaction @@ -19,9 +21,12 @@ def get_spotify(): return sp -def query_song(song, artist=None): +def query_song(song=None, artist=None): + print(str(song) + " / " + str(artist)) if song is not None and artist is not None: song_search = '*{}* artist:{}'.format(song, artist) + elif song is None and artist is not None: + return query_artist(artist) else: song_search = song @@ -45,6 +50,15 @@ def query_song(song, artist=None): return data +def query_artist(artist): + tracks = get_spotify().search(q=("artist:" + artist), limit=10, type='track')['tracks']['items'] + if len(tracks) > 0: + tracks.reverse() # Place best matches first + + return tracks + return None + + def best_confidence(title, query): """Find best match for a title against a query. Some titles include ( Remastered 2016 ) and similar info. This method diff --git a/jarvis/skills/entertainement/spotify/vocab/fr-fr/From.voc b/jarvis/skills/entertainement/spotify/vocab/fr-fr/From.voc deleted file mode 100644 index 0a9c8be..0000000 --- a/jarvis/skills/entertainement/spotify/vocab/fr-fr/From.voc +++ /dev/null @@ -1,3 +0,0 @@ -de -du groupe -d' \ No newline at end of file diff --git a/jarvis/skills/entertainement/spotify/vocab/fr-fr/Play.voc b/jarvis/skills/entertainement/spotify/vocab/fr-fr/Play.voc deleted file mode 100644 index 089ec9b..0000000 --- a/jarvis/skills/entertainement/spotify/vocab/fr-fr/Play.voc +++ /dev/null @@ -1,6 +0,0 @@ -joue -mets -mets -fait moi écouter -écouter -joue le morceau \ No newline at end of file diff --git a/jarvis/skills/entertainement/spotify/vocab/fr-fr/Spotify.voc b/jarvis/skills/entertainement/spotify/vocab/fr-fr/Spotify.voc deleted file mode 100644 index 03457d3..0000000 --- a/jarvis/skills/entertainement/spotify/vocab/fr-fr/Spotify.voc +++ /dev/null @@ -1,3 +0,0 @@ -Spotify -spotify -spot \ No newline at end of file diff --git a/jarvis/skills/entertainement/spotify/vocab/fr-fr/current_song.intent b/jarvis/skills/entertainement/spotify/vocab/fr-fr/current_song.intent index 800d98d..936a18a 100644 --- a/jarvis/skills/entertainement/spotify/vocab/fr-fr/current_song.intent +++ b/jarvis/skills/entertainement/spotify/vocab/fr-fr/current_song.intent @@ -2,4 +2,5 @@ C'est quoi le nom du morceau actuel On écoute quoi la C'est quoi le titre de la chanson actuelle C'est quoi cette chanson -C'est quoi cette musique \ No newline at end of file +C'est quoi cette musique +c'est quoi la chanson actuelle \ No newline at end of file diff --git a/jarvis/skills/entertainement/spotify/vocab/fr-fr/play_a_song.intent b/jarvis/skills/entertainement/spotify/vocab/fr-fr/play_a_song.intent index a5c704e..adbaa0b 100644 --- a/jarvis/skills/entertainement/spotify/vocab/fr-fr/play_a_song.intent +++ b/jarvis/skills/entertainement/spotify/vocab/fr-fr/play_a_song.intent @@ -1,5 +1,6 @@ Joue {song} sur spotify Joue {song} +Joue la chanson {song} Joue {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify @@ -22,4 +23,11 @@ Mets voir {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spo Fait moi écouter {song} Fait moi écouter {song} sur spotify Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} -Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify \ No newline at end of file +Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify +Joue quelque chose de {singer} +Mets de la musique du chanteur {singer} +Joue quelque chose du chanteur {singer} +Joue quelque chose de la chanteuse {singer} +Mets quelque chose de {singer} +Mets quelque chose de la chanteuse {singer} +Mets quelque chose du chanteur {singer} \ No newline at end of file diff --git a/jarvis/skills/intent_manager.py b/jarvis/skills/intent_manager.py index c339a7c..0d58566 100644 --- a/jarvis/skills/intent_manager.py +++ b/jarvis/skills/intent_manager.py @@ -1,3 +1,5 @@ +import json + from adapt.engine import DomainIntentDeterminationEngine from padatious import IntentContainer @@ -35,7 +37,7 @@ def train_padatious(): padatious_intents_container.train() -def process_handlers(): +def load_all_skills(): for handler in intents_handlers_adapt: function_handler = intents_handlers_adapt.get(handler) intent_builder = getattr(function_handler[0], "_data", [])[0] @@ -87,8 +89,10 @@ def recognise(sentence): # print(best_intent) # DEBUG - # TODO: add data for adapt handle(best_intent['intent_type'], data={'utterance': sentence}) + + return best_intent + except StopIteration as e: pass # print("No match... (Adapt)") @@ -107,6 +111,9 @@ def recognise(sentence): data['utterance'] = result.sent data.update(result.matches) # adding the matches from padatious to the data handle(result.name, data) + + return json.dumps(str(result)) + else: pass # print("No match... (Padatious)")