-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spotify_Oled_Control.py
307 lines (247 loc) · 10.6 KB
/
Spotify_Oled_Control.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
import os
import time
from json.decoder import JSONDecodeError
import spotipy
import spotipy.util as util
from luma.core.render import canvas
from PIL import ImageFont
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1309, ssd1325, ssd1331, sh1106
import threading
from datetime import datetime
from datetime import timedelta
import configparser
font_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "fonts", 'cour.ttf'))
font = ImageFont.truetype(font_path, 18)
# substitute spi(device=0, port=0) below if using that interface
serial = i2c(port=1, address=0x3C)
# substitute ssd1331(...) or sh1106(...) below if using that device
device = ssd1306(serial)
Width = 128
Height = 64
scrollspeed = 4
scrollbackspeed = 8
songfontsize = 18
artistfontsize = 15
seekfontsize = 10
# Get associated values from config file
config = configparser.ConfigParser()
config.read('config.txt')
credentialValues = config['credentials']
client_id = credentialValues['client_id']
client_secret = credentialValues['client_secret']
redirect_uri = credentialValues['redirect_uri']
username = credentialValues['username']
scope = 'user-read-playback-state'
class Spotify:
def __init__(self, username, scope, client_id, client_secret, redirect_uri):
self.username = username
self.scope = scope
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.cache_path = ".cache-{}".format(username)
self.track = None
self.artists = None
self.durationMs = None
self.progressMs = None
self.shuffleState = None
self.isPlaying = None
self.sp = spotipy.Spotify(
auth_manager=spotipy.SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=self.scope,
cache_path=self.cache_path)
)
def get_playback(self):
playback = self.sp.current_playback()
try:
self.track = playback['item']['name']
self.artists = playback['item']['artists']
self.durationMs = playback['item']['duration_ms']
self.progressMs = playback['progress_ms']
self.shuffleState = playback['shuffle_state']
self.isPlaying = playback['is_playing']
# TypeError is raised when nothing is playing
except TypeError:
self.isPlaying = False
def __str__(self):
if self.isPlaying:
return "Now playing " + self.track + " by " + self.artists[0]["name"] + " from Spotify"
return "Nothing playing"
class ScrollThread(threading.Thread):
def __init__(self, word, fontsize, ypos):
threading.Thread.__init__(self)
self.word = word
self.end = False
self.Width = Width
self.x = 5
self.ypos = ypos
self.font = ImageFont.truetype(font_path, fontsize)
self.move = False # True = right
self.scrolling = False
with canvas(device) as draw:
self.w, self.h = draw.textsize(self.word, font=self.font)
def calc_scrolling(self):
with canvas(device) as draw:
self.w, self.h = draw.textsize(self.word, font=self.font)
if self.w > Width:
self.scrolling = True
def run(self): # scroll
while True:
lastmove = self.move
if self.scrolling and not self.end: # This could be cleaner by only using one while loop & reverse variable
if self.move:
self.x += scrollbackspeed
else:
self.x -= scrollspeed
if self.x < ((Width - self.w) - 10) and not self.move: # Was moving left and has moved enough
self.move = True
else:
if self.x > 0 and self.move: # Was moving right and more than 0
self.move = False
if not self.move and lastmove:
self.end = True
time.sleep(3)
time.sleep(.2)
def draw_obj(self):
draw.text((self.x, self.ypos), self.word, font=self.font, fill="white")
class SeekThread(threading.Thread):
def __init__(self, current_pos, song_len, isplaying):
threading.Thread.__init__(self)
self.padding = 35
self.font = ImageFont.truetype(font_path, seekfontsize)
self.currentPos = current_pos
self.lastTime = int(time.time())
self.songLen = song_len
self.end = False
self.isPlaying = isplaying
self.xpos = None
def run(self):
while True:
diff = time.time() - self.lastTime
self.lastTime = time.time()
if self.isPlaying:
self.currentPos += diff
percent = self.currentPos / self.songLen
self.xpos = self.padding + int(percent * (Width - self.padding * 2))
if percent >= 1:
self.end = True
else:
self.end = False
time.sleep(1)
def draw_obj(self):
m, s = divmod(int(self.songLen), 60)
h, m = divmod(m, 60)
c_song_leng = '{:02d}:{:02d}'.format(m, s)
draw.text((Width - self.padding + 5, (Height - 11)), c_song_leng, font=self.font, fill="white")
m, s = divmod(int(self.currentPos), 60)
h, m = divmod(m, 60)
c_song_pos = '{:02d}:{:02d}'.format(m, s)
draw.text((0, (Height - 11)), c_song_pos, font=self.font, fill="white")
if self.isPlaying:
# draw seek bar outline
draw.rectangle((self.padding, (Height - 6), (Width - self.padding), (Height - 3)), "black", "white", 1)
# draw bar within
draw.rectangle((self.padding, (Height - 5), (self.xpos + 2), (Height - 4)), "black", "white", 2)
else:
# draw pause
draw.rectangle((67, (Height - 11), 70, Height), "white", "white", 1)
draw.rectangle((55, (Height - 11), 58, Height), "white", "white", 1)
def remove_feat(trackname):
if "(feat." in trackname:
start = trackname.index("(feat")
end = trackname.index(")") + 2
return trackname.replace(trackname[start:end], "")
else:
return trackname
def concat_artists(artists):
if len(artists) > 1:
names = ""
names += artists[0]["name"]
for i in range(1, len(artists)):
names += "," + artists[i]["name"]
return names
else:
return artists[0]["name"]
if __name__ == "__main__":
try:
with canvas(device) as draw:
draw.text((Width / 2, 32), "loading", font=ImageFont.truetype(font_path, 12), fill="white")
spotifyData = Spotify(username=username, scope=scope, client_id=client_id, client_secret=client_secret,
redirect_uri=redirect_uri)
lastSong = ""
spotifyData.get_playback()
NETWORKTIMEOUT = 1 # ten seconds
drawTime = datetime.now() + timedelta(seconds=NETWORKTIMEOUT)
songScrollThread = ScrollThread(word=remove_feat(spotifyData.track), fontsize=songfontsize, ypos=5)
songScrollThread.start()
artistScrollThread = ScrollThread(word=concat_artists(spotifyData.artists), fontsize=artistfontsize, ypos=30)
artistScrollThread.start()
with canvas(device) as draw:
songScrollThread.draw_obj()
artistScrollThread.draw_obj()
try:
lastSong = spotifyData.track + spotifyData.artists[0]["name"]
except AttributeError:
pass
seekThread = SeekThread((spotifyData.progressMs / 1000), (spotifyData.durationMs / 1000), spotifyData.isPlaying)
seekThread.start()
while True:
try:
lastSong = spotifyData.track + spotifyData.artists[0]["name"]
except AttributeError:
pass
print(spotifyData)
songScrollThread.scrolling = False
songScrollThread.x = 0
songScrollThread.word = remove_feat(spotifyData.track)
songScrollThread.calc_scrolling()
artistScrollThread.scrolling = False
artistScrollThread.x = 0
artistScrollThread.word = concat_artists(spotifyData.artists)
artistScrollThread.calc_scrolling()
seekThread.songLen = spotifyData.durationMs / 1000
seekThread.currentPos = spotifyData.progressMs / 1000
seekThread.isPlaying = spotifyData.isPlaying
if spotifyData.progressMs < spotifyData.durationMs:
seekThread.end = False
while not seekThread.end:
with canvas(device) as draw:
songScrollThread.draw_obj()
artistScrollThread.draw_obj()
seekThread.draw_obj()
if datetime.now() > drawTime:
if not songScrollThread.scrolling: # Check if both are not scrolling
print("Checking song playback")
spotifyData.get_playback()
seekThread.currentPos = spotifyData.progressMs / 1000
seekThread.isPlaying = spotifyData.isPlaying
drawTime = datetime.now() + timedelta(seconds=NETWORKTIMEOUT)
if spotifyData.track + spotifyData.artists[0]["name"] != lastSong:
break
else:
artistScrollThread.end = False
else:
if songScrollThread.end: # TODO potentially should check if both scrolls are at 0
print("Scroll ended, checking song playback")
spotifyData.get_playback()
seekThread.currentPos = spotifyData.progressMs / 1000
seekThread.isPlaying = spotifyData.isPlaying
if spotifyData.track + spotifyData.artists[0]["name"] != lastSong:
print("Song changed")
break
else:
songScrollThread.end = False
artistScrollThread.end = False
drawTime = datetime.now() + timedelta(seconds=NETWORKTIMEOUT)
else:
pass
spotifyData.get_playback()
print("Song ended or changed")
except KeyboardInterrupt:
pass