subclient

Subsonic ncurses client
git clone https://git.e1e0.net/subclient.git
Log | Files | Refs | README

commit 84c1de8fb7f013d21a079caef4d7e5e563a808c8
parent e65e114f024ac64ac28f78f55917e4f29bf5a0a4
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 13 Jun 2021 18:30:43 +0200

basic player running

Diffstat:
Asubclient/playback.py | 22++++++++++++++++++++++
Msubclient/subclient.py | 28++++++++++++++++++++++++++--
Msubclient/subsonic.py | 3+++
3 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/subclient/playback.py b/subclient/playback.py @@ -0,0 +1,22 @@ +from python_mpv_jsonipc import MPV +import tempfile + + +class Player: + def __init__(self): + self.player = MPV(ipc_socket='/tmp/subclient-mpv-socket', + audio_display='no') + + def play(self, stream): + with tempfile.NamedTemporaryFile() as fp: + while True: + chunk = stream.read(512*1024) + if not chunk: + break + fp.write(chunk) + fp.seek(0) + self.player.play(fp.name) + self.player.wait_for_property('eof-reached') + + def exit(self): + self.player.terminate() diff --git a/subclient/subclient.py b/subclient/subclient.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import configparser -from subclient.subsonic import Subsonic +from subclient import subsonic from subclient import helpers +from subclient import playback import py_cui @@ -15,8 +16,11 @@ class SubClient: self.config = configparser.ConfigParser() self.config.read('config.ini') - self.subsonic = Subsonic(self.config['subclient']) + self.subsonic = subsonic.Subsonic(self.config['subclient']) + self.player = playback.Player() self.master = master + + self.master.run_on_exit(self.player.exit) # The scrolled list cells that will contain our tasks in each of the # three categories self.artist_scroll_cell = self.master.add_scroll_menu( @@ -28,12 +32,19 @@ class SubClient: self.now_playing_block = self.master.add_block_label( 'Now', 4, 0, row_span=1, column_span=3) + # self.master.add_key_command(py_cui.keys.KEY_Q_LOWER, self._exit) self.artist_scroll_cell.add_key_command(py_cui.keys.KEY_ENTER, self.update_albums) + self.artist_scroll_cell.add_key_command(py_cui.keys.KEY_TAB, + self._switch_to_next) self.album_scroll_cell.add_key_command(py_cui.keys.KEY_ENTER, self.update_songs) + self.album_scroll_cell.add_key_command(py_cui.keys.KEY_TAB, + self._switch_to_next) self.song_scroll_cell.add_key_command(py_cui.keys.KEY_ENTER, self.play_song) + self.song_scroll_cell.add_key_command(py_cui.keys.KEY_TAB, + self._switch_to_first) self.update_artists() @@ -56,11 +67,24 @@ class SubClient: def play_song(self): song = self.song_scroll_cell.get() self.now_playing_block.set_title(self._now_playing_format(song)) + stream = self.subsonic.get_song_stream(song) + self.player.play(stream) def _now_playing_format(self, song): return (f'Now playing:\n' f'{song.title} ({helpers.format_duration(song.duration)})') + def _switch_to_next(self): + all_widgets = self.master.get_widgets() + current_widget = self.master.get_selected_widget() + neighbors = self.master._get_horizontal_neighbors( + current_widget, + py_cui.keys.KEY_RIGHT_ARROW) + self.master.set_selected_widget(all_widgets[neighbors[0]].get_id()) + + def _switch_to_first(self): + self.master.set_selected_widget(self.artist_scroll_cell.get_id()) + def main(): root = py_cui.PyCUI(5, 3) diff --git a/subclient/subsonic.py b/subclient/subsonic.py @@ -54,3 +54,6 @@ class Subsonic: def get_songs_from_album(self, album): songs = self.s.getAlbum(album.id)['album']['song'] return [Song(**s) for s in songs] + + def get_song_stream(self, song): + return self.s.stream(song.id, tformat='raw')