Skip to content

Commit

Permalink
Merge pull request #1494 from edcarroll/master
Browse files Browse the repository at this point in the history
PlexUpdate plugin updated for Plex Home
  • Loading branch information
sampsyo committed Jun 4, 2015
2 parents 419a12a + 865fb0a commit 7cb0037
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
26 changes: 20 additions & 6 deletions 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.
Expand All @@ -31,27 +34,37 @@ 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.
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 += '?' + urlencode({'X-Plex-Token': 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'port': 32400,
u'token': u''})

self.register_listener('database_change', self.listen_for_db_change)

Expand All @@ -68,7 +81,8 @@ def update(self, lib):
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:
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions docs/plugins/plexupdate.rst
Expand Up @@ -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:

Expand All @@ -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
-------------
Expand All @@ -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.
6 changes: 4 additions & 2 deletions test/test_plexupdate.py
Expand Up @@ -87,7 +87,8 @@ def test_get_music_section(self):
# 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):
Expand All @@ -98,7 +99,8 @@ def test_update_plex(self):
# 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():
Expand Down

0 comments on commit 7cb0037

Please sign in to comment.