-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
186 lines (151 loc) · 6.11 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python3
import time
import datetime
import json
import logging
import spotipy
import requests
from gpiozero import Button
def import_config():
with open('config.json', 'r') as f:
return(json.load(f))
class Spotify():
def __init__(self, client_id, client_secret, redirect_uri, logger, *args, **kwargs) -> None:
self.logger = logger
self.logger.debug("Creating spotify class")
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.scope = "user-read-playback-state streaming"
self.auth_manager, self.spotify = self.authenticate()
self.logger.info("Spotify initialized")
def authenticate(self):
self.logger.debug("Authenticating to Spotify")
auth_manager = spotipy.oauth2.SpotifyOAuth(scope=self.scope, client_id=self.client_id, client_secret=self.client_secret, redirect_uri=self.redirect_uri)
spotify = spotipy.Spotify(auth_manager=auth_manager)
self.logger.info("Authenticated to Spotify")
return auth_manager, spotify
def refresh_token(self):
token_info = self.auth_manager.cache_handler.get_cached_token()
if self.auth_manager.is_token_expired(token_info):
self.logger.info("Token expired, recreating session")
self.auth_manager, self.spotify = self.authenticate()
def get_devices(self):
return self.spotify.devices()
def select_device(self, device_name):
self.logger.info(f"Selecting device with name: {device_name}")
devices = self.get_devices()
selected_devices = [device for device in devices.get("devices") if device.get("name") == device_name]
if not selected_devices:
self.logger.error(f"No device found with name: {device_name}, sleeping for 5 seconds and exiting.")
time.sleep(5)
exit(1)
self.selected_device = selected_devices[0]
self.selected_device_id = self.selected_device.get("id")
self.logger.info(f"Device found with id: {self.selected_device_id}")
def get_status(self):
return self.spotify.current_playback()
def start(self, uri):
if not self.selected_device_id:
return
# Play URI
self.spotify.start_playback(self.selected_device_id, uri)
def next(self):
self.spotify.next_track(self.selected_device_id)
def pause(self):
if not self.selected_device_id:
return
self.spotify.pause_playback()
def shuffle(self, shuffle_state = True):
if not self.selected_device_id:
return
self.spotify.shuffle(shuffle_state, self.selected_device_id)
def handle_click(button, spotify, uri, allowed_time, logger):
now = datetime.datetime.now()
if (now.hour > allowed_time.get("end") or now.hour <= allowed_time.get("start")):
logger.error(f"Button clicked but not within allowed time {allowed_time}")
time.sleep(1)
return
#Refresh token
spotify.refresh_token()
try:
status = spotify.get_status()
except requests.exceptions.ConnectionError as e:
logger.error(e)
logger.info("Error grabbing current status, trying again.")
status = spotify.get_status()
#Defaults
is_playing = False
playing_device_id = ""
current_context_uri = ""
if status:
playing_device = status.get("device")
if playing_device:
playing_device_id = playing_device.get("id")
is_playing = status.get("is_playing")
current_context_uri = status.get("context").get("uri")
# If we are playing on the selected device. We pause
if is_playing and playing_device_id == spotify.selected_device_id:
# Pause
logger.info(f"Pausing playback with pin: {button.get('pin')}")
try:
spotify.pause()
except spotipy.exceptions.SpotifyException as e:
logger.error(e)
logger.info("Waiting 2 seconds and trying again.")
time.sleep(2)
spotify.pause
else:
if current_context_uri == uri:
#Next
logger.info(f"Next playback with pin: {button.get('pin')}")
try:
spotify.shuffle(True)
spotify.next()
except spotipy.exceptions.SpotifyException as e:
logger.error(e)
logger.info("Waiting 2 seconds and trying again.")
time.sleep(2)
spotify.shuffle(True)
spotify.next()
else:
# Play
logger.info(f"Starting playback with pin: {button.get('pin')}")
try:
spotify.shuffle(False)
spotify.shuffle(True)
spotify.start(uri)
except spotipy.exceptions.SpotifyException as e:
logger.error(e)
logger.info("Waiting 2 seconds and trying again.")
time.sleep(2)
spotify.shuffle(False)
spotify.shuffle(True)
spotify.start(uri)
def main():
config = import_config()
debug = config.get("debug")
if debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
spotify = Spotify(logger=logger, **config)
buttons = config.get("buttons")
selected_device_name = config.get("selected_device_name")
allowed_time = config.get("allowed_time") if config.get("allowed_time") else {}
# spotify.select_device(selected_device_name)
spotify.selected_device_id = config.get("selected_device_id")
# Setting up buttons
gpio_buttons = []
for button in buttons:
button_pin = button.get("pin")
spotify_uri = button.get("spotify_uri")
gpio_button = Button(button_pin)
gpio_button.when_pressed = lambda button=button, button_pin=button_pin, spotify_uri=spotify_uri: handle_click(button, spotify, spotify_uri, allowed_time, logger)
gpio_buttons.append(gpio_button)
# Main loop
while True:
time.sleep(10)
if __name__ == "__main__":
main()