subclient

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

commit 8ea075cbd70cefccbf755e5ad7ba5a304f58eaf5
parent 343ebf538e56b3eff6c2045b9f86b4a85eb1080a
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 29 Aug 2021 19:25:33 +0200

implement rudimentary cache

Diffstat:
Msubclient/helpers.py | 19+++++++++++++++++++
Msubclient/player.py | 35+++++++++++++++++++++++------------
Msubclient/subclient.py | 13++-----------
3 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/subclient/helpers.py b/subclient/helpers.py @@ -1,5 +1,8 @@ +import os import time +HOME = os.environ.get('HOME') + def format_duration(seconds): """Formats a number of seconds in hours:minutes:seconds @@ -12,3 +15,19 @@ def format_duration(seconds): time_format = "%H:%M:%S" if seconds > 3600 else "%M:%S" ty_res = time.gmtime(seconds) return time.strftime(time_format, ty_res) + + +def get_cache_folder(): + cache_home = os.environ.get('XDG_CACHE_HOME') + if cache_home: + return cache_home + '/subclient' + else: + return HOME + '/.cache/subclient' + + +def get_config_file(): + config_home = os.environ.get('XDG_CONFIG_HOME') + if config_home: + return config_home + '/subclient.ini' + else: + return HOME + '/.config/subclient.ini' diff --git a/subclient/player.py b/subclient/player.py @@ -1,5 +1,6 @@ from python_mpv_jsonipc import MPV -import tempfile +from subclient import helpers +import os class Player: @@ -7,23 +8,33 @@ class Player: self.mpv = MPV(ipc_socket='/tmp/subclient-mpv-socket', audio_display='no') self.orig = origin + self.cache_dir = helpers.get_cache_folder() def play(self, playlist): self.stop() for s in playlist: - stream = self.orig.get_song_stream(s) - fp = tempfile.NamedTemporaryFile(prefix='subclient_song_', - delete=False) - while True: - chunk = stream.read(512*1024) - if not chunk: - break - fp.write(chunk) - fp.seek(0) - self.mpv.loadfile(fp.name, 'append-play') - fp.close() + filename = self.download_stream(s) + self.mpv.loadfile(filename, 'append-play') # self.mpv.wait_for_property('eof-reached') + def download_stream(self, song): + if not os.path.isdir(self.cache_dir): + os.makedirs(self.cache_dir) + + stream = self.orig.get_song_stream(song) + filename = f'{self.cache_dir}/{song.id}' + + if not os.path.isfile(filename): + with open(filename, 'wb') as fp: + while True: + chunk = stream.read(512*1024) + if not chunk: + break + fp.write(chunk) + fp.close() + + return filename + def exit(self): self.mpv.terminate() diff --git a/subclient/subclient.py b/subclient/subclient.py @@ -5,21 +5,12 @@ import configparser import curses from subclient import subsonic from subclient import player -import os +from subclient import helpers __version__ = 'v0.0.1' -def get_config_file(): - home = os.environ.get('HOME') - config_home = os.environ.get('XDG_CONFIG_HOME') - if config_home: - return config_home + '/subclient.ini' - else: - return home + '/.config/subclient.ini' - - class SubClient: UP = -1 DOWN = 1 @@ -32,7 +23,7 @@ class SubClient: def __init__(self): self.config = configparser.ConfigParser() - self.config.read(get_config_file()) + self.config.read(helpers.get_config_file()) self.subsonic = subsonic.Subsonic(self.config['subclient'])