Skip to content

Commit 88b7b8f

Browse files
author
BytequillPC
committed
ENV CONFIG + CLEANUP + AUTH OVERHAUL
1 parent 5d221ed commit 88b7b8f

5 files changed

Lines changed: 98 additions & 89 deletions

File tree

.env.example

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# If not set at all, defaults to AUTO
2+
COM_PORT=AUTO
3+
# Can be:
4+
# A - Turing Smart Screen (rev. A) 3.5" and UsbMonitor screens (all sizes)
5+
# B - XuanFang (rev. B & flagship) 3.5" screens
6+
# C - Turing Smart Screen 5" screens
7+
# D - Kipye Qiye Smart Display 3.5"
8+
# S - Simulated display. Creates a file called screencap.png with the display output
9+
COM_REV=A
10+
# Reccomended to keep default for spacing to work out as it is based on pixels
11+
DISPLAY_WIDTH=480
12+
DISPLAY_HEIGHT=320
13+
# Display brightness is a percentage
14+
DISPLAY_BRIGHTNESS=45
15+
16+
BGCOL_R=50
17+
BGCOL_G=50
18+
BGCOL_B=50
19+
# In seconds; Due to ratelimits a reasonable value is 2
20+
CHECK_EVERY=2
21+
22+
# Spotify config, make sure your application has the URI added
23+
# the URI must be localhost(or 127.0.0.1)
24+
SPOTIPY_CLIENT_ID=ENTER_OWN_HERE
25+
SPOTIPY_REDIRECT_URI=http://localhost:9099/getToken

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
.cache
44
log.log
55

6+
.env
7+
68
# Came with the library as an example program, used it for API reference
79
simple-program.py
810

main.py

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,72 @@
11
from library.lcd.lcd_comm_rev_a import LcdCommRevA
2+
from library.lcd.lcd_comm_rev_b import LcdCommRevB
3+
from library.lcd.lcd_comm_rev_c import LcdCommRevC
4+
from library.lcd.lcd_comm_rev_d import LcdCommRevD
25
from library.lcd.lcd_simulated import LcdSimulated
36
from library.lcd.lcd_comm import LcdComm, Orientation
47
from PIL import Image
58
from time import sleep
6-
from spotify_auth import HandleAuth
79
import spotipy
10+
from spotipy.oauth2 import SpotifyPKCE
811
import datetime
912
import threading
1013
import signal
11-
import io
14+
import io, os, os.path
1215
import urllib.request as urllib
16+
import dotenv
1317

14-
COM_PORT = "AUTO"
15-
COM_REV = LcdCommRevA
16-
WIDTH, HEIGHT = 480, 320
17-
BRIGHTNESS = 45
18-
BGCOL = (50, 50, 50)
19-
CHECK_EVERY = 4 # in seconds
18+
if os.path.exists(".env"):
19+
dotenv.load_dotenv()
20+
elif os.path.exists(".env.example"):
21+
dotenv.load_dotenv(dotenv_path=".env.example")
22+
else:
23+
print("[warn] No .env or .env.example file found. Make sure to pass in correct configuration in your environment")
2024

