From 7b6a1ed30c96b9b3072109f1b6988330084acbca Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Tue, 16 Sep 2025 16:25:58 -0500 Subject: [PATCH] added config file with caching and bitrate --- textual1/SubSonic.py | 5 ++++- textual1/app.py | 20 ++++++++++++++++---- textual1/config.ini | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 textual1/config.ini diff --git a/textual1/SubSonic.py b/textual1/SubSonic.py index bdb78c4..bec6de4 100644 --- a/textual1/SubSonic.py +++ b/textual1/SubSonic.py @@ -306,11 +306,14 @@ class SubpyConn: return starred # Returns a requests Response object, the data stream can be found in Response.content - def stream(self, song_id: str) -> requests.Response: + def stream(self, song_id: str, maxBitRate: int | None = None) -> requests.Response: url = self.base_url + '/rest/stream?' payload = self._getBasePayload() + if maxBitRate: + payload['maxBitRate'] = maxBitRate + payload['id'] = song_id return requests.get(url + self._encodePayload(payload), stream=True) diff --git a/textual1/app.py b/textual1/app.py index c21c714..796b38a 100644 --- a/textual1/app.py +++ b/textual1/app.py @@ -1,8 +1,10 @@ +import configparser import logging +import asyncio import random import os +import re import io -import asyncio LOG_FILE = 'textual.log' LOGGING_LEVEL = logging.DEBUG @@ -12,7 +14,14 @@ LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s' if os.path.exists(LOG_FILE): os.remove(LOG_FILE) -CACHE = True +# Read config file +config = configparser.ConfigParser() +config.read('config.ini') + +CACHE = config.getboolean('STREAMING', 'Cache') +BITRATE = config.get('STREAMING', 'Bitrate') +BITRATE = re.findall(r'[0-9]+', BITRATE)[0] +BITRATE = int(BITRATE) if BITRATE else BITRATE os.makedirs(".cache/art/", exist_ok=True) os.makedirs(".cache/audio/", exist_ok=True) @@ -30,7 +39,7 @@ from textual import on, events, work from textual.binding import Binding from textual.reactive import reactive from textual.app import App, ComposeResult, RenderResult -from textual.widgets import Footer, Header, OptionList, DataTable, ProgressBar, Label, Input, Placeholder +from textual.widgets import Footer, Header, OptionList, DataTable, ProgressBar, Label, Input from textual.widget import Widget from textual.containers import Horizontal, Vertical, Container from textual.screen import Screen @@ -639,7 +648,10 @@ class TextualSubpyPlayer(App): asyncio.create_task(asyncio.to_thread(conn.scrobble, song.id, submission=False)) else: # Run the blocking stream call in a separate thread - r = await asyncio.to_thread(conn.stream, song.id) + if BITRATE: + r = await asyncio.to_thread(conn.stream, song.id, maxBitRate=BITRATE) + else: + r = await asyncio.to_thread(conn.stream, song.id) audio_content = r.content if CACHE: diff --git a/textual1/config.ini b/textual1/config.ini new file mode 100644 index 0000000..ba20e20 --- /dev/null +++ b/textual1/config.ini @@ -0,0 +1,3 @@ +[STREAMING] +Cache = true +Bitrate = 320kbps