mirror of
https://github.com/beetbox/beets.git
synced 2026-02-12 10:22:13 +01:00
Add a plugin to update a Kodi music library
I created one for an older version before, but it didn't work since the change to Python 3. So I created a new one that works.
This commit is contained in:
parent
44963598e8
commit
77d155cdea
1 changed files with 66 additions and 0 deletions
66
beetsplug/kodiupdate.py
Normal file
66
beetsplug/kodiupdate.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""Updates a Kodi library whenever the beets library is changed. This is based on the Plex Update plugin.
|
||||
|
||||
Put something like the following in your config.yaml to configure:
|
||||
kodi:
|
||||
host: localhost
|
||||
port: 8080
|
||||
user: user
|
||||
pwd: secret
|
||||
"""
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import requests
|
||||
import json
|
||||
from requests.auth import HTTPBasicAuth
|
||||
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.
|
||||
"""
|
||||
url = "http://{0}:{1}/jsonrpc/".format(host, port)
|
||||
|
||||
# The kodi jsonrpc documentation states that Content-Type: application/json is mandatory
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
# Create the payload. Id seems to be mandatory.
|
||||
payload = {'jsonrpc': '2.0', 'method':'AudioLibrary.Scan', 'id':1}
|
||||
r = requests.post(url, auth=HTTPBasicAuth(user, password), json=payload, headers=headers)
|
||||
return r
|
||||
|
||||
class KodiUpdate(BeetsPlugin):
|
||||
def __init__(self):
|
||||
super(KodiUpdate, self).__init__()
|
||||
|
||||
# Adding defaults.
|
||||
config['kodi'].add({
|
||||
u'host': u'localhost',
|
||||
u'port': 8080,
|
||||
u'user': u'kodi',
|
||||
u'pwd': u'kodi'})
|
||||
|
||||
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 for the end"""
|
||||
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(u'Updating Kodi library...')
|
||||
|
||||
# Try to send update request.
|
||||
try:
|
||||
update_kodi(
|
||||
config['kodi']['host'].get(),
|
||||
config['kodi']['port'].get(),
|
||||
config['kodi']['user'].get(),
|
||||
config['kodi']['pwd'].get())
|
||||
self._log.info(u'... started.')
|
||||
|
||||
except requests.exceptions.RequestException:
|
||||
self._log.warning(u'Update failed.')
|
||||
|
||||
Loading…
Reference in a new issue