import spotipy from spotipy.oauth2 import SpotifyOAuth from spotipy.exceptions import SpotifyException from spotify_settings import getSpotifySettings class spotifyAbstract: sp = None previousVolume = 20 @staticmethod def SpotifyControl(): if spotifyAbstract.sp is None: spotifySettings = getSpotifySettings() spotifyAbstract.sp = spotipy.Spotify(auth_manager=SpotifyOAuth( client_id=spotifySettings["client_id"], client_secret=spotifySettings["client_secret"], redirect_uri=spotifySettings["redirect_uri"], scope="user-modify-playback-state user-read-playback-state" )) return spotifyAbstract.sp @staticmethod def currentPlayback(): try: return spotifyAbstract.SpotifyControl().current_playback() except SpotifyException as error: print("Could not check Spotify playback.") print(error) return None @staticmethod def somethingPlaying(): playback = spotifyAbstract.currentPlayback() if playback is None: return False return playback["is_playing"] @staticmethod def getCurrentVolume(): playback = spotifyAbstract.currentPlayback() if playback is None: return None if playback["device"] is None: return None return playback["device"]["volume_percent"] @staticmethod def setVolume(volume): if volume < 0: volume = 0 if volume > 100: volume = 100 try: spotifyAbstract.SpotifyControl().volume(volume) print(f"Spotify volume set to {volume}%") except SpotifyException as error: print("Could not set Spotify volume.") print(error) @staticmethod def pause(): if not spotifyAbstract.somethingPlaying(): print("Spotify is not currently playing. Not sending pause command.") return try: spotifyAbstract.SpotifyControl().pause_playback() print("Spotify paused.") except SpotifyException as error: print("Could not pause Spotify.") print(error) @staticmethod def play(): try: spotifyAbstract.SpotifyControl().start_playback() print("Spotify started.") except SpotifyException as error: print("Could not start Spotify.") print(error) @staticmethod def togglePlayPause(): if spotifyAbstract.somethingPlaying(): spotifyAbstract.pause() else: spotifyAbstract.play() @staticmethod def nextTrack(): if not spotifyAbstract.somethingPlaying(): print("Spotify is not currently playing. Not sending next-track command.") return try: spotifyAbstract.SpotifyControl().next_track() print("Skipped to next track.") except SpotifyException as error: print("Could not skip track.") print(error) @staticmethod def previousTrack(): if not spotifyAbstract.somethingPlaying(): print("Spotify is not currently playing. Not sending previous-track command.") return try: spotifyAbstract.SpotifyControl().previous_track() print("Went to previous track.") except SpotifyException as error: print("Could not go to previous track.") print(error) @staticmethod def volumeUp(): spotifyAbstract.volumeUpBy(5) @staticmethod def volumeUpBy(increment): volume = spotifyAbstract.getCurrentVolume() if volume is None: print("Could not read current Spotify volume.") return spotifyAbstract.setVolume(volume + increment) @staticmethod def volumeDown(): spotifyAbstract.volumeDownBy(5) @staticmethod def volumeDownBy(increment): volume = spotifyAbstract.getCurrentVolume() if volume is None: print("Could not read current Spotify volume.") return spotifyAbstract.setVolume(volume - increment) @staticmethod def muteToggle(): volume = spotifyAbstract.getCurrentVolume() if volume is None: print("Could not read current Spotify volume.") return if volume > 0: spotifyAbstract.previousVolume = volume spotifyAbstract.setVolume(0) print("Spotify muted.") else: spotifyAbstract.setVolume(spotifyAbstract.previousVolume) print("Spotify unmuted.")