| 1 |
"""Updates a Kodi library whenever the beets library is changed. |
| 2 |
This is based on the Plex Update plugin. |
| 3 |
|
| 4 |
Put something like the following in your config.yaml to configure: |
| 5 |
kodi: |
| 6 |
host: localhost |
| 7 |
port: 8080 |
| 8 |
user: user |
| 9 |
pwd: secret |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
from typing import TYPE_CHECKING |
| 15 |
|
| 16 |
import requests |
| 17 |
|
| 18 |
from beets.plugins import BeetsPlugin |
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from beets.library import LibModel, Library |
| 22 |
|
| 23 |
|
| 24 |
def update_kodi( |
| 25 |
host: str, port: int, user: str, password: str |
| 26 |
) -> requests.Response: |
| 27 |
"""Sends request to the Kodi api to start a library refresh.""" |
| 28 |
url = f"http://{host}:{port}/jsonrpc" |
| 29 |
|
| 30 |
"""Content-Type: application/json is mandatory |
| 31 |
according to the kodi jsonrpc documentation""" |
| 32 |
|
| 33 |
headers = {"Content-Type": "application/json"} |
| 34 |
|
| 35 |
# Create the payload. Id seems to be mandatory. |
| 36 |
payload = {"jsonrpc": "2.0", "method": "AudioLibrary.Scan", "id": 1} |
| 37 |
return requests.post( |
| 38 |
url, auth=(user, password), json=payload, headers=headers, timeout=10 |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
class KodiUpdate(BeetsPlugin): |
| 43 |
def __init__(self) -> None: |
| 44 |
super().__init__("kodi") |
| 45 |
|
| 46 |
# Adding defaults. |
| 47 |
self.config.add( |
| 48 |
[{"host": "localhost", "port": 8080, "user": "kodi", "pwd": "kodi"}] |
| 49 |
) |
| 50 |
|
| 51 |
self.config["user"].redact = True |
| 52 |
self.config["pwd"].redact = True |
| 53 |
self.register_listener("database_change", self.listen_for_db_change) |
| 54 |
|
| 55 |
def listen_for_db_change(self, lib: Library, model: LibModel) -> None: |
| 56 |
"""Listens for beets db change and register the update""" |
| 57 |
self.register_listener("cli_exit", self.update) |
| 58 |
|
| 59 |
def update(self, lib: Library) -> None: |
| 60 |
"""When the client exists try to send refresh request to Kodi server.""" |
| 61 |
self._log.info("Requesting a Kodi library update...") |
| 62 |
|
| 63 |
kodi = self.config.get() |
| 64 |
|
| 65 |
# Backwards compatibility in case not configured as an array |
| 66 |
if not isinstance(kodi, list): |
| 67 |
kodi = [kodi] |
| 68 |
|
| 69 |
for instance in kodi: |
| 70 |
# Try to send update request. |
| 71 |
try: |
| 72 |
r = update_kodi( |
| 73 |
instance["host"], |
| 74 |
instance["port"], |
| 75 |
instance["user"], |
| 76 |
instance["pwd"], |
| 77 |
) |
| 78 |
r.raise_for_status() |
| 79 |
|
| 80 |
json = r.json() |
| 81 |
if json.get("result") != "OK": |
| 82 |
self._log.warning( |
| 83 |
"Kodi update failed: JSON response was {0!r}", json |
| 84 |
) |
| 85 |
continue |
| 86 |
|
| 87 |
self._log.info( |
| 88 |
"Kodi update triggered for {}:{}", |
| 89 |
instance["host"], |
| 90 |
instance["port"], |
| 91 |
) |
| 92 |
except requests.exceptions.RequestException as e: |
| 93 |
self._log.warning("Kodi update failed: {}", str(e)) |
| 94 |
continue |