I'm working on TUI spotify player with urwid library and can't figure out how to update text with the name of playing track every time track is changed.
I have 2 widgets on the screen. First with liked tracks and second with currently playing track. I know about function currrent_playback but I don't understand how to get information that user changed a song and correspondingly text with currently playing song should be changed.
I came up with this function, but still don't understand how can I call it every time user changes track:
def get_current_playing_track(sp):
playback = sp.current_playback()
if playback and playback['is_playing']:
return f"{playback['item']['artists'][0]['name']} | {playback['item']['name']}"
else:
return 'Nothing is playing now...'
Function for creating "liked tracks widget"
# right_part of the screen
def liked_tracks_widget(liked_tracks, sp):
right_part = urwid.Overlay(liked_tracks, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
align='right', width=('relative', 50),
valign='top', height=('relative', 90),
min_width=20, min_height=9)
return right_part
Function for displaying "currently playing track widget"
# lower part of the screen
def playing_track_widget(sp, playing_track):
text_object = urwid.Text(playing_track)
text_filler = urwid.Filler(text_object, 'bottom')
frame = urwid.Frame(text_filler, footer=None)
return frame
Main function, where I run urwid loop:
# main function for TUI
def run_tui(tracks, sp):
liked_tracks = urwid.Padding(convert_to_ListBox(u'Liked Tracks', tracks.values(), sp), left=2, right=2)
# updated state of currently playing track
playing_track = get_current_playing_track(sp)
# widget with liked tracks
right_part = liked_tracks_widget(liked_tracks, sp)
# widget with currently playing track
lower_part = playing_track_widget(sp, playing_track)
columns = urwid.Columns([
('weight', 1, lower_part),
('weight', 1, right_part)
], dividechars=1)
main_loop = urwid.MainLoop(columns, unhandled_input=unhandled_input)
main_loop.run()