Skip to content

Commit 73fa635

Browse files
author
GuardKenzie
committed
Fixed an issue where the player would crash if the playlist was cleared while running
Fixed an issue where the player would crash when songs did not have metadata
1 parent bcf864d commit 73fa635

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

bin/miniplayer

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,34 @@ class Player:
359359
art.save(self.album_art_loc, "PNG")
360360

361361

362+
def getSongInfo(self, song):
363+
"""
364+
A function that returns a tuple of the given songs
365+
album, artist, title
366+
if they do not exist, the function will return
367+
"", "", filename respectively
368+
"""
369+
try:
370+
album = song["album"]
371+
except KeyError:
372+
album = ""
373+
374+
try:
375+
artist = song["artist"]
376+
except KeyError:
377+
artist = ""
378+
379+
try:
380+
title = song["title"]
381+
except KeyError:
382+
# If no title, use base file name
383+
aux = song["file"]
384+
aux = os.path.basename(aux)
385+
title = aux
386+
387+
return album, artist, title
388+
389+
362390
def checkSongUpdate(self):
363391
"""
364392
Checks if there is a new song playing
@@ -385,25 +413,7 @@ class Player:
385413
if self.control_cycle == 0:
386414
self.selected_song = int(song["pos"])
387415

388-
try:
389-
self.album = song["album"]
390-
except KeyError:
391-
self.album = ""
392-
393-
try:
394-
self.artist = song["artist"]
395-
except KeyError:
396-
self.artist = ""
397-
398-
try:
399-
self.title = song["title"]
400-
except KeyError:
401-
# If no title, use base file name
402-
aux = song["file"]
403-
aux = os.path.basename(aux)
404-
aux = os.path.splitext(aux)[0]
405-
self.title = aux
406-
416+
self.album, self.artist, self.title = self.getSongInfo(song)
407417
self.last_song = song
408418

409419
self.getAlbumArt(song["file"])
@@ -517,8 +527,11 @@ class Player:
517527

518528
elif action == "select":
519529
self.control_cycle = 1
520-
self.client.play(self.selected_song % len(self.client.playlist()))
521-
self.update_needed = True
530+
playlist_length = len(self.client.playlist())
531+
532+
if playlist_length > 0:
533+
self.client.play(self.selected_song % playlist_length)
534+
self.update_needed = True
522535

523536

524537
key = self.stdscr.getch()
@@ -586,6 +599,13 @@ class Player:
586599
current_song = self.client.currentsong()
587600

588601
# selected_pos = int(current_song["pos"])
602+
playlist_length = len(self.client.playlist())
603+
if playlist_length == 0:
604+
selected_pos = 0
605+
self.playlist_win.erase()
606+
self.playlist_win.refresh()
607+
return
608+
589609
selected_pos = self.selected_song % len(playlist)
590610

591611
# Determine where to start the playlist
@@ -618,8 +638,15 @@ class Player:
618638
self.playlist_win.move(line, 0)
619639

620640
if playlist_item is not None:
641+
_, artist, title = self.getSongInfo(playlist_item)
642+
643+
if artist == "":
644+
sep = ""
645+
else:
646+
sep = " - "
647+
621648
self.playlist_win.addstr(
622-
f"{playlist_item['artist']} - {playlist_item['title']}"[:self.playlist_window_width - 1],
649+
f"{artist}{sep}{title}"[:self.playlist_window_width - 1],
623650
pair
624651
)
625652

0 commit comments

Comments
 (0)