From b7d75e1d18b6f803c7be5955d378813256523405 Mon Sep 17 00:00:00 2001 From: Ed Carroll Date: Thu, 4 Jun 2015 00:49:17 +0100 Subject: [PATCH 1/4] 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. --- beetsplug/plexupdate.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/beetsplug/plexupdate.py b/beetsplug/plexupdate.py index 5aa096486..b2743f515 100644 --- a/beetsplug/plexupdate.py +++ b/beetsplug/plexupdate.py @@ -1,9 +1,10 @@ """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) @@ -15,10 +16,10 @@ 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 +32,12 @@ 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) - api_endpoint = 'library/sections/{0}/refresh'.format(section_key) + 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. @@ -44,6 +45,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 += '?X-Plex-Token={0}'.format(token) + return url + + class PlexUpdate(BeetsPlugin): def __init__(self): super(PlexUpdate, self).__init__() @@ -51,7 +60,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 +78,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: From f78393aa8687b32835a23894362d8785078a9b43 Mon Sep 17 00:00:00 2001 From: Ed Carroll Date: Thu, 4 Jun 2015 01:13:30 +0100 Subject: [PATCH 2/4] Updated tests to include Plex Token --- test/test_plexupdate.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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(): From 1db1edfb42e09e1641327ca31952e970e8e963a9 Mon Sep 17 00:00:00 2001 From: Ed Carroll Date: Thu, 4 Jun 2015 01:33:17 +0100 Subject: [PATCH 3/4] Fixed line lengths --- beetsplug/plexupdate.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beetsplug/plexupdate.py b/beetsplug/plexupdate.py index b2743f515..480007d66 100644 --- a/beetsplug/plexupdate.py +++ b/beetsplug/plexupdate.py @@ -1,5 +1,6 @@ """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. + +Plex Home users enter the Plex Token to enable updating. Put something like the following in your config.yaml to configure: plex: host: localhost @@ -37,7 +38,8 @@ def update_plex(host, port, token): """ # 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) + 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. From 865fb0a25f5036f8cb0415edd4a491578fd7fc63 Mon Sep 17 00:00:00 2001 From: Ed Carroll Date: Thu, 4 Jun 2015 10:48:38 +0100 Subject: [PATCH 4/4] Updated docs and changelog Updated the append function to be a bit safer when adding the token parameter. Updated docs and changelog --- beetsplug/plexupdate.py | 3 ++- docs/changelog.rst | 2 ++ docs/plugins/plexupdate.rst | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/beetsplug/plexupdate.py b/beetsplug/plexupdate.py index 480007d66..02ed2324c 100644 --- a/beetsplug/plexupdate.py +++ b/beetsplug/plexupdate.py @@ -12,6 +12,7 @@ from __future__ import (division, absolute_import, print_function, 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 @@ -51,7 +52,7 @@ 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) + url += '?' + urlencode({'X-Plex-Token': token}) return url 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.