Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlexUpdate plugin updated for Plex Home #1494

Merged
merged 4 commits into from
Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions beetsplug/plexupdate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""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)
Expand All @@ -15,10 +17,10 @@
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 +33,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 += '?X-Plex-Token={0}'.format(token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For safety, it's probably best to use:

url += '?' + urllib.encode({'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 +80,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
6 changes: 4 additions & 2 deletions test/test_plexupdate.py
Original file line number Diff line number Diff line change
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