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:
1 parent
e30ca09627
commit
c4cc91ee1a
13 files changed
+708
-372
No files matched your search
+24
-13
@@ -6,16 +6,23 @@ import argparse
|
||||
import asyncio
|
||||
from collections.abc import Sequence
|
||||
|
||||
from . import __version__, api
|
||||
from .fallback import fallback_channels
|
||||
from . import __version__
|
||||
from .providers import available_providers, get_provider
|
||||
from .providers.base import make_client
|
||||
from .spectrum import SOURCE_MODES
|
||||
|
||||
|
||||
def _build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="somafm-tui",
|
||||
description="A flashy terminal radio for somafm.com — live spectrum, "
|
||||
"song history and desktop media integration.",
|
||||
prog="beefy-player",
|
||||
description="Beefy Player — a flashy terminal player for internet "
|
||||
"radio: live spectrum, song history and desktop media integration.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--provider",
|
||||
choices=available_providers(),
|
||||
metavar="ID",
|
||||
help="station catalogue to use (default: the one in the config file)",
|
||||
)
|
||||
parser.add_argument("--station", metavar="ID", help="station id to select")
|
||||
parser.add_argument(
|
||||
@@ -72,13 +79,15 @@ def _build_parser() -> argparse.ArgumentParser:
|
||||
return parser
|
||||
|
||||
|
||||
def _list_stations(offline: bool) -> int:
|
||||
stations = api.load_cached_channels()
|
||||
def _list_stations(offline: bool, provider_id: str | None = None) -> int:
|
||||
provider = get_provider(provider_id)
|
||||
stations = provider.load_cached_stations()
|
||||
if not stations and not offline:
|
||||
|
||||
async def load():
|
||||
client = api.make_client()
|
||||
client = make_client(provider.user_agent)
|
||||
try:
|
||||
return await api.fetch_channels(client)
|
||||
return await provider.fetch_stations(client)
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
@@ -87,7 +96,8 @@ def _list_stations(offline: bool) -> int:
|
||||
except Exception:
|
||||
stations = []
|
||||
if not stations:
|
||||
stations = api.parse_channels({"channels": fallback_channels()})
|
||||
stations = provider.fallback_stations()
|
||||
print(f"# {provider.name} ({provider.id}) — {provider.station_source}")
|
||||
for station in sorted(stations, key=lambda item: item.title.casefold()):
|
||||
print(f"{station.id:<20} {station.title}")
|
||||
return 0
|
||||
@@ -103,13 +113,14 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
return 0
|
||||
|
||||
if args.list:
|
||||
return _list_stations(args.offline)
|
||||
return _list_stations(args.offline, args.provider)
|
||||
|
||||
# Import the app lazily so --list/--version do not probe the terminal for
|
||||
# image protocols.
|
||||
from .app import SomaFMApp
|
||||
from .app import BeefyPlayerApp
|
||||
|
||||
app = SomaFMApp(
|
||||
app = BeefyPlayerApp(
|
||||
provider=args.provider,
|
||||
autoplay=args.autoplay,
|
||||
station_id=args.station,
|
||||
volume=args.volume,
|
||||
|
||||
Reference in new issue
Block a user