beets/beetsplug/plexupdate.py
Ed Carroll b7d75e1d18 PlexUpdate plugin updated for Plex Home
PlexUpdate plugin updated to allow Plex Tokens to be provided with the
server details so that beets can update a Plex library that requires
authentication.
2015-06-04 00:49:17 +01:00

86 lines
2.7 KiB
Python

"""Updates an Plex library whenever the beets library is changed.
For Plex home users ensure that the token is filled out with the Plex Token for your user.
Put something like the following in your config.yaml to configure:
plex:
host: localhost
port: 32400
token: token
"""
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import requests
from urlparse import urljoin
import xml.etree.ElementTree as ET
from beets import config
from beets.plugins import BeetsPlugin
def get_music_section(host, port, token):
"""Getting the section key for the music library in Plex.
"""
api_endpoint = append_token('library/sections', token)
url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint)
# Sends request.
r = requests.get(url)
# Parse xml tree and extract music section key.
tree = ET.fromstring(r.text)
for child in tree.findall('Directory'):
if child.get('title') == 'Music':
return child.get('key')
def update_plex(host, port, token):
"""Sends request to the Plex api to start a library refresh.
"""
# Getting section key and build url.
section_key = get_music_section(host, port, token)
api_endpoint = append_token('library/sections/{0}/refresh'.format(section_key), token)
url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint)
# Sends request and returns requests object.
r = requests.get(url)
return r
def append_token(url, token):
"""Appends the Plex Home token to the api call if required.
"""
if token:
url += '?X-Plex-Token={0}'.format(token)
return url
class PlexUpdate(BeetsPlugin):
def __init__(self):
super(PlexUpdate, self).__init__()
# Adding defaults.
config['plex'].add({
u'host': u'localhost',
u'port': 32400,
u'token': u''})
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 Plex server.
"""
self._log.info('Updating Plex library...')
# Try to send update request.
try:
update_plex(
config['plex']['host'].get(),
config['plex']['port'].get(),
config['plex']['token'].get())
self._log.info('... started.')
except requests.exceptions.RequestException:
self._log.warning('Update failed.')