forked from Concept-Bytes/HomePod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot.py
64 lines (51 loc) · 1.87 KB
/
spot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
import os
load_dotenv()
username = os.getenv('SPOTIFY_USERNAME')
clientID = os.getenv('SPOTIFY_CLIENT_ID')
clientSecret = os.getenv('SPOTIFY_CLIENT_SECRET')
redirect_uri = 'http://localhost:8888/callback'
def spotify_authenicate(client_id, client_secret, redirect_uri, username):
scope = "user-read-currently-playing user-modify-playback-state"
auth_manager = SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, username=username)
return spotipy.Spotify(auth_manager = auth_manager)
spotify = spotify_authenicate(clientID, clientSecret, redirect_uri, username)
def get_current_playing_info():
global spotify
current_track = spotify.current_user_playing_track()
if current_track is None:
return None
artist_name = current_track['item']['artists'][0]['name']
album_name = current_track['item']['album']['name']
track_title = current_track['item']['name']
return {
"artist": artist_name,
"album": album_name,
"title": track_title
}
def start_music():
global spotify
try:
spotify.start_playback()
except spotipy.SpotifyException as e:
return f"Error in starting playback: {str(e)}"
def stop_music():
global spotify
try:
spotify.pause_playback()
except spotipy.SpotifyException as e:
return f"Error in starting playback: {str(e)}"
def skip_to_next():
global spotify
try:
spotify.next_track()
except spotipy.SpotifyException as e:
return f"Error in starting playback: {str(e)}"
def skip_to_previous():
global spotify
try:
spotify.previous_track()
except spotipy.SpotifyException as e:
return f"Error in starting playback: {str(e)}"