-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeck.py
executable file
·263 lines (221 loc) · 8.52 KB
/
speck.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
import time
import webbrowser
from configparser import ConfigParser
import spotipy
from spotipy import util
from spotipy import SpotifyException
import rumps
# Config
config = ConfigParser()
config.read("speck.ini")
# Config > Secrets
CLIENT_ID = config["secrets"]["client_id"]
CLIENT_SECRET = config["secrets"]["client_secret"]
USERNAME = config["secrets"]["username"]
# Config > Settings
DEBUG = config["settings"]["debug"]
REDIRECT_URI = config["settings"]["redirect_uri"]
SCOPE = config["settings"]["scope"]
MAX_TRACK_LENGTH = int(config["settings"]["max_track_length"])
MAX_RETRIES = int(config["settings"]["max_retries"])
UPDATE_INTERVAL = float(config["settings"]["update_interval"])
class App(rumps.App):
rumps.debug_mode(DEBUG)
def __init__(self) -> None:
super(App, self).__init__("Speck")
self.token = ""
self.spotify = ""
self.state_prev = ""
self.state = ""
self.pause_count = 0
self.track_data = {}
self.device_selected = ""
self.menu = [
"Pause/Play",
None,
"Next",
"Previous",
None,
"Save to your Liked Songs",
None,
"Track Info",
None,
"Devices",
None,
]
self.authorize_spotify()
self.populate_menu_device()
""" Devices """
def populate_menu_device(self):
self.devices = [device for device in self.spotify.devices()["devices"]]
for device in self.devices:
self.menu["Devices"].add(device["name"])
self.device_titles = self.menu["Devices"][device["name"]].title
self.menu["Devices"][device["name"]].set_callback(self.__set_active_device)
if device["is_active"]:
self.menu["Devices"][device["name"]].state = 1
def __get_active_device(self) -> list:
devices = self.spotify.devices()["devices"]
active_devices = [device for device in devices if device["is_active"] is True]
if self.device_selected:
active_device = self.device_selected
elif active_devices:
active_device = active_devices[0]["id"]
else:
rumps.alert(
title="No active devices",
message="Select a device from the Devices menu.",
)
active_device = None
return active_device
def __set_active_device(self, sender):
# TODO should also check on id from __get_active_device() on initial load and then set state
device_title = sender.title
for device in self.devices:
if device["name"] == device_title:
device_id = device["id"]
self.device_selected = device_id
for item in self.menu["Devices"]:
self.menu["Devices"][item].state = 0
sender.state = 1
self.pause_play_track(sender)
""" Spotify """
def authorize_spotify(self) -> None:
self.token = util.prompt_for_user_token(
USERNAME,
scope=SCOPE,
redirect_uri=REDIRECT_URI,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
)
self.spotify = spotipy.Spotify(
auth=self.token, retries=MAX_RETRIES, status_retries=MAX_RETRIES
)
""" Spotify > Functions """
def __set_saved_track(self, track_id: str) -> None:
menu_item = self._menu["Save to your Liked Songs"]
if self.spotify.current_user_saved_tracks_contains([track_id])[0] is True:
menu_item.title = "Remove from your Liked Songs"
else:
menu_item.title = "Save to your Liked Songs"
menu_item.set_callback(self.add_remove_saved_track)
def __get_playback_state(self) -> None:
menu_item = self._menu["Pause/Play"]
if self.track_data["is_playing"] is True:
menu_item.title = "Pause"
else:
menu_item.title = "Play"
menu_item.set_callback(self.pause_play_track)
def __set_menu_playback_state(
self, state: str, track: str = None, band: str = None
) -> None:
if state != self.state_prev:
self.icon = f"./resources/{state}.png"
if state == "active":
self.title = track + " · " + str(band)
self.pause_count = 0
elif state == "paused":
self.title = track + " · " + str(band)
else:
self.title = str.capitalize(f"{state}")
if self.state and self.state_prev == "paused":
self.pause_count += 1
""" Utils """
def __shorten_text(self, text: str) -> str:
if len(text) > MAX_TRACK_LENGTH:
text = text[0:MAX_TRACK_LENGTH] + "..."
return text
def __get_bands(self, artists: str) -> str:
band = []
for artist in artists:
band.append(self.__shorten_text(artist))
band = ", ".join(band)
return band
""" Rumps Player """
@rumps.clicked("Pause/Play")
def pause_play_track(self, sender) -> None:
"""Pause or play track based on playback status"""
try:
if self.track_data is None:
rumps.alert(
title="No active songs",
message="Play a song from the Spotify app.",
)
elif self.state == "active":
self.spotify.pause_playback()
else:
self.spotify.transfer_playback(device_id=self.__get_active_device())
self.update_track(self)
except SpotifyException:
self.authorize_spotify()
@rumps.clicked("Next")
def next_track(self, sender) -> None:
"""Next track"""
try:
self.spotify.next_track()
time.sleep(0.25)
self.update_track()
except SpotifyException:
self.authorize_spotify()
@rumps.clicked("Previous")
def prev_track(self, sender) -> None:
try:
self.spotify.previous_track()
time.sleep(0.25)
self.update_track()
except SpotifyException:
self.authorize_spotify()
@rumps.clicked("Track Info")
def open_browser(self, sender) -> None:
"""Open web browser to search track information"""
webbrowser.open(
f'https://google.com/search?q={self.track_data["item"]["name"]}+{self.track_data["item"]["artists"][0]["name"]}'
)
@rumps.clicked("Save to your Liked Songs")
def add_remove_saved_track(self, sender) -> None:
"Add or remove playing track to Liked playlist"
track_id = self.track_data["item"]["id"]
if self.spotify.current_user_saved_tracks_contains([track_id])[0] is False:
try:
self.spotify.current_user_saved_tracks_add(tracks=[track_id])
rumps.notification(
title="Saved to your liked songs",
subtitle=None,
message=f'{self.track_data["item"]["artists"][0]["name"]} - {self.track_data["item"]["name"]}',
)
except SpotifyException:
self.authorize_spotify()
else:
self.spotify.current_user_saved_tracks_delete([track_id])
""" Track Update """
@rumps.timer(UPDATE_INTERVAL)
def update_track(self, sender=None) -> None:
if self.token:
try:
self.track_data = self.spotify.current_user_playing_track()
except SpotifyException:
self.authorize_spotify()
self.update_track()
if self.track_data is not None:
is_playing = self.track_data["is_playing"]
track_id = self.track_data["item"]["id"]
track = self.__shorten_text(self.track_data["item"]["name"])
band = self.__get_bands([self.track_data["item"]["artists"][0]["name"]])
self.__get_playback_state()
self.__set_saved_track(track_id)
if is_playing is True:
self.state_prev = self.state
self.state = "active"
else: # Spotify is Paused
self.state_prev = self.state
self.state = "paused"
self.__set_menu_playback_state(self.state, track, band)
else:
self.state = "sleeping"
self.__set_menu_playback_state(self.state)
else:
self.__set_menu_playback_state("error")
if __name__ == "__main__":
app = App()
app.run()