diff --git a/beetsplug/plexupdate.py b/beetsplug/plexupdate.py index 5aa096486..02ed2324c 100644 --- a/beetsplug/plexupdate.py +++ b/beetsplug/plexupdate.py @@ -1,24 +1,27 @@ """Updates an Plex library whenever the beets library is changed. +Plex Home users enter the Plex Token to enable updating. 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 +from urllib import urlencode import xml.etree.ElementTree as ET from beets import config from beets.plugins import BeetsPlugin -def get_music_section(host, port): +def get_music_section(host, port, token): """Getting the section key for the music library in Plex. """ - api_endpoint = 'library/sections' + api_endpoint = append_token('library/sections', token) url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint) # Sends request. @@ -31,12 +34,13 @@ def get_music_section(host, port): return child.get('key') -def update_plex(host, port): +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) + section_key = get_music_section(host, port, token) api_endpoint = 'library/sections/{0}/refresh'.format(section_key) + api_endpoint = append_token(api_endpoint, token) url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint) # Sends request and returns requests object. @@ -44,6 +48,14 @@ def update_plex(host, port): return r +def append_token(url, token): + """Appends the Plex Home token to the api call if required. + """ + if token: + url += '?' + urlencode({'X-Plex-Token': token}) + return url + + class PlexUpdate(BeetsPlugin): def __init__(self): super(PlexUpdate, self).__init__() @@ -51,7 +63,8 @@ class PlexUpdate(BeetsPlugin): # Adding defaults. config['plex'].add({ u'host': u'localhost', - u'port': 32400}) + u'port': 32400, + u'token': u''}) self.register_listener('database_change', self.listen_for_db_change) @@ -68,7 +81,8 @@ class PlexUpdate(BeetsPlugin): try: update_plex( config['plex']['host'].get(), - config['plex']['port'].get()) + config['plex']['port'].get(), + config['plex']['token'].get()) self._log.info('... started.') except requests.exceptions.RequestException: diff --git a/docs/changelog.rst b/docs/changelog.rst index daf9210df..4f24cc37e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -56,6 +56,8 @@ Fixes: * Fix a bug, where the autotagger still considers matches that are specifically listed under the config's ignored section. :bug:`1487` * Fix a bug with unicode strings when generating thumbnails. :bug:`1485` +* :doc:`/plugins/plexupdate`: Fixed library updates not working when in a Plex + Home by allowing a token with requests. 1.3.13 (April 24, 2015) diff --git a/docs/plugins/plexupdate.rst b/docs/plugins/plexupdate.rst index 09ff940de..2e589f6a5 100644 --- a/docs/plugins/plexupdate.rst +++ b/docs/plugins/plexupdate.rst @@ -13,6 +13,9 @@ which looks like this:: plex: host: localhost port: 32400 + token: token + +Use the token configuration option only when in a Plex Home (see `Plex Token`_) To use the ``plexupdate`` plugin you need to install the `requests`_ library with: @@ -23,6 +26,7 @@ server every time you change your beets library. .. _Plex: http://plex.tv/ .. _requests: http://docs.python-requests.org/en/latest/ +.. _Plex Token: https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token Configuration ------------- @@ -33,3 +37,5 @@ The available options under the ``plex:`` section are: Default: ``localhost``. - **port**: The Plex server port. Default: 32400. +- **token**: The Plex Home token. + Default: Empty. diff --git a/test/test_plexupdate.py b/test/test_plexupdate.py index b173e23e4..9949be933 100644 --- a/test/test_plexupdate.py +++ b/test/test_plexupdate.py @@ -87,7 +87,8 @@ class PlexUpdateTest(unittest.TestCase, TestHelper): # Test if section key is "2" out of the mocking data. self.assertEqual(get_music_section( self.config['plex']['host'], - self.config['plex']['port']), '2') + self.config['plex']['port'], + self.config['plex']['token']), '2') @responses.activate def test_update_plex(self): @@ -98,7 +99,8 @@ class PlexUpdateTest(unittest.TestCase, TestHelper): # Testing status code of the mocking request. self.assertEqual(update_plex( self.config['plex']['host'], - self.config['plex']['port']).status_code, 200) + self.config['plex']['port'], + self.config['plex']['token']).status_code, 200) def suite():