feat(providers): add a pluggable station-catalogue seam

The app now holds one RadioProvider and asks it for stations, song history and stream URLs instead of importing SomaFM's HTTP client directly.

- models.py: provider-neutral Song/Station plus the shared parsers every catalogue needs (Station.provider/.uid keep ids unique once there is more than one).
- providers/base.py: the RadioProvider interface and the toolkit it needs in practice - httpx client factory, .pls/.m3u resolution with an 8h disk cache, and a default stream_candidates() strategy.
- providers/somafm/: everything SomaFM-specific (channels.json, songs/<id>.json, PLS layout, guessed icecast mirrors) behind SomaFMProvider.
- providers/__init__.py: registry, get_provider() and --provider, remembered in the config file.

Widgets render branding, splash copy and the help credits from the active provider, so a second catalogue needs no UI changes; with SomaFM selected the screen is byte-identical to before.
This commit is contained in:
beefcarver committed 2026-11-03 16:10:30 -05:00
1 parent e30ca09627
commit c4cc91ee1a
13 files changed
+708 -372

No files matched your search

+36 -28
View File
@@ -1,4 +1,10 @@
"""The Textual application: layout, key bindings and orchestration."""
"""The Textual application: layout, key bindings and orchestration.
The app is deliberately catalogue-agnostic: it holds one
:class:`~beefy_player.providers.base.RadioProvider` and asks it for stations,
song history and stream URLs. SomaFM is one such provider; see
:mod:`beefy_player.providers`.
"""
from __future__ import annotations
@@ -17,11 +23,12 @@ from textual.css.query import NoMatches
from textual.theme import Theme
from textual.widgets import Input
from . import api, artwork, bandcamp, config
from .api import Station
from .fallback import fallback_channels
from . import artwork, bandcamp, config
from .mediasession import create_media_session, media_actions_queue
from .models import Station, parse_last_playing
from .player import Player
from .providers import get_provider
from .providers.base import is_direct_stream, make_client
from .spectrum import SpectrumEngine
from .widgets.header import HeaderBar
from .widgets.help import HelpScreen
@@ -56,11 +63,11 @@ TERMINAL_THEME = Theme(
)
class SomaFMApp(App[None]):
class BeefyPlayerApp(App[None]):
CSS_PATH = "theme.tcss"
ENABLE_COMMAND_PALETTE = False
TITLE = "SomaFM"
SUB_TITLE = "terminal radio"
TITLE = "Beefy Player"
SUB_TITLE = "terminal internet radio"
BINDINGS: ClassVar[list[Binding]] = [
Binding("up,k", "cursor_up", "up", show=False),
@@ -89,6 +96,7 @@ class SomaFMApp(App[None]):
def __init__(
self,
*,
provider: str | None = None,
autoplay: bool | None = None,
station_id: str | None = None,
volume: int | None = None,
@@ -104,6 +112,8 @@ class SomaFMApp(App[None]):
self.register_theme(TERMINAL_THEME)
self.theme = TERMINAL_THEME.name
self.config = config.Config.load()
#: The station catalogue in use (see :mod:`beefy_player.providers`).
self.provider = get_provider(provider or self.config.provider)
# One-off command line overrides are deliberately *not* written back
# into the config file (only an explicit --volume is remembered).
self.autoplay = self.config.autoplay if autoplay is None else autoplay
@@ -177,16 +187,16 @@ class SomaFMApp(App[None]):
self.spectrum.start()
self.media.start()
self._http = api.make_client()
self._http = make_client(self.provider.user_agent)
stations = api.load_cached_channels()
stations = self.provider.load_cached_stations()
if not stations:
stations = api.parse_channels({"channels": fallback_channels()})
stations = self.provider.fallback_stations()
self._set_stations(stations)
self._select_initial_station()
if self.current is not None:
cached = api.load_cached_songs(self.current.id)
cached = self.provider.load_cached_history(self.current.id)
if cached:
self.current.history = cached
self.current.now_playing = cached[0]
@@ -195,7 +205,7 @@ class SomaFMApp(App[None]):
self._tick_timer = self.set_interval(0.05, self._tick)
if not self.offline:
self._refresh_channels()
self._refresh_stations()
if self.current is not None:
self._fetch_history(self.current.id)
@@ -238,7 +248,7 @@ class SomaFMApp(App[None]):
station.favorite = previous.favorite
station.favorite = self.config.is_favorite(station.id)
if station.now_playing is None and station.last_playing:
station.now_playing = api.parse_last_playing(station.last_playing)
station.now_playing = parse_last_playing(station.last_playing)
self.stations = stations
wanted = self.current.id if self.current is not None else self.start_station_id
@@ -337,10 +347,10 @@ class SomaFMApp(App[None]):
return
if self.offline or self._http is None:
urls = station.synthetic_mirrors()
urls = self.provider.fallback_urls(station)
else:
try:
urls = await api.stream_candidates(self._http, station)
urls = await self.provider.stream_candidates(self._http, station)
except Exception as exc:
self.playing_state = "error"
self._set_notice(f"could not resolve stream: {exc}", 8)
@@ -357,7 +367,7 @@ class SomaFMApp(App[None]):
self.query_one(StagePanel).refresh_stage()
return
direct = next((url for url in urls if api.is_direct_stream(url)), None)
direct = next((url for url in urls if is_direct_stream(url)), None)
self._stream_url = direct or urls[0]
if direct is not None:
self.spectrum.set_source(direct)
@@ -371,9 +381,9 @@ class SomaFMApp(App[None]):
if station is None or self._http is None:
return
try:
songs = await api.fetch_history(self._http, station_id)
songs = await self.provider.fetch_history(self._http, station_id)
except Exception:
songs = api.load_cached_songs(station_id)
songs = self.provider.load_cached_history(station_id)
if not songs:
return
@@ -408,9 +418,9 @@ class SomaFMApp(App[None]):
return
song = station.now_playing
self.title = (
f"SomaFM — {song.display_artist} · {song.display_title}"
f"{self.provider.name} — {song.display_artist} · {song.display_title}"
if song is not None
else "SomaFM"
else self.provider.name
)
if song is not None:
self.media.set_track(
@@ -426,7 +436,7 @@ class SomaFMApp(App[None]):
self.media.set_track(
station=station.title,
title=station.last_playing or station.title,
artist=station.dj or "SomaFM",
artist=station.dj or self.provider.name,
album=station.description,
art_url=station.image,
stream_url=self._stream_url,
@@ -687,17 +697,15 @@ class SomaFMApp(App[None]):
# workers
# ------------------------------------------------------------------ #
@work(exclusive=True, group="channels", exit_on_error=False)
async def _refresh_channels(self) -> None:
@work(exclusive=True, group="stations", exit_on_error=False)
async def _refresh_stations(self) -> None:
if self.offline or self._http is None:
return
try:
stations = await api.fetch_channels(self._http)
stations = await self.provider.fetch_stations(self._http)
except Exception:
if not self.stations:
self._set_stations(
api.parse_channels({"channels": fallback_channels()})
)
self._set_stations(self.provider.fallback_stations())
self._set_notice("offline — using the built-in station list", 8)
else:
self._set_notice("station refresh failed — using the cached list", 6)
@@ -848,7 +856,7 @@ class SomaFMApp(App[None]):
self._set_notice("offline mode — nothing to refresh", 3)
return
self.artwork_url = ""
self._refresh_channels()
self._refresh_stations()
if self.current is not None:
self._update_artwork()
self._fetch_history(self.current.id)