update Beets

This commit is contained in:
AdeHub
2024-08-24 16:44:41 +12:00
parent a63098a919
commit 046d4d82b4
116 changed files with 17353 additions and 9964 deletions
+44 -34
View File
@@ -24,27 +24,29 @@ Put something like the following in your config.yaml to configure:
"""
import requests
from beets import config
from beets.plugins import BeetsPlugin
def update_kodi(host, port, user, password):
"""Sends request to the Kodi api to start a library refresh.
"""
"""Sends request to the Kodi api to start a library refresh."""
url = f"http://{host}:{port}/jsonrpc"
"""Content-Type: application/json is mandatory
according to the kodi jsonrpc documentation"""
headers = {'Content-Type': 'application/json'}
headers = {"Content-Type": "application/json"}
# Create the payload. Id seems to be mandatory.
payload = {'jsonrpc': '2.0', 'method': 'AudioLibrary.Scan', 'id': 1}
payload = {"jsonrpc": "2.0", "method": "AudioLibrary.Scan", "id": 1}
r = requests.post(
url,
auth=(user, password),
json=payload,
headers=headers)
headers=headers,
timeout=10,
)
return r
@@ -54,42 +56,50 @@ class KodiUpdate(BeetsPlugin):
super().__init__()
# Adding defaults.
config['kodi'].add({
'host': 'localhost',
'port': 8080,
'user': 'kodi',
'pwd': 'kodi'})
config["kodi"].add(
[{"host": "localhost", "port": 8080, "user": "kodi", "pwd": "kodi"}]
)
config['kodi']['pwd'].redact = True
self.register_listener('database_change', self.listen_for_db_change)
config["kodi"]["pwd"].redact = True
self.register_listener("database_change", self.listen_for_db_change)
def listen_for_db_change(self, lib, model):
"""Listens for beets db change and register the update"""
self.register_listener('cli_exit', self.update)
self.register_listener("cli_exit", self.update)
def update(self, lib):
"""When the client exists try to send refresh request to Kodi server.
"""
self._log.info('Requesting a Kodi library update...')
"""When the client exists try to send refresh request to Kodi server."""
self._log.info("Requesting a Kodi library update...")
# Try to send update request.
try:
r = update_kodi(
config['kodi']['host'].get(),
config['kodi']['port'].get(),
config['kodi']['user'].get(),
config['kodi']['pwd'].get())
r.raise_for_status()
kodi = config["kodi"].get()
except requests.exceptions.RequestException as e:
self._log.warning('Kodi update failed: {0}',
str(e))
return
# Backwards compatibility in case not configured as an array
if not isinstance(kodi, list):
kodi = [kodi]
json = r.json()
if json.get('result') != 'OK':
self._log.warning('Kodi update failed: JSON response was {0!r}',
json)
return
for instance in kodi:
# Try to send update request.
try:
r = update_kodi(
instance["host"],
instance["port"],
instance["user"],
instance["pwd"],
)
r.raise_for_status()
self._log.info('Kodi update triggered')
json = r.json()
if json.get("result") != "OK":
self._log.warning(
"Kodi update failed: JSON response was {0!r}", json
)
continue
self._log.info(
"Kodi update triggered for {0}:{1}",
instance["host"],
instance["port"],
)
except requests.exceptions.RequestException as e:
self._log.warning("Kodi update failed: {0}", str(e))
continue