11from 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
25from library .lcd .lcd_simulated import LcdSimulated
36from library .lcd .lcd_comm import LcdComm , Orientation
47from PIL import Image
58from time import sleep
6- from spotify_auth import HandleAuth
79import spotipy
10+ from spotipy .oauth2 import SpotifyPKCE
811import datetime
912import threading
1013import signal
11- import io
14+ import io , os , os . path
1215import 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 ##
2364GLOBAL_LOCK = threading .Lock ()
65+ OAUTH2_SCOPES = (
66+ 'user-modify-playback-state' ,
67+ 'user-read-currently-playing' ,
68+ 'user-read-playback-state'
69+ )
2470RUN = True
2571SP : 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\n There should be a new tab in your default browser\n If that didnt happen, go to: \n http://localhost:9099/ " , x = 2 , y = 219 + 5 ,
212+ self .comm .DisplayText ("Please authorize this application to access your spotify data\n There 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):
183234def 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+
186244if __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 ()
0 commit comments