Use the same environment variables as mpc does

This commit is contained in:
Danielle McLean 2023-12-06 11:42:50 +11:00
parent 9ff488d807
commit 095c099a38
Signed by: 00dani
GPG key ID: 52C059C3B22A753E
3 changed files with 13 additions and 11 deletions

View file

@ -25,11 +25,11 @@ You may not need any configuration! If you've got a relatively normal MPD setup
Currently, mpd-now-playable can only be configured through environment variables. Command-line arguments are intentionally not supported, since your MPD password is among the supported settings and command-line arguments are not a secure way to pass secrets such as passwords into commands. Reading configuration from a file is secure, so mpd-now-playable may support a config file in future. Currently, mpd-now-playable can only be configured through environment variables. Command-line arguments are intentionally not supported, since your MPD password is among the supported settings and command-line arguments are not a secure way to pass secrets such as passwords into commands. Reading configuration from a file is secure, so mpd-now-playable may support a config file in future.
The following environment variables are read: The following environment variables are read. The `MPD_HOST` and `MPD_PORT` variables are supported in the same way `mpc` uses them, but you can alternatively provide your password as a separate `MPD_PASSWORD` variable if you wish.
- `MPD_HOSTNAME` - defaults to `localhost`, which should be fine for most users. If you want to control a remote MPD server, though, you can. - `MPD_HOST` - defaults to `localhost`, which should be fine for most users. If you want to control a remote MPD server, though, you can.
- `MPD_PORT` - defaults to 6600, which will almost always be the correct port to use. - `MPD_PORT` - defaults to 6600, which will almost always be the correct port to use.
- `MPD_PASSWORD` - has no default. Set this only if your MPD server expects a password. - `MPD_PASSWORD` - has no default. Set this only if your MPD server expects a password. You can also provide a password by setting `MPD_HOST=password@host`, if you want to be consistent with how `mpc` works.
One simple secure way to set your environment variables is with a small wrapper script like this: One simple secure way to set your environment variables is with a small wrapper script like this:
```shell ```shell

View file

@ -8,13 +8,15 @@ from .mpd.listener import MpdStateListener
async def listen() -> None: async def listen() -> None:
port = int(environ.get("MPD_PORT", "6600"))
host = environ.get("MPD_HOST", "localhost")
password = environ.get("MPD_PASSWORD")
if password is None and "@" in host:
password, host = host.split("@", maxsplit=1)
listener = MpdStateListener() listener = MpdStateListener()
now_playing = CocoaNowPlaying(listener) now_playing = CocoaNowPlaying(listener)
await listener.start( await listener.start(hostname=host, port=port, password=password)
hostname=environ.get("MPD_HOSTNAME", "localhost"),
port=int(environ.get("MPD_PORT", "6600")),
password=environ.get("MPD_PASSWORD"),
)
await listener.loop(now_playing) await listener.loop(now_playing)

View file

@ -46,10 +46,10 @@ class MpdStateListener(Player):
self.art_cache = MpdArtworkCache(self) self.art_cache = MpdArtworkCache(self)
async def start( async def start(
self, hostname: str = "localhost", port: int = 6600, password: str | None = None self, host: str = "localhost", port: int = 6600, password: str | None = None
) -> None: ) -> None:
print(f"Connecting to MPD server {hostname}:{port}...") print(f"Connecting to MPD server {host}:{port}...")
await self.client.connect(hostname, port) await self.client.connect(host, port)
if password is not None: if password is not None:
print("Authorising to MPD with your password...") print("Authorising to MPD with your password...")
await self.client.password(password) await self.client.password(password)