forked from bishely/one-button-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
100 lines (90 loc) · 2.58 KB
/
test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import sys
#from gpiozero import Button
import spotipy
import spotipy.util as util
import time
import json
#button = Button(pin=12)
username = 'xxxYourUserNamexxx'
password = 'xxxYourPasswordxxx'
playlist = 'xxxYourPlayListURIfromSpotifyxxx'
spotconnect_device_name = 'xxxYourTargetSpotifyConnectDeviceNamexxx'
SP_CLIENT_ID = 'xxxClientIDofYourAppxxx'
SP_CLIENT_SECRET = 'xxxClientSecretofYourAppxxx'
SP_REDIRECT_URI = 'http://localhost/'
global token
global playing
global device
token = ''
playing = False
device = ''
scope = 'user-library-read, user-read-playback-state, user-modify-playback-state'
def spotStart():
global token
token = util.prompt_for_user_token(username, scope,client_id=SP_CLIENT_ID,client_secret=SP_CLIENT_SECRET,redirect_uri=SP_REDIRECT_URI)
def spotDevices():
global token
print "Get Devices"
if token:
global device
sp = spotipy.Spotify(auth=token)
devices = sp.devices()
devices = devices['devices']
dictionary = {}
for item in devices:
dictionary[item['name']] = item['id']
print dictionary
device = dictionary[spotconnect_device_name]
else:
#print "Can't get token for", username
token = ''
def spotPlay():
global token
global playing
print "Play"
if token:
if playing == True:
# if we're already playing, skip to a new track
sp = spotipy.Spotify(auth=token)
sp.next_track()
else:
# if we're not playing, play the playlist, turn on shuffle and skip to a new (random) track
sp = spotipy.Spotify(auth=token)
sp.start_playback(device_id=device,context_uri=playlist)
sp.shuffle(True)
sp.next_track()
playing = True
else:
#print "Can't get token for", username
token = ''
def spotStop():
global token
global playing
print "Stop"
if token:
if playing == False:
# if playback is already paused/stopped, do nothing
pass
else:
# stop (pause) playing
sp = spotipy.client.Spotify(auth=token)
sp.pause_playback()
playing = False
else:
#print "Can't get token for", username
token = ''
while True:
if not token:
spotStart()
spotDevices()
if True:
if playing == True:
# long press script
spotStop()
time.sleep(5)
else:
# short press script
spotPlay()
time.sleep(5)
else:
time.sleep(5)