commit 94c5c46725154cb27e790ce1af6bff38fadff71c
parent d829883339e531de1d426b011db9f046829d6966
Author: Paco Esteban <paco@e1e0.net>
Date: Mon, 7 Jun 2021 19:20:21 +0200
play X random songs as per config
Diffstat:
3 files changed, 43 insertions(+), 20 deletions(-)
diff --git a/helpers.py b/helpers.py
@@ -0,0 +1,13 @@
+import time
+
+
+def format_duration(seconds):
+ """Formats a number of seconds in hours:minutes:seconds
+
+ :param seconds: The number of seconds to format
+ :type seconds: int
+ :return: String of the form "%H:%M:%S"
+ :rtype: string
+ """
+ ty_res = time.gmtime(seconds)
+ return time.strftime("%H:%M:%S", ty_res)
diff --git a/playback.py b/playback.py
@@ -0,0 +1,15 @@
+import subprocess
+import tempfile
+
+
+def play(stream):
+ """ Initiates a subprocess for the audio player, mpv(1) by default
+
+ :param stream: subsonic stream returned by libsonic.stream()
+ :type stream: file-like object
+ """
+ with tempfile.NamedTemporaryFile() as fp:
+ fp.write(stream.read())
+ fp.seek(0)
+ subprocess.call(["mpv", "--no-audio-display", "--really-quiet",
+ fp.name])
diff --git a/subclient/subclient.py b/subclient/subclient.py
@@ -2,34 +2,29 @@
import configparser
import libsonic
-import sys
+
+from helpers import format_duration
+from playback import play
def main():
config = configparser.ConfigParser()
config.read('config.ini')
- url = config['subsonic']['url']
- user = config['subsonic']['username']
- pwd = config['subsonic']['password']
-
- conn = libsonic.Connection(url, user, pwd, port=443)
-
- # Let's get 2 completely random songs
- # songs = conn.getRandomSongs(size=2)
- # We'll just pretty print the results we got to the terminal
- # pprint(songs)
-
- stream = conn.stream('370bce250b179939304f7ac3eed94936', tformat='raw')
+ url = config['subclient']['url']
+ user = config['subclient']['username']
+ pwd = config['subclient']['password']
+ random_list_size = config['subclient']['randomListSize']
- while True:
- chunk = stream.read(1024)
- if not chunk:
- sys.stderr.write("EOF\n")
- break
+ subsonic = libsonic.Connection(url, user, pwd, port=443)
- sys.stdout.buffer.write(chunk)
- # print(dir(stream))
+ # Let's get X completely random songs
+ songs = subsonic.getRandomSongs(size=random_list_size)
+ for song in songs['randomSongs']['song']:
+ print(f"Playing: {song['title']} by {song['artist']}"
+ f"({format_duration(song['duration'])})")
+ stream = subsonic.stream(song['id'], tformat='raw')
+ play(stream)
if __name__ == "__main__":