| 1 |
"""Updates an MPD index whenever the library is changed. |
| 2 |
|
| 3 |
Put something like the following in your config.yaml to configure: |
| 4 |
mpd: |
| 5 |
host: localhost |
| 6 |
port: 6600 |
| 7 |
password: seekrit |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import os |
| 13 |
import socket |
| 14 |
from typing import TYPE_CHECKING |
| 15 |
|
| 16 |
from beets import config |
| 17 |
from beets.plugins import BeetsPlugin |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from beets.library import LibModel, Library |
| 21 |
|
| 22 |
|
| 23 |
# No need to introduce a dependency on an MPD library for such a |
| 24 |
# simple use case. Here's a simple socket abstraction to make things |
| 25 |
# easier. |
| 26 |
class BufferedSocket: |
| 27 |
"""Socket abstraction that allows reading by line.""" |
| 28 |
|
| 29 |
def __init__(self, host: str, port: int, sep: bytes = b"\n") -> None: |
| 30 |
if host[0] in ["/", "~"]: |
| 31 |
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 32 |
self.sock.connect(os.path.expanduser(host)) |
| 33 |
else: |
| 34 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 35 |
self.sock.connect((host, port)) |
| 36 |
self.buf = b"" |
| 37 |
self.sep = sep |
| 38 |
|
| 39 |
def readline(self) -> bytes: |
| 40 |
while self.sep not in self.buf: |
| 41 |
data = self.sock.recv(1024) |
| 42 |
if not data: |
| 43 |
break |
| 44 |
self.buf += data |
| 45 |
if self.sep in self.buf: |
| 46 |
res, self.buf = self.buf.split(self.sep, 1) |
| 47 |
return res + self.sep |
| 48 |
return b"" |
| 49 |
|
| 50 |
def send(self, data: bytes) -> None: |
| 51 |
self.sock.send(data) |
| 52 |
|
| 53 |
def close(self) -> None: |
| 54 |
self.sock.close() |
| 55 |
|
| 56 |
|
| 57 |
class MPDUpdatePlugin(BeetsPlugin): |
| 58 |
def __init__(self) -> None: |
| 59 |
super().__init__() |
| 60 |
config["mpd"].add( |
| 61 |
{ |
| 62 |
"host": os.environ.get("MPD_HOST", "localhost"), |
| 63 |
"port": int(os.environ.get("MPD_PORT", 6600)), |
| 64 |
"password": "", |
| 65 |
} |
| 66 |
) |
| 67 |
config["mpd"]["password"].redact = True |
| 68 |
|
| 69 |
# For backwards compatibility, use any values from the |
| 70 |
# plugin-specific "mpdupdate" section. |
| 71 |
for key in config["mpd"].keys(): |
| 72 |
if self.config[key].exists(): |
| 73 |
config["mpd"][key] = self.config[key].get() |
| 74 |
|
| 75 |
self.register_listener("database_change", self.db_change) |
| 76 |
|
| 77 |
def db_change(self, lib: Library, model: LibModel) -> None: |
| 78 |
self.register_listener("cli_exit", self.update) |
| 79 |
|
| 80 |
def update(self, lib: Library) -> None: |
| 81 |
self.update_mpd( |
| 82 |
config["mpd"]["host"].as_str(), |
| 83 |
config["mpd"]["port"].get(int), |
| 84 |
config["mpd"]["password"].as_str(), |
| 85 |
) |
| 86 |
|
| 87 |
def update_mpd( |
| 88 |
self, |
| 89 |
host: str = "localhost", |
| 90 |
port: int = 6600, |
| 91 |
password: str | None = None, |
| 92 |
) -> None: |
| 93 |
"""Sends the "update" command to the MPD server indicated, |
| 94 |
possibly authenticating with a password first. |
| 95 |
""" |
| 96 |
self._log.info("Updating MPD database...") |
| 97 |
|
| 98 |
try: |
| 99 |
s = BufferedSocket(host, port) |
| 100 |
except OSError: |
| 101 |
self._log.warning("MPD connection failed", exc_info=True) |
| 102 |
return |
| 103 |
|
| 104 |
resp = s.readline() |
| 105 |
if b"OK MPD" not in resp: |
| 106 |
self._log.warning("MPD connection failed: {0!r}", resp) |
| 107 |
return |
| 108 |
|
| 109 |
if password: |
| 110 |
s.send(f'password "{password}"\n'.encode()) |
| 111 |
resp = s.readline() |
| 112 |
if b"OK" not in resp: |
| 113 |
self._log.warning("Authentication failed: {0!r}", resp) |
| 114 |
s.send(b"close\n") |
| 115 |
s.close() |
| 116 |
return |
| 117 |
|
| 118 |
s.send(b"update\n") |
| 119 |
resp = s.readline() |
| 120 |
if b"updating_db" not in resp: |
| 121 |
self._log.warning("Update failed: {0!r}", resp) |
| 122 |
|
| 123 |
s.send(b"close\n") |
| 124 |
s.close() |
| 125 |
self._log.info("Database updated.") |