@@ -51,7 +51,7 @@ def poll(self):
5151 dr ,dw ,de = select .select ([sys .stdin ], [], [], 0.0 )
5252 return sys .stdin .read (1 ) if not dr == [] else None
5353
54- def get_lyrics (artist , title ):
54+ def fetch_lyrics (artist , title ):
5555 title = re .sub (r'(-.*)' , '' , title )
5656 search_string = f'{ artist } { title } lyrics'
5757 search_string = urllib .parse .quote_plus (search_string )
@@ -99,33 +99,47 @@ def save_lyrics(lyrics, lyrics_file):
9999
100100atexit .register (show_cursor )
101101
102- @ueberzug .Canvas ()
103- def main (canvas ):
104- rows , columns = terminal_size ()
105- song , artist , album , art_url = get_spotify_song_data ()
106-
102+ def get_lyrics (song , artist , album , art_url ):
107103 home = str (Path .home ())
108104 lyrics_directory = os .path .join (home , '.cache' , 'spotify-lyrics' )
109105 artist_directory = os .path .join (lyrics_directory , artist .replace ('/' , '' ))
106+ album_directory = os .path .join (artist_directory , album .replace ('/' , '' ))
110107 image_directory = os .path .join (artist_directory , 'album_arts' )
111- lyrics_file = os .path .join (artist_directory , song .replace ('/' , '' ))
108+ lyrics_file = os .path .join (album_directory , song .replace ('/' , '' ))
112109 image_file = '{}.png' .format (os .path .join (image_directory , album ))
113110
114111 if not os .path .isdir (lyrics_directory ): os .mkdir (lyrics_directory )
115112 if not os .path .isdir (artist_directory ): os .mkdir (artist_directory )
113+ if not os .path .isdir (album_directory ): os .mkdir (album_directory )
116114 if not os .path .isdir (image_directory ): os .mkdir (image_directory )
117115
118116 if not os .path .exists (lyrics_file ):
119- lyrics = get_lyrics (artist , song )
117+ lyrics = fetch_lyrics (artist , song )
120118 save_lyrics (lyrics , lyrics_file )
121119 else :
122120 lyrics = read_lyrics (lyrics_file )
123121
122+
124123 try :
125124 if not os .path .exists (image_file ):
126125 urlretrieve (art_url , image_file )
127126 except FileNotFoundError :
128127 pass
128+ except urllib .error .URLError :
129+ pass
130+
131+ data = {'lyrics' : lyrics ,
132+ 'lyrics_file' : lyrics_file ,
133+ 'image_file' : image_file }
134+
135+ return lyrics , lyrics_file , image_file
136+
137+
138+ @ueberzug .Canvas ()
139+ def main (canvas ):
140+ rows , columns = terminal_size ()
141+ song , artist , album , art_url = get_spotify_song_data ()
142+ lyrics , lyrics_file , image_file = get_lyrics (song , artist , album , art_url )
129143
130144 album_cover = canvas .create_placement ('album_cover' ,
131145 x = columns // 2 , y = 4 ,
@@ -143,6 +157,20 @@ def main(canvas):
143157 old_rows , old_columns = rows , columns
144158 with KeyPoller () as key_poller :
145159 while True :
160+ new_song , new_artist , new_album , new_art_url = get_spotify_song_data ()
161+ if new_song != song or new_artist != artist :
162+ lyrics , lyrics_file , image_file = get_lyrics (new_song , new_artist ,
163+ new_album , new_art_url )
164+ album_cover .path = image_file
165+ song = new_song
166+ artist = new_artist
167+ album = new_album
168+ art_url = new_art_url
169+
170+ os .system ('clear' )
171+ move_cursor (0 , 0 )
172+ print_metadata (artist , album , song )
173+
146174 rows , columns = terminal_size ()
147175 if old_rows != rows or old_columns != columns :
148176 difference = rows - old_rows
@@ -177,14 +205,14 @@ def main(canvas):
177205 if c == 'q' :
178206 os .system ('clear' )
179207 break
180- if c == 'j' :
208+ elif c == 'j' :
181209 if rows - start_row == n_entries :
182210 current_line += 1
183211 current_line = min (current_line , len (wrapped_lines )- 1 )
184- if c == 'k' :
212+ elif c == 'k' :
185213 current_line += - 1
186214 current_line = max (current_line , 0 )
187- if c == 'e' :
215+ elif c == 'e' :
188216 try :
189217 EDITOR = os .environ .get ('EDITOR' )
190218 call ([EDITOR , lyrics_file ])
@@ -194,13 +222,13 @@ def main(canvas):
194222 os .system ('clear' )
195223 print ('$EDITOR is not set' )
196224 time .sleep (1 )
197- if c == 'r' :
225+ elif c == 'r' :
198226 os .system ('clear' )
199227 move_cursor (0 , 0 )
200228 print_metadata (artist , album , song )
201- if c == 'd' :
229+ elif c == 'd' :
202230 os .remove (lyrics_file )
203- lyrics = get_lyrics (artist , song )
231+ lyrics = fetch_lyrics (artist , song )
204232 save_lyrics (lyrics , lyrics_file )
205233
206234if __name__ == '__main__' :
0 commit comments