From 14da3bc8f2dc87354381fb3ef57b112392a9b831 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Mon, 13 Sep 2021 22:10:34 +0200 Subject: [PATCH] Adapted spotify skill for using new required config fields system --- .../skills/entertainement/spotify/__init__.py | 5 +++-- jarvis/skills/entertainement/spotify/spotify.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/jarvis/skills/entertainement/spotify/__init__.py b/jarvis/skills/entertainement/spotify/__init__.py index cab258b..1ecf8a8 100644 --- a/jarvis/skills/entertainement/spotify/__init__.py +++ b/jarvis/skills/entertainement/spotify/__init__.py @@ -1,18 +1,19 @@ from jarvis.skills import Skill, SkillRegistering from jarvis.skills.decorators import intent_file_handler from jarvis.skills.entertainement.spotify import spotify +from jarvis.utils import config_utils class SpotifySkill(Skill, metaclass=SkillRegistering): def __init__(self, data=dict): - super().__init__("SpotifySkill", data) + super().__init__("SpotifySkill", data, required_config=['SPOTIFY_CLIENT_ID', 'SPOTIFY_CLIENT_SECRET']) @intent_file_handler("play_a_song.intent", "PlaySongWithSpotifyIntent") def handle_play_a_song(self, data): print(data) 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 matching_song is not None and len(matching_song) >= 1: diff --git a/jarvis/skills/entertainement/spotify/spotify.py b/jarvis/skills/entertainement/spotify/spotify.py index f24b607..fa01ead 100644 --- a/jarvis/skills/entertainement/spotify/spotify.py +++ b/jarvis/skills/entertainement/spotify/spotify.py @@ -12,14 +12,19 @@ from jarvis.utils import config_utils scope = "user-read-playback-state, user-modify-playback-state, user-read-currently-playing" # TODO: Investigate the open_browser and automatic auth renewing without user interaction -sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, - client_id=config_utils.get_in_config("SPOTIFY_CLIENT_ID"), - client_secret=config_utils.get_in_config("SPOTIFY_CLIENT_SECRET"), - redirect_uri='http://localhost:8888/callback/', - open_browser=False)) + +sp = None def get_spotify(): + global sp + if sp is None: + sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, + client_id=config_utils.get_in_secret("SPOTIFY_CLIENT_ID"), + client_secret=config_utils.get_in_secret( + "SPOTIFY_CLIENT_SECRET"), + redirect_uri='http://localhost:8888/callback/', + open_browser=False)) return sp @@ -53,7 +58,7 @@ def query_song(song=None, artist=None): def is_music_playing(): - return sp.current_user_playing_track()['is_playing'] + return get_spotify().current_user_playing_track()['is_playing'] def best_confidence(title, query):