2021-07-29 12:35:07 +02:00
|
|
|
from jarvis.skills import Skill, SkillRegistering
|
2021-07-29 14:57:55 +02:00
|
|
|
from jarvis.skills.decorators import intent_file_handler
|
2021-07-29 23:04:31 +02:00
|
|
|
from jarvis.skills.entertainement.spotify import spotify
|
2021-07-29 12:35:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SpotifySkill(Skill, metaclass=SkillRegistering):
|
2021-08-01 11:48:57 +02:00
|
|
|
def __init__(self, data=dict):
|
|
|
|
super().__init__("SpotifySkill", data)
|
2021-07-29 12:35:07 +02:00
|
|
|
|
2021-07-29 20:16:22 +02:00
|
|
|
@intent_file_handler("play_a_song.intent", "PlaySongWithSpotifyIntent")
|
2021-07-29 14:57:55 +02:00
|
|
|
def handle_play_a_song(self, data):
|
2021-07-30 14:10:35 +02:00
|
|
|
print(data)
|
|
|
|
|
2021-08-28 16:56:15 +02:00
|
|
|
matching_song = spotify.query_song(data['song'] if 'song' in data else None,
|
2021-07-30 19:58:58 +02:00
|
|
|
data['artist'] if 'artist' in data else None)
|
2021-07-30 14:10:35 +02:00
|
|
|
|
2021-08-28 16:56:15 +02:00
|
|
|
if matching_song is not None and len(matching_song) >= 1:
|
2021-08-22 18:44:32 +02:00
|
|
|
|
|
|
|
# pause the music before speaking dialog
|
|
|
|
if spotify.is_music_playing():
|
|
|
|
spotify.get_spotify().pause_playback()
|
2021-08-01 18:24:13 +02:00
|
|
|
|
2021-08-01 12:22:00 +02:00
|
|
|
if 'artist' in data and 'song' not in data:
|
2021-08-28 16:56:15 +02:00
|
|
|
self.speak_dialog("play_from_artist", {'artist': matching_song['artists'][0]['name']})
|
2021-08-01 12:22:00 +02:00
|
|
|
else:
|
2021-08-28 16:56:15 +02:00
|
|
|
self.speak_dialog("play_song_from_artist", {'song': matching_song[
|
|
|
|
'name'], 'artist': matching_song['artists'][0]['name']})
|
2021-07-30 14:10:35 +02:00
|
|
|
|
2021-08-28 16:56:15 +02:00
|
|
|
spotify.get_spotify().add_to_queue(uri=matching_song['uri'])
|
2021-07-30 14:10:35 +02:00
|
|
|
spotify.get_spotify().next_track()
|
2021-07-30 19:58:58 +02:00
|
|
|
else:
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("nothing_found")
|
2021-07-29 23:29:42 +02:00
|
|
|
|
|
|
|
@intent_file_handler("pause_music.intent", "PauseSpotifyIntent")
|
|
|
|
def pause_music(self, data):
|
2021-07-31 13:25:37 +02:00
|
|
|
if spotify.is_music_playing():
|
|
|
|
spotify.get_spotify().pause_playback()
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("pause_spotify")
|
2021-07-31 13:25:37 +02:00
|
|
|
else:
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("nothing_playing")
|
2021-07-31 13:25:37 +02:00
|
|
|
|
|
|
|
@intent_file_handler("resume_music.intent", "ResumeSpotifyIntent")
|
|
|
|
def resume_music(self, data):
|
|
|
|
if not spotify.is_music_playing():
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("resume_music")
|
2021-07-31 13:25:37 +02:00
|
|
|
spotify.get_spotify().start_playback()
|
|
|
|
else:
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("already_playing")
|
2021-07-30 10:14:18 +02:00
|
|
|
|
|
|
|
@intent_file_handler("current_song.intent", "CurrentSongSpotifyIntent")
|
|
|
|
def current_song(self, data):
|
|
|
|
current_playback = spotify.get_spotify().current_playback()
|
|
|
|
if current_playback['is_playing']:
|
|
|
|
song_name = current_playback['item']['name']
|
|
|
|
artist = current_playback['item']['artists'][0]['name']
|
|
|
|
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog("current_playing_song_from_artist", {'song': song_name, 'artist': artist})
|
2021-07-30 12:54:25 +02:00
|
|
|
else:
|
2021-08-01 17:02:21 +02:00
|
|
|
self.speak_dialog('nothing_playing')
|
2021-07-30 12:28:42 +02:00
|
|
|
|
|
|
|
|
2021-08-01 11:48:57 +02:00
|
|
|
def create_skill(data):
|
|
|
|
return SpotifySkill(data)
|