25+
try:
26+
try: # > If not set at all, defaults to AUTO
27+
COM_PORT = os.getenv("COM_PORT")
28+
except Exception:
29+
COM_PORT = "AUTO"
30+
revint = os.getenv("COM_REV")
31+
if revint.upper() == "A":
32+
COM_REV = LcdCommRevA
33+
elif revint.upper() == "B":
34+
COM_REV = LcdCommRevB
35+
elif revint.upper() == "C":
36+
COM_REV = LcdCommRevC
37+
elif revint.upper() == "D":
38+
COM_REV = LcdCommRevD
39+
elif revint.upper() == "S":
40+
COM_REV = LcdSimulated
41+
else:
42+
print("[error] Please specify a valid revision")
43+
exit(1)
44+
45+
WIDTH, HEIGHT = int(os.getenv("DISPLAY_WIDTH")), int(os.getenv("DISPLAY_HEIGHT"))
46+
BRIGHTNESS = int(os.getenv("DISPLAY_BRIGHTNESS"))
47+
if BRIGHTNESS < 0 or BRIGHTNESS > 100:
48+
print("[error] Brightness is a percentage value (i.e 0-100)")
49+
exit(1)
50+
BGCOL = (int(os.getenv("BGCOL_R")), int(os.getenv("BGCOL_G")), int(os.getenv("BGCOL_B")))
51+
for col in BGCOL:
52+
if col < 0 or col > 255:
53+
print("[error] BGCOL values must be in range 0-255")
54+
exit(1)
55+
CHECK_EVERY = int(os.getenv("CHECK_EVERY"))
56+
if os.getenv("SPOTIPY_CLIENT_ID") == "ENTER_OWN_HERE":
57+
print("[error] You must create and specify a spotify client id. If not sure follow install steps in README.md")
58+
exit(1)
59+
except KeyError or TypeError as e:
60+
print(f"[error] An error occured during setting of configuration\n{e}")
61+
exit(1)
2162

2263
## DO NOT TOUCH, NOT CONFIGURATION ##
2364
GLOBAL_LOCK = threading.Lock()
65+
OAUTH2_SCOPES = (
66+
'user-modify-playback-state',
67+
'user-read-currently-playing',
68+
'user-read-playback-state'
69+
)
2470
RUN = True
2571
SP: spotipy.Spotify
2672

@@ -76,7 +122,12 @@ def screenOFFProcedure(self: App):
76122
screenOFFProcedure(self)
77123
return
78124
if playback["is_playing"]:
79-
if not self.isScreen: self.comm.ScreenOn(); self.isScreen = True; print("Turning screen ON")
125+
if not self.isScreen:
126+
self.lock.acquire()
127+
self.comm.ScreenOn()
128+
self.lock.release()
129+
self.isScreen = True
130+
print("Turning screen ON")
80131
if not self.isSong: self.isSong = True
81132
id = playback["item"]["id"]
82133
self.time_done = seconds_to_time(int(playback["progress_ms"]/1000))
@@ -158,7 +209,7 @@ def drawLoginPage(self):
158209
logo = Image.open("res/imgs/spoti-logo.png")
159210
logo.thumbnail((219,219))
160211
self.lock.acquire()
161-
self.comm.DisplayText("Please authorize this application to access your spotify data\nThere should be a new tab in your default browser\nIf that didnt happen, go to: \nhttp://localhost:9099/", x=2, y=219+5,
212+
self.comm.DisplayText("Please authorize this application to access your spotify data\nThere should be a new tab in your default browser", x=2, y=219+5,
162213
font="Roboto-Italic.ttf",
163214
font_size=18,
164215
font_color=(255, 255, 255),
@@ -183,6 +234,13 @@ def seconds_to_time(seconds):
183234
def time_to_seconds(time_obj: datetime.time) -> int:
184235
return time_obj.hour * 3600 + time_obj.minute * 60 + time_obj.second
185236

237+
def HandleAuth() -> spotipy.Spotify:
238+
# BRUH, I implemented a whole flask web server and it just had this builtin
239+
sp = spotipy.Spotify(oauth_manager=SpotifyPKCE(scope=OAUTH2_SCOPES))
240+
oauth: SpotifyPKCE = sp.oauth_manager
241+
oauth.get_access_token()
242+
return sp
243+
186244
if __name__ == "__main__":
187245
def stopall(signum, frame):
188246
global RUN
@@ -214,6 +272,4 @@ def stopall(signum, frame):
214272
while RUN:
215273
pass
216274

217-
lcd_comm.closeSerial()
218-
219-
# TODO: add .env configuration
275+
lcd_comm.closeSerial()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ pywin32==306
1010
PyYAML==6.0.2
1111
spotipy==2.24.0
1212
uptime==3.0.1
13-
flask == 3.0.3
13+
flask==3.0.3
14+
python-dotenv == 1.0.1

spotify_auth.py

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